0
votes

I have a VB.NET program that adds code to the ThisDocument module in MS Word in the following way:

wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)

Which creates the following sub:

Public Sub Example_Click()
Msgbox "Working" 
End Sub

I used to call this via the click event of a command button however it would be desirable call it from a ribbon button created as part of a COM Add-In.

I have been able to call global macros successfully in the Add-In by adding the following code to the button's click sub,.

Globals.TestWordApp.Application.Run("Macro1")

All attempts to run code contained within ThisDocument have failed however.

Any help would be greatly appreciated.

1
Subs in ThisDocument can be accessed as COM methods, but since they aren't in the typelib, in a COM environment you wuld typically have to get the dispid for the method via IDispatch and use that to call the method. AFAICS from a VB.NET VSTO addin you can in fact use Globals.ThisAddIn.Application.ActiveDocument.Macro1() to call such a macro, but it may not be the approved way to do it.user1379931

1 Answers

0
votes

Application.Run runs macros.

Macros exist in modules, and ThisDocument is a class module.

Try creating a new module or use an existing module instead of ThisDocument.