Word Open event

D

Doekoe

I want to have some code to run as Word (client opens Word) opens.
However I can not find the right Event. I thought it will do so at
Document_New(), but this event is happening when you hit the New Icon.

What event is happening as soon as Word opens?

Thanks,

Dirk
 
J

Jonathan West

Doekoe said:
I want to have some code to run as Word (client opens Word) opens.
However I can not find the right Event. I thought it will do so at
Document_New(), but this event is happening when you hit the New Icon.

What event is happening as soon as Word opens?

Thanks,

Dirk

There is no such event. However, if you have an add-in, if you name a macro
"AutoExec" it will automatically run when the add-in is first loaded by
Word. If the add-in is placed in Word's Startup folder, it is automatically
loaded when Word is started.

Look up Auto Macros in the VBA help for more on this.
 
D

Doekoe

There is no such event. However, if you have an add-in, if you name a macro
"AutoExec" it will automatically run when the add-in is first loaded by
Word. If the add-in is placed in Word's Startup folder, it is automatically
loaded when Word is started.

Look up Auto Macros in the VBA help for more on this.

Thanks for you answer, however can you tell mee what an add-in is and
how to make this?

Dirk
 
N

NNV

Doekoe said:
Thanks for you answer, however can you tell mee what an add-in is and
how to make this?

Dirk
You only use event Document_Open()
Don't need to use add-in.
 
D

Doekoe

You only use event Document_Open()
Don't need to use add-in.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

I know the Document_Open() event, this one I used and programmed,
however this is not run when Word starts. This events executes when
you open a Word Document. The same with the Document_New() event, this
event executes when you hit the new icon. I do not understand why the
Document_New() is not executed when Word starts up, because then Word
shows a new document.

Dirk
 
N

NNV

--
Coding...coding and coding


Doekoe said:
I know the Document_Open() event, this one I used and programmed,
however this is not run when Word starts. This events executes when
you open a Word Document. The same with the Document_New() event, this
event executes when you hit the new icon. I do not understand why the
Document_New() is not executed when Word starts up, because then Word
shows a new document.

Dirk
Hi Doekoe, you must put Document_Open event in ThisDocument, not in Module.
In Microsoft Office Word, events only execute when are put in ThisDocument of
Project.
Please, let try this code in ThisDocument
Sub Document_Open()
MsgBox "Hello"
End Sub

Good luck!!!!
 
S

Shauna Kelly

Hi Dirk

An add-in is created in the same way as a template (File > New; create a new
Template). But do *not* save it in the UserTemplates folder. Instead, either
save it anywhere and load it through Tools > Templates and Add-ins. Or, more
usually, save it in the folder that Word designates as the Startup folder at
Tools > Options > File Locations.

For further information see
What do Templates and Add-ins store?
http://www.word.mvps.org/FAQs/Customization/WhatTemplatesStore.htm

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
S

Shauna Kelly

Hi NNV

Inline

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


Hi Doekoe, you must put Document_Open event in ThisDocument, not in
Module.
In Microsoft Office Word, events only execute when are put in ThisDocument
of
Project.


That is correct. The Document_Open event runs from ThisDocument.

Please, let try this code in ThisDocument
Sub Document_Open()
MsgBox "Hello"
End Sub

Good luck!!!!


The Document_Open event will fire:
- if the code is in a document, then when that document opens

- if the code is in a template, then whenever any document based on that
template is opened

- if the code is in an add-in, then whenever you do File > Open to open
the add-in. That does not happen in ordinary operations, so if there is
Document_Open code in an add-in, it effectively never fires.

If you put code for a Document_Open event in normal.dot, then it will fire
every time you create a new document based on normal.dot. It will not fire
whenever any new document is created, and it will not fire whenever Word
starts (not least because you might start Word without displaying a
document, or might tell Word to start with a new document based on a
specific template).
 
D

Doekoe

Hi NNV

Inline

Hope this helps.

Shauna Kelly. Microsoft MVP.http://www.shaunakelly.com/word











That is correct. The Document_Openeventruns from ThisDocument.



The Document_Openeventwill fire:
- if the code is in a document, then when that document opens

- if the code is in a template, then whenever any document based on that
template is opened

- if the code is in an add-in, then whenever you do File > Open to open
the add-in. That does not happen in ordinary operations, so if there is
Document_Open code in an add-in, it effectively never fires.

If you put code for a Document_Openeventin normal.dot, then it will fire
every time you create a new document based on normal.dot. It will not fire
whenever any new document is created, and it will not fire whenever Word
starts (not least because you might start Word without displaying a
document, or might tell Word to start with a new document based on a
specific template).- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Hi Shauna,

First off all thanks for your reply. I am busy to get things work, but
it will not happen. Here what I have done:

- I created a template and save it as 'dirk.dot' in C:\Documents and
Settings\d.schingenga\Application Data\Microsoft\Word\OPSTARTEN, as
adviced
- If I start MS Word and open the VBA Editor, it only shows Normal en
Project(Document1), so my template is not loaded I guess

The longer I am trying the more confused I get about how things work.
I would appreciate some more help, only thing I want is: every time a
user starts Word, some code is executed.

