Link to home
Start Free TrialLog in
Avatar of VTKegan
VTKeganFlag for United States of America

asked on

Access -> Excel Template with macros

I am using the following code to export from Access to Excel:

Dim xlObj As Object, xltPath As String
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("DMEL-Excel-Query")
xltPath = GetProjectDirectory() & "MEL\DMELHeader.xltx"
Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add (xltPath)
    With xlObj
        .Range("E3") = DLookup("[ProjectName]", "Main-ProjectInformation")
        .Range("E4") = DLookup("[ProjectLocation]", "Main-ProjectInformation")
        .Range("A6").CopyFromRecordset rs
    End With
    CheckForFile st
    xlObj.ActiveWorkbook.SaveAs st

xlObj.Quit
Set xlObj = Nothing

I am trying to create Excel Macros that do certain things, and I would like to create them in the template file so when I export to Excel I use the template file, copy in the data, and then have access to the macros.

Is this possible? If so do I need to save as something else? or do something different with my template file?

Any advice is greatly appreciated.
Avatar of rockiroads
rockiroads
Flag of United States of America image

You could try this

open up your excel template, click on tools/macro - > record a macro
do your stuff then stop the macro
now look at the generated code
apply to your excel automation code

simplest way I can think of
Avatar of VTKegan

ASKER

I already have the macros created.  

The problem is when I export my excel data using the template file and save, the macro does not go along with it.  If I try to use the buttons it just says that the macro cannot be found.
ok, I understand now. You have a template file with a macro and when you save it as an xls, you want the macro to be saved with it.
My bad as I thought you wanted to create additional vba code.

I would say you should open up your template file Workbooks.open("c:\mytemplate.xls") then use this obect. At the end, instead of performing a save, do a saveas

or perform the saveas at the initial point of opening, so you have your new workbook then, then do the ops you want.
Avatar of VTKegan

ASKER

Excel template files are in the format .xltx

When you open xltx files, the default command isnt to actually open the template file, it opens a xlsx version with the template file in the background.

The point of the save command shouldn't matter.  What I save as could matter but whether I copy then save or save then copy shouldn't affect this.

Should I not use .xltx files and only use .xlsx files?
Avatar of VTKegan

ASKER

In order for macros to go along with the template file, do I need to save as a macro enabled workbook?
yes, I take it then you are using office 2007 or 2010
You need to save your template as either *.xltm or the older *.xlt format.

Further, when you save your resulting file, you need to save it as either *.xlsm, or the older *.xls format.
looks like macro enabled templates are suffixed with xltm
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial