Reputation: 31
I have created a VSTO addin for Word 2016
On a tab in Word I have a number of buttons, but I only want to show some buttons if the name of the document or another property is a certain value.
But I cannot get it to fire when a document is opened.
Am I missing something?
Imports Microsoft.Office.Interop.Word
Public Class ThisAddIn
Dim wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Private Sub Application_DocumentOpen(Doc As Document) Handles Application.DocumentOpen
MsgBox("opened")
If wordApp.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Posting Slip - Client Receipt" Then
PostClientReceipt.Visible = True
End If
End Sub
End Class
Upvotes: 0
Views: 138
Reputation: 49395
First of all, there is no need to get the Word Application
running instance in the following way from your VSTO Word add-in:
Dim wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
Instead, you can use the Application
property of the add-in class. For example:
Globals.ThisAddIn.Application
Second, the DocumentOpen
event is fired before the add-in is loaded when you double click the file. Since Word 2010, the Word startup behavior is changed, VSTO runtime waits for Word to be ready before firing the ThisAddIn_Startup
event. In this scenario by that time the DocumentOpen
and WindowActivate
events are already fired.
You may find the DocumentOpen and WindowActivate events do not fire on Word 2010 thread helpful.
As a possible workaround at startup you may check for any opened document and call the DocumentOpen
event handler programmatically.
Upvotes: 1