Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22

    Export to Excel Template

    Hi,



    I'm wanting to export data from Access in to an Excel template. I'm using a query and command button to do this, and so far have been able to export to Excel ok, however I want to send this to a template where the top 2 rows have nicely formatted heading. So essentially, I want to export data to row 3 of the excel template.

    The methods I've used so far seem to overwrite the template rather than leaving the template as is and saving the output a new file. Therefore, if this is also possible, I want the export to use the template file, but ensure that a new file is saved leaving the template blank to be used again and again.

    I seem to be getting lost in a world of VBA code, so any held will be greatly appreciated.

    Thanks,

    Phil

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Cant, but you can open excel, then COPYFROMRECORDSET to row 3.

    Code:
    'be sure to put excel object library, in vbe, tools, references
    
    dim XL As Excel.Application
    Set XL = CreateObject("excel.application")
    
    set rst = currentdb.openrecordset("qsMyQuery")
    with xl
      .Workbooks.Open sXLfile
      .range("A3").select
      .ActiveCell.CopyFromRecordset rst
    end with
    Set XL = nothing

  3. #3
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Hi ranman256,

    Thanks very much for your help. I've copied this code in to my database and changed the query, but unfortunately it doesn't appear to be working. Any suggestions?

    Thank you.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    And what does 'not working' mean - error message, wrong results, nothing happens?

    Post your exact code.
    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
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Hi June7,

    I have copied the code exactly as above and have just changed the query to the corresponding one in my database. I have attached a picture of the error I'm getting when I click the command button.

    Many thanks,

    Phil Click image for larger version. 

Name:	Error.jpg 
Views:	25 
Size:	98.5 KB 
ID:	33841

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    Code is using early binding to Excel objects. Make sure you have Microsoft Excel x.0 Object Library activated in VBA editor: Tools>References
    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.

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also the top two lines on EVERY code module should be
    Code:
    Option Compare Database
    Option Explicit   '<<<--- add this line

  8. #8
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Hi all,

    Thanks for your help so far, and apologies for the delay in getting back to you.

    I have tried the codes as suggested and amended the Tools > References to the Excel object library but I am still getting an error message. If I leave out the 'Option Compare...' lines as suggested above the system looks to attempt opening Excel but then I get a different error message. If I leave these lines in, I get the same error as shown above.

    I have uploaded a picture of the new error message and the code I have been using.

    Thanks again everyone...really do appreciate all your help, I'm clearly an Access novice.

    Phil Click image for larger version. 

Name:	Error 2.jpg 
Views:	17 
Size:	88.9 KB 
ID:	33902

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    What is the value of sXLFile? Where is this variable declared and set?

    You are copying ranman's code but need to adapt it to your situation. Replace sXLFile with a string that is the path\filename of your workbook.

    If you had Option Explicit in the module header as suggested by ssanfu, the compiler would probably have flagged this variable as undeclared because I doubt it has been declared anywhere.
    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.

  10. #10
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Thanks for this.

    I've changed this to the filepath, so my code now reads:

    Private Sub Command0_Click()
    Dim XL As Excel.Application
    Set XL = CreateObject("excel.application")
    Set rst = CurrentDb.OpenRecordset("qryExportAllProfBodies")
    With XL
    .Workbooks.Open.C:\Desktop\qryExportAllProfBodiesT emplate.xlsx
    .range("A3").select
    .ActiveCell.CopyFromRecordset rst
    End With
    Set XL = Nothing
    End Sub


    However, I keep getting an error message which says:

    Compile error:

    Expected: line number or label or statement or end of statement


    Within the code, the line which contains the file path is now highlighted red.

  11. #11
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    It need to be in quotes as a string

    Code:
    .Workbooks.Open "C:\Desktop\qryExportAllProfBodiesT emplate.xlsx"
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    Literal strings must always be within quotes or apostrophes.
    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.

  13. #13
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Hi,

    Thanks for this tip, I now seem to be getting somewhere. The only issue I now have is that Excel isn't actually opening. For testing purposes I'm just saving the template to my desktop. When I click the command button I can see the database processing and then an icon appears on my desktop (the type you see when you have a document open which is slightly faded). But Excel doesn't open. It's only when I double click the original file it opens. But in doing so I'm getting messages telling me I'm overwriting, etc. Any suggestions?

    When I do open Excel the data is there.

    Thanks
    Phil

  14. #14
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Try making the object visible;
    Code:
    With XL
        .Visible
        .Workbooks.Open"C:\Desktop\qryExportAllProfBodiesTemplate.xlsx"
        .range("A3").select
        .ActiveCell.CopyFromRecordset rst
    End With
    Last edited by Minty; 05-10-2018 at 10:00 AM. Reason: Used the old incorrect code!
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #15
    phil123 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    22
    Afraid I just get an error message:

    Compile error:

    Invalid use of property

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Missing data in export to excel template
    By MissaLissa in forum Modules
    Replies: 1
    Last Post: 08-03-2017, 11:22 AM
  2. Export Query Results to Excel Template
    By laterdater in forum Macros
    Replies: 2
    Last Post: 09-25-2015, 11:20 AM
  3. Replies: 3
    Last Post: 07-31-2014, 01:08 AM
  4. Replies: 1
    Last Post: 10-28-2013, 12:32 PM
  5. Export table to excel using excel template
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 1
    Last Post: 06-27-2011, 02:10 AM

Tags for this Thread

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