On 3/26/2019 3:01 PM, Adrian Caspersz wrote:
On 26/03/2019 18:46, Lynn McGuire wrote:
On 3/26/2019 12:47 PM, Lynn McGuire wrote:
How does one execute a VBA macro in Excel from OLE ?Â* I cannot get
the C++ code to work.
BTW, I am using the C++ code from
I am calling AutoWrap with name of the VBA macro in the
visualBasicMacroName string.Â* I am getting an error that the
pDisp-GetIDsOfNames call in AutoWrap is not finding the VBA method.
Â*Â*Â*Â*Â*VariantInit ( & result1);
Â*Â*Â*Â*Â*std::string errorMsg = "Executing Visual Basic Macro, " +
visualBasicMacroName + " (ExecuteVisualBasicMacro)";
Â*Â*Â*Â*Â*WCHAR methodName ;
Â*Â*Â*Â*Â*charToWchar (visualBasicMacroName.c_str (), methodName, sizeof
(methodName) / sizeof (WCHAR));
Â*Â*Â*Â*Â*AutoWrap (DISPATCH_METHOD, & result1, pExcelWorkbooks,
methodName, errorMsg, 0);
Is the VBA method in a 'module', i.e. not a sheet.
As a visibility test, can you call it from somewhere else, say PowerShell?
FWIW, these days I'm a fan of controlling Excel from PowerShell instead
of VBA inside, as I can reach out to a lot more connected systems.
That C++ looks scary, unless you have other reasons for being in that
 event handlers being an exception.
Cool, I got it to work with the following and my macro was run:
PS C:\Users\lmc $E = New-Object -ComObject Excel.Application
PS C:\Users\lmc $workbook = $E.Workbooks.Open("C:\dii\spreadsheet2.xlsm")
PS C:\Users\lmc $E.Run("testmacro")
PS C:\Users\lmc $workbook.save()
PS C:\Users\lmc $workbook.close()
But I could not get Excel to quit:
PS C:\Users\lmc $workbook.quit()
Method invocation failed because [System.__ComObject] doesn't contain a
method named 'quit'.
At line:1 char:15
+ $workbook.quit <<<< ()
+ CategoryInfo : InvalidOperation: (quit:String) ,
+ FullyQualifiedErrorId : MethodNotFound