Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel Export Question

Posted on 2011-09-06
4
Medium Priority
?
221 Views
Last Modified: 2012-05-12
I am using a variation of the code below to send some data out to an excel template.

On the excel template, I have a command button that runs a macro.

Was wondering if this code could be modified in such a way that it automatically fires the macro tied to the command button?  I could then get rid of the command button...

Placing the macro in the workbook_open event is not working because the workbook is opened before the data is pasted in...


Dim db As dao.Database, rs As dao.Recordset
Dim i As Integer, j As Integer
Dim xlObj As Object
Dim Sheet As Object



Set db = currentdb

Set rs = db.OpenRecordset("yourQuery")
Set xlObj = CreateObject("Excel.Application")
xlObj.Workbooks.Add
Set Sheet = xlObj.ActiveWorkbook.sheets(1)
    
'copy the headers
Dim iRow, iCol
iRow = 2
    For iCol = 0 To rs.Fields.Count - 1
        Sheet.Cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
    Next


Sheet.Range("A3").CopyFromRecordset rs  'this copy just the data

sheet.range("A1").Value=txtStartDt & "-" & txtEndDt
xlObj.Visible = True

Open in new window

0
Comment
Question by:snyperj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 36491648
have you tried

Call WorkbookName!MacroName at the end of your code?

Or

Application.Run("'WorkBook Name With Spaces.xlsm'!MacroName")
0
 

Author Comment

by:snyperj
ID: 36491708
Yes, raises an error that says it can't find the procedure (Macro name)...

I also tried the full path to the .xlt file which is in a shared folder on the network.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 36492355

Dim db As dao.Database, rs As dao.Recordset
Dim i As Integer, j As Integer
Dim xlObj As Object
Dim Sheet As Object



Set db = currentdb

Set rs = db.OpenRecordset("yourQuery")
Set xlObj = CreateObject("Excel.Application")
xlObj.Workbooks.Add
Set Sheet = xlObj.ActiveWorkbook.Worksheets(1)
   
'copy the headers
Dim iRow, iCol
iRow = 2
    For iCol = 0 To rs.Fields.Count - 1
        Sheet.Cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
    Next


Sheet.Range("A3").CopyFromRecordset rs  'this copy just the data

sheet.range("A1").Value=txtStartDt & "-" & txtEndDt
xlObj.Visible = True


'ADD this line

xlobj.run "NameOfMacro"  '<<< YOU HAVE TO CHANGE  "NameOfMacro" with the ACTUAL NaME of the macro from the excel template





0
 

Author Closing Comment

by:snyperj
ID: 36495223
Thanks...
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question