Results 1 to 5 of 5
  1. #1
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398

    Format an Excel spreadsheet from Access

    The function below opens an excel spreadsheet, does basic formatting, then closes the spreadsheet. For some reason, Excel is staying open until I exit the database. I can't figure out why it is not closing.

    Function ExportExcel(strFileName As String, strQry As String)
    'Export query data into Excel spreadsheet
    If Dir(Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx") <> "" Then
    Kill Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx"


    DoCmd.TransferSpreadsheet acExport, 10, strQry, Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx", True, ""
    Else
    DoCmd.TransferSpreadsheet acExport, 10, strQry, Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx", True, ""
    End If

    'Create Excel Object, manipulate newly saved spreadsheet
    'Create Object variables
    Dim xlApp As Object
    Dim wbExcel As Object

    'Set Excel Object
    Set xlApp = CreateObject("Excel.Application")

    'Set Object containing opened strFileName spreadsheet
    Set wbExcel = xlApp.Workbooks.Open(Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx")

    'Manipulate spreadsheet file
    'Autofit Columns
    wbExcel.Sheets(1).Columns.AutoFit

    'Set column headings to bold typeface
    wbExcel.Sheets(1).Rows(1).Font.Bold = True

    'Set name of worksheet to Sheet1
    wbExcel.Sheets(1).Name = "Sheet1"

    'Leave cursor at cell A1
    Range("A1").Select

    'Close and Save Workbook
    xlApp.ActiveWorkbook.Close (True)

    'Close Excel Object
    xlApp.Quit

    'Clear Object variables
    Set wbExcel = Nothing
    Set xlApp = Nothing

    Thank you in advance for your help with this problem!!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Test with all the formatting code commented. Just open and close the Excel object. Does that work? Then activate each format operation one at time and test.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    You're good!! It is my range statement. What should be the proper statement to select cell A1 before saving and closing the spreadsheet?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try:

    wbExcel.Sheets("Sheet1").Range("A1").Select
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    I figured it out.
    wbExcel.Sheets(1).Cells(1,1).Select

    Thanks again!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 11-26-2012, 12:35 PM
  2. Creating excel spreadsheet from access vba
    By nyneave in forum Programming
    Replies: 1
    Last Post: 10-12-2012, 09:59 AM
  3. Open Excel spreadsheet in Access
    By carlyd in forum Forms
    Replies: 1
    Last Post: 02-17-2012, 01:09 PM
  4. Email Excel Spreadsheet from Access
    By Nancy in forum Access
    Replies: 2
    Last Post: 11-09-2010, 02:37 PM
  5. Replies: 2
    Last Post: 02-19-2010, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums