Macro introuvable lors de l'exécution d'un sous excel avec VBS

J'ai trouvé le code suivant en ligne pour essayer d'automatiser une macro Excel.

' 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 

Lorsqu'il est exécuté, je reçois l'erreur suivante.

 The macro may not be available in this workbook or all macros may be disabled. 

Ma feuille de diffusion s'appelle Excel Report Creator.xlsm, la feuille s'appelle la feuille de reference et le sous-programme CommandButton1_Click. Est-ce que je manque quelque chose ici? Possible un réglage dans Excel?

Merci, Bill

Lorsque le nom du classur comporte des espaces, vous devez l'envelopper en guillemets simples. Et vous n'avez pas besoin du path complet si le classur est ouvert:

 ' Build the macro name with the full path to the workbook Dim strMacroName strMacroName = "'Excel Report Creator.xlsm'" & _ "!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 

Et assurez-vous que le Sub que vous appelez est Public