LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default executing VBA macro in Excel from OLE

On 27/03/2019 21:30, Lynn McGuire wrote:
On 3/26/2019 1:46 PM, 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.

Thanks,
Lynn


BTW, I am using the C++ code from
Â*Â*Â* http://support.microsoft.com/kb/216686

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.

Â*Â*Â*Â*Â*VARIANT result1;
Â*Â*Â*Â*Â*VariantInit ( & result1);
Â*Â*Â*Â*Â*std::string errorMsg = "Executing Visual Basic Macro, " +
visualBasicMacroName + " (ExecuteVisualBasicMacro)";
Â*Â*Â*Â*Â*WCHAR methodName [1000];
Â*Â*Â*Â*Â*charToWchar (visualBasicMacroName.c_str (), methodName, sizeof
(methodName) / sizeof (WCHAR));
Â*Â*Â*Â*Â*AutoWrap (DISPATCH_METHOD, & result1, pExcelWorkbooks,
methodName, errorMsg, 0);

Thanks,
Lynn


Found and fixed my several problems with getting Excel.Run to working.
The main problem was that the AutoWrap method needed to be L"Run" and
the name of the method needed to be in a VARIANT data structure.Â* Also,
the Run command needed to be executed as a function of the Excel
application itself.

Â*Â*Â*Â*VARIANT result1;
Â*Â*Â*Â*VariantInit ( & result1);
Â*Â*Â*Â*std::string errorMsg = "Executing Visual Basic Macro, " +
visualBasicMacroName + " (ExecuteVisualBasicMacro)";
Â*Â*Â*Â*VARIANT methodName;
Â*Â*Â*Â*VariantInit ( & methodName);
Â*Â*Â*Â*methodName.vt = VT_BSTR;
Â*Â*Â*Â*Â*Â*Â* //Â* UTF-8 to wide
Â*Â*Â*Â*std::wstring wstrMethodName;
Â*Â*Â*Â*UTF8toWide (visualBasicMacroName.c_str (), wstrMethodName);
Â*Â*Â*Â*Â*Â*Â* //Â* the _bstr_t does not work with Watcom C++
Â*Â*Â*Â*Â*Â*Â* //Â* _bstr_t notebookNameBstr = _bstr_t (wstrNotebookName.c_str
());
Â*Â*Â*Â*BSTR methodNameBstr = SysAllocString (wstrMethodName.c_str ());
Â*Â*Â*Â*methodName.bstrVal = methodNameBstr;
Â*Â*Â*Â*OLEMethod (DISPATCH_METHOD, & result1, pExcelApplication, L"Run",
errorMsg, 1, methodName);
Â*Â*Â*Â*if (result1.vt == VT_DISPATCH)
Â*Â*Â*Â*{
Â*Â*Â*Â*}
Â*Â*Â*Â*VariantClear ( & result1);
Â*Â*Â*Â*VariantClear ( & methodName);


Thanks for the update :)

--
Adrian C
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a .BAT file using excel macro Vasanth Excel Discussion (Misc queries) 1 May 19th 12 05:11 PM
Executing a macro from an add-in in Excel 2007 OzJan Excel Programming 0 October 3rd 08 01:28 PM
Executing a VBA macro from within Excel 97 script RickT Excel Programming 1 April 24th 07 11:44 PM
Excel Macro Stop Executing Agustus Excel Programming 0 March 27th 06 05:11 PM
Executing an older excel 4.0 macro pgerrish Excel Programming 0 April 22nd 04 05:41 PM


All times are GMT +1. The time now is 02:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"