I've found the following code online to try an automate an Excel macro.
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\Excel Report Creator.xlsm"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB,0, true)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\Excel Report Creator" & "!ReferenceSheet.CommandButton1_Click"
on error resume next
myExcelWorker.Run strMacroName
if err.number <> 0 Then
MsgBox "errerr: " & err.Description
End If
err.clear
on error goto 0
oWorkBook.Save
myExcelWorker.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
myExcelWorker.Quit
Set myExcelWorker = Nothing
Set WshShell = Nothing
When ran I receive the following error.
The macro may not be available in this workbook or all macros may be disabled.
My spread sheet is called Excel Report Creator.xlsm, the sheet is called ReferenceSheet, and the sub is CommandButton1_Click. Am I missing anything here? Possibly a setting within Excel?
Thanks, Bill
Enable macrosmessage bar. Go into theTrust Centerin Excel and set<path-to-"Excel Report Creator.xlsm"to be a trusted location & you should be good to go.