Solved

Excel Export Question

Posted on 2011-09-06
4
217 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
  • 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 500 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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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