Word DocumentEvents

Add-in Express™ Support Service
That's what is more important than anything else

Word DocumentEvents
DocumentOpen and NewDocument Event 
Michael Kensy


Guest


Hello,

I have some problems with the two adxWordEvents: NewDocument and DocumentOpen.
Maybe I miss something in the logic of how AddInExpress works or Word in general. Anyway here is my problem:

If Word is already open, the NewDocument Event is fired correctly if I use the WorApp.Documents.Add method and even if I right-click on a Word-File in the Windows Explorer by choosing "New" in the context Menu. (everythings fine so far)

If Word is not open and I right-click on a file and choose "New" the DocumentOpen event is fired instead of the NewDocument event. Thats not what I have expected, because I want to manipulate a Word Document by filling present Bookmarks but only if it is a new Document. So in the case someone chooses the option to rightclick on a file and choos new (or double click on a template file)it is technicaly a new file bit the wrong event is fired.

Anything I am missing here with Word events? How can I get the right NewDocument event even if Word is not started?

Tried it with Word 2010 32Bit on Windows 7 64Bit with Visual Studio 2010.
Posted 13 Apr, 2011 08:48:15 Top
Eugene Astafiev


Guest


Hi Michael,

I have reproduced the issue on my PC with Windows 7 SP1 and Outlook 2010 (both x64). This is not a bug.

Please note that when you rightclick on a file and choose the New context menu item an existing file is opened and the DocumentOpen event is fired in Word. Am I right? Why do you think that the NewDocument event should be fired instead?
Posted 13 Apr, 2011 12:31:49 Top
Michael Kensy


Guest


Hi Eugene,

if I use programmatically the WordApp.Documents.Add method like this:

WordApp.Documents.Add(Template: myTemplateFilePath, NewTemplate: true);


A new instance of this existing document will be opend and the NewDocument event is fired (which is what I want)
Now I thought that this behaviour is as if I choose New in the Context menu. What is confusing me the most is, that if Word is already open, the NewDocument event is fired if I choose New in the context menu, if Word is closed, the DocumentOpen event is fired. In both cases I create a new instance of the document.

Maybe this is how Word is used to work so I will explain why it is important for me:

I have a number of template files. On this Templates there are Bookmarks which are filled by the AddIn if a user opens this document as a new instance of it. So I used the NewDocument event to do this Job. Now imagine, that someone (an admin eg.) wants to modify the template-structure itself so it is unlikely, that the template is filled with custom data. So in that case, the DocumentOpen event will do nothing. But now, a user wants to print a document of this template which should be filled with the custom data. If he chooses right-click -> new (or double click if it is a dotx file) the AddIn will not auto complete the new instance of this document because the DocumentOpen event is fired and not the NewDocument. If I use the Documents.Add method it will.

Hope I could explain what my Issue is. Any ideas how to handle this?

Greetings
Michael
Posted 14 Apr, 2011 03:07:24 Top
Michael Kensy


Guest


Ok I have done some testing and have some interesting results:

I have created a new Document and wrote two simple VBA code lines in it:


Private Sub Document_New()
    MsgBox "new event"
End Sub

Private Sub Document_Open()
    MsgBox "open event"
End Sub


Now I have created an AddInExpress project and simply put a MessageBox into the two adx events for DocumentOpen and NewDocument. After I registered it I did some testing. Here are the results:

Empty Document:
Right-Click -> New:
VBA: NewDocument
AddInExpress: NewDocument

Double-Click on Document (or rightclick->open/edit)
VBA: DocumentOpen
AddInExpress: NewDocument

Document with any content:
Right-Click -> New
VBA: NewDocument
AddInExpress: DocumentOpen

Double-Click on Document (or rightclick->open/edit)
VBA: DocumentOpen
AddInExpress: DocumentOpen


Now VBA is acting as I have expected it but AddIn Express is acting different in two of the four cases. Now my question: Is that intended, maybe some technical background that I am lacking here between AddIns and VBA?
Posted 14 Apr, 2011 03:35:36 Top
Eugene Astafiev


Guest


Hi Michael,

Please note that VBA is not based on a COM add-ins technology. May I ask you to reproduce the issue with a regular VSTO or, better, shared add-in project instead? Do you get the same results?
Posted 14 Apr, 2011 03:51:25 Top
Michael Kensy


Guest


To be honest I have never worked with shared add-in project so I don't know how to access the events here (or where).

What I am asking myself is: Why should there be a difference between how VBA fires these two events and AddIn Express? If you choose the New option in the context menu isn't it logic that the new document is fired because you do not open an existing document but creating a new instance of an existing document which is a new one?

Anyway I would like to test it with shared-addins, if anyone can give me some advises how and where to add code to the two events and how to register and test a shared-addin?
Posted 14 Apr, 2011 04:35:02 Top
Eugene Astafiev


Guest


Hi Michael,

Please find the code in the http://www.add-in-express.com/forum/read.php?FID=5&TID=6845 forum thread. Also I have just sent a sample shared add-in project to you for testing. Please check out your Inbox and then let me know whether it works.
Posted 14 Apr, 2011 06:03:40 Top
Michael Kensy


Guest


Hi Eugene,

I tested your code with a shared AddIn and now I have three different technologies (VBA, Shared Addin, AddInExpress) with 3 different behaviours.

First: If the Word process is active, all the three technologies are fireing the expected events.

If the Word process is not active only VBA fires the (in my opinion) correct events, the Shared Addin fires none of the two events and AddIn Express fires confusing events depending on the content of the document (empty or filled).

What I want is, if Word is not active and someone opens a Document with Doubleclick (no template File) or Rightclick open/edit, the DocumentOpen event should be fired, if New is chosen, the NewDocument event should be fired no matter if the document is empty or filled with content. Maybe this is impossible with COM AddIns because the event is fired before the AddIn is load or something like that.

So is the event-behaviour of AddIn Express as expected (new Document event on empty Document no matter what option is choosen and DocumentOpen with non empty Documents no matter what option is chosen)?
Posted 14 Apr, 2011 08:36:46 Top
Eugene Astafiev


Guest


Hi Michael,

Could you please share your desktop to me? I will take a look at the issue online.
Posted 14 Apr, 2011 08:53:38 Top
Michael Kensy


Guest


Hi Eugene,

was a little bit busy.

Sure I would like to share my desktop, so you can have a look. How do we coordinate it?
Posted 15 Apr, 2011 05:33:44 Top