View Single Post
  #8   Report Post  
Old March 27th 19, 03:47 PM posted to microsoft.public.excel.programming
GS[_6_] GS[_6_] is offline
external usenet poster
First recorded activity by ExcelBanter: Apr 2015
Posts: 1,182
Default executing VBA macro in Excel from OLE

So Lynn, are you done with C# or just spreading your wings?

An alternative would be to have a module named (say) "m_OpenClose" in your xlsm
that has:

Sub Auto_Open()
'Startup code here executes when the workbook is opened
Call testmacro
End Sub 'Auto_Open

Sub Auto_Close()
'Cleanup code here
End Sub

In your C++ app:
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 $workbook.RunAutoMacros 1

This enables your xlsm to do all the work just by opening it; - runs your
macro, saves itself, shuts down Excel.

Note: If you put the code in the Workbook_Open event in the ThisWorkbook
component you can skip the 3rd line of code in C++ app.

Optionally, your "testmacro" can save and quit as well so all you need to do is
run it.


Free usenet access at
Classic VB Users Regroup!