3

I am using the Office PIA to integrate an application into Word.

There are multiple word documents which run a macro when you open them. My goal is to lock the current document, but after the macro has run. Is there a way to do this? Thought something like this should work, but there is no such event like "MacroExecuted" or something else:

public void OpenDocument(string path)
{
    var app = new Application();
    app.Documents.Open(path);
    app.ActiveDocument.MacroExecuted += LockDocumentEvent;
    app.Visible = true;
}
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Philipp Eger
  • 2,235
  • 4
  • 23
  • 34
  • I just checked vba-internal and there it looks like `app.Documents.Open(path);` would only return after the associated code was executed. Did you actually have an issue with still running macro when `Open` returned? – grek40 Apr 13 '17 at 09:12

1 Answers1

0

Found it, after long search:

The important method is contained in the Document Object.

app.ActiveDocument.RunAutoMacro(WdAutoMacros.wdAutoOpen);

WdAutoMacros enumeration contains different values which indicates which macro schould be executed. For me it was

WdAutoMacros.wdAutoNew

and

WdAutoMacros.wdAutoOpen

Now the macro is executed after opening the document from Office SDK.

Philipp Eger
  • 2,235
  • 4
  • 23
  • 34