title: Calling a function within ThisDocument in Microsoft Word (VB.Net/VBA)
body: <p>I have a VB.NET program that adds code to the ThisDocument module in MS Word in the following way:  </p>
  
  <pre><code>wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
  </code></pre>
  
  <p>Which creates the following sub:</p>
  
  <pre><code>Public Sub Example_Click()
  Msgbox "Working" 
  End Sub
  </code></pre>
  
  <p>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. </p>
  
  <p>I have been able to call global macros successfully in the Add-In by adding the following code to the button's click sub,.</p>
  
  <pre><code>Globals.TestWordApp.Application.Run("Macro1")
  </code></pre>
  
  <p>All attempts to run code contained within ThisDocument have failed however.</p>
  
  <p>Any help would be greatly appreciated.</p>