Results 1 to 8 of 8
  1. #1
    rlsublime is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    58

    VBA to Format Exported Excel Data

    I have the following do cmd code that will export my query to an excel file. I am trying to edit the format of the file within the code itself. How would I be able to do that?Thanks



    DoCmd.OutputTo acQuery, "Investor_Request_Query", "MicrosoftExcel 97-10(*.xls)", "C:\Investor Request Report.xls", True, ""

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    In order to format, you would use automation:

    http://support.microsoft.com/kb/247412
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rlsublime is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    58
    Thanks still not sure how to implement the following line. The link that you posted has a section on creating a new query within the code but what i am trying to do is format the Repurchase_Report excel file. Thanks

    DoCmd.OutputTo acQuery, "Repurchase_Query", "MicrosoftExcel 97-10(*.xls)", "C:\Repurchase_Report.xls", True, ""

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I deleted your duplicate thread. You would use automation to open the file created by your OutputTo code, and apply your desired formatting.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    rlsublime is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    58
    I am not sure how to select "C:\Repurchase_Report.xls" in the code and make formatting changes to the sheet. Any ideas?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I happen to be working on an Excel automation project at the moment. Here are the relevant lines (strFile contains the full path to the file):

    Dim xl As Object
    Set xl = CreateObject("excel.application")

    xl.Workbooks.Open (strFile)

    I don't know what formatting you're trying to do, but a handy trick I use is to record a macro in Excel doing the things you want. Look in the VBA editor to see the code that got created. It will give you the shell of what you need to do from Access.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    rlsublime is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    58
    Thanks. I tried the following code and was able to generate the report, however, I was not able to successfully format the sheet. For example in the code below, the ‘Columns("A:A").EntireColumn.AutoFit” code did not work and gave me a message “Sub or function not defined. Thanks for the help.

    Code:
     
    Dim xl As Object
    Set xl = CreateObject("C:\Repurchase_Report.xls")
    DoCmd.OutputTo acQuery, "Repurchase_Query", "MicrosoftExcel 97-10(*.xls)", "C:\Repurchase_Report.xls", True, ""
    Activesheet.select
    Columns("A:A").EntireColumn.AutoFit
    xl.Workbooks.Open (C:\Repurchase_Report.xls)

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    When automating from Access, you need to include the Excel object:


    xl.Columns("A:A").EntireColumn.AutoFit

    Also, I think you want to do the OutputTo before opening the file for formatting (or do the entire process via automation).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 5
    Last Post: 02-24-2012, 11:02 AM
  2. Auto Update an Exported Excel Spreadsheet
    By halt4814 in forum Access
    Replies: 1
    Last Post: 01-18-2012, 04:30 PM
  3. to open Exported Excel File
    By waqas in forum Programming
    Replies: 3
    Last Post: 10-16-2011, 12:33 PM
  4. Replies: 1
    Last Post: 04-13-2011, 01:23 PM
  5. Exporting and Updating exported data
    By Singapore Sam in forum Import/Export Data
    Replies: 2
    Last Post: 12-15-2009, 09:33 AM

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