PDA

View Full Version : Where to put Document_Open()



WallIT
01-13-2015, 11:31 AM
Hi,

I want to check any document that gets opened for a specific document property value (the value in the Company field). I need this to happen when any/all documents are opened, either from File > Open, or double-clicking or email attachments, etc. I am using a global template which is specified in the Word Startup folder.

The code I am using was lifted from this example, just to see if it works http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_27305417.html I will modify the code to check for the document property once I have this simple code working.

I think my problem is where to put the code so it fires for all documents that get opened. I have attempted to put the above command in various places, such as 'ThisDocument' and Class Modules\ThisApplication but it is not working when the global is loaded and I attempt to open other documents.

Putting the code in 'ThisDocument' does work if I then open the same document, so I know the code is good, but my placement is off.

Help/advice appreciated

Thanks.

SamT
01-13-2015, 10:15 PM
You want this code to run NO MATTER WHAT DOCUMENT IS OPENED?

Well, one of our Word experts will be along soon and he will take care of you.

gmayor
01-14-2015, 12:34 AM
If you want the code to fire when Word is opened by clicking a link, then because of the way Word handles add-in templates, you will need the code in the normal template.

You can use either option in the link.

If you are happy to open documents from within Word File > Open, then you can use a global add-in template.

WallIT
01-14-2015, 02:45 AM
Thanks Graham. You mention I can put the code in the global add-in if I'm happy to use the File > Open. Can you confirm where in the global template this code should be to work like that?

You also mention that in order to get the code to fire for other ways documents are opened (such as clicking a link) then I need to put the code in the normal.dot - If I put the code in a Word addin instead, would it fire for all docs no matter how they are opened?

gmayor
01-14-2015, 02:59 AM
If I put the code in a Word addin instead, would it fire for all docs no matter how they are opened?No. It will only do that if you put the code in the normal template.
If you put it in a global add-in template it will only work when the document is opened from Word's File > Open option ... as I said in my previous reply.

SamT
01-14-2015, 08:24 AM
Graham,

In Excel I can get an event to fire in one workbook when a different book is opened. The code has to be in a class module. The Excel Event is the WorkBookOpen event. (Not the frequently used Sub Workbook_Open())

Sub App_WorkbookOpen(ByVal Wb As Workbook)
Doesn't Word have something like that?

gmayor
01-14-2015, 08:49 AM
The problem is the order in which associated files are opened when you start Word by clicking a filename from an attachment or in Windows Explorer. The document is opened before the add-in with the macro becomes available and so it doesn't fire. If you open the file from within Word then the sequence is changed and so the add-in macro does then work.

I would be happy to be contradicted, but I am pretty sure that this is the case.

You can test it with a macro that pops up a message.

SamT
01-14-2015, 10:52 AM
No, that makes sense, the VBA Project Explorer shows the files in the order opened and Personal.xls is second even when I use the menu to start Excel.

SO, if my understanding is complete http://www.vbaexpress.com/forum/images/smilies/082.gif It can be done, but, but, but, Mike would have to run Word before opening any Document files. That choice is his.