Dirk
 
S

Shauna Kelly

Doekoe said:
Hi Shauna,

First off all thanks for your reply. I am busy to get things work, but
it will not happen. Here what I have done:

- I created a template and save it as 'dirk.dot' in C:\Documents and
Settings\d.schingenga\Application Data\Microsoft\Word\OPSTARTEN, as
adviced
- If I start MS Word and open the VBA Editor, it only shows Normal en
Project(Document1), so my template is not loaded I guess

The longer I am trying the more confused I get about how things work.
I would appreciate some more help, only thing I want is: every time a
user starts Word, some code is executed.

Dirk


Hi Dirk

OK, let's go back to first principles. Since you said your startup folder
was named OPSTARTEN, I'm assuming you don't have an English language version
of Word, so you may have to translate the menu items.

First, do Tools > Options > Security. Click the Macro Security button.
You'll see the Security dialog. On the Trusted Publishers tab, tick the
button "Trust all installed add-ins and templates" and click OK, OK to get
back to Word.

Now, create a little test like this:

1. File > New. In the task pane, choose "On my computer". You'll see the
Templates dialog box. In the bottom right hand corner, choose to create a
new Template, and then click OK.

2. Word should have created "Template 1" or a name like that.

3. Alt-F11 to open up the VBE. In the project explorer list, click the item
with name of the template (eg "Template 1") and then do Insert > Module.

4. Paste the following into the code area

Public Sub AutoExec()

MsgBox "This message is from " & ThisDocument.Name

End Sub

5. Close the VBE to get back to the template itself.

6. Tools > Options. On the File Locations tab, identify the location of the
Statup folder. Make sure it hasn't been changed somehow.

7. Save the template in your Word startup folder.

8. Exit Word and re-start Word.

9. Expect:
(a) to see the message box
(b) at Tools > Templates and Add-ins you can see your Add-in listed and
ticked.

OK?

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
D

Doekoe

Hi Dirk

OK, let's go back to first principles. Since you said your startup folder
was named OPSTARTEN, I'm assuming you don't have an English language version
of Word, so you may have to translate the menu items.

First, do Tools > Options > Security. Click the Macro Security button.
You'll see the Security dialog. On the Trusted Publishers tab, tick the
button "Trust all installed add-ins and templates" and click OK, OK to get
back to Word.

Now, create a little test like this:

1. File > New. In the task pane, choose "On my computer". You'll see the
Templates dialog box. In the bottom right hand corner, choose to create a
new Template, and then click OK.

2. Word should have created "Template 1" or a name like that.

3. Alt-F11 to open up the VBE. In the project explorer list, click the item
with name of the template (eg "Template 1") and then do Insert > Module.

4. Paste the following into the code area

Public Sub AutoExec()

MsgBox "This message is from " & ThisDocument.Name

End Sub

5. Close the VBE to get back to the template itself.

6. Tools > Options. On the File Locations tab, identify the location of the
Statup folder. Make sure it hasn't been changed somehow.

7. Save the template in your Word startup folder.

8. Exit Word and re-start Word.

9. Expect:
(a) to see the message box
(b) at Tools > Templates and Add-ins you can see your Add-in listed and
ticked.

OK?

Hope this helps.

Shauna Kelly. Microsoft MVP.http://www.shaunakelly.com/word- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Hi Shauna,

Thanks for your time to make the step by step manual. I run through
your steps and saw I already did it right. Only the AutoExec was on a
different location. However at first it also did not work, and now I
found out why. It is because Outlook is started. As soon as I close
Outlook, and starts Word it works fine. I get the message and also my
own code was executed.

Now I am wondering how to get this work past Outlook, because Outlook
is always run at our clients. Two questions left:

1. How do I get it work even if Outlook is running (I use Word as my
text Editor in Outlook, if I schwitch this off, it works fine)

2. If I open Word and go to VBA, my own Template is locked (not able
to change something), but the 'Normal' template is editable. Why is
that?

Regards,

Dirk
 
J

Jonathan West

Hi Shauna,

Thanks for your time to make the step by step manual. I run through
your steps and saw I already did it right. Only the AutoExec was on a
different location. However at first it also did not work, and now I
found out why. It is because Outlook is started. As soon as I close
Outlook, and starts Word it works fine. I get the message and also my
own code was executed.

Now I am wondering how to get this work past Outlook, because Outlook
is always run at our clients. Two questions left:

1. How do I get it work even if Outlook is running (I use Word as my
text Editor in Outlook, if I switch this off, it works fine)

This has been the cause of a lot of problems. The simplest approach is to
have your Autoexec macro set a flag when it has completed its initialisation
work. Every other macro in the add-in should start out by checking the flag.
If it hasn't been set, then run Autoexec before proceeding. AutoExec is not
run on loading if the first use of Word in a session is to edit an email,
but at least it gets run before any other macro gets going.
2. If I open Word and go to VBA, my own Template is locked (not able
to change something), but the 'Normal' template is editable. Why is
that?

In order to edit macros in your add-in, you have to have the file open in a
document editing window.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top