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

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.
LVL 10
Who is Participating?
rockiroadsConnect With a Mentor Commented:
I was having a play and looking at the save options. I opened up a template but for some reason it would not save as a excel macro enabled workbook


    Set objExcel = CreateObject("Excel.Application")
    Set objWB = objExcel.Workbooks.Open("c:\temp\xtest.xltm")
    objWB.Worksheets(1).Cells(5, 4) = 22
    objExcel.Visible = True

    objWB.SaveAs "Filename:=C:\temp\xtest.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

Is your original template in excel template format?

Be back in about 15mins, gotta pop out

by the way, value of xlOpenXMLWorkbookMacroEnabled is 52

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
VTKeganAuthor Commented:
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.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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"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.
VTKeganAuthor Commented:
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?
VTKeganAuthor Commented:
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
Patrick MatthewsCommented:
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
rockiroadsConnect With a Mentor Commented:
right got it, I was doing the template bit wrong. I forgot that when you use templates you have to add the workbook, not open it up, doh!

    Set objExcel = CreateObject("Excel.Application")
    Set objWB = objExcel.Workbooks.Add("c:\temp\xtest.xltm")
    objWB.Worksheets(1).Cells(5, 4) = 22
    objExcel.Visible = True

    objWB.SaveAs "c:\temp\xtest.xlsm", xlOpenXMLWorkbookMacroEnabled

I opened up a macro enabled template, changed a value, ensured its visible to the user (for my testing purposes) then saved it as a macro enabled workbook
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.