Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing vba code when the Word document is opening

Status
Not open for further replies.

MrBaRRon

Programmer
Aug 8, 2003
39
IT
Hi everybody,

I would like that my code is executed when the Word document is load, when we open it.

Where must I put the code ? What must I do ?

Thank U
 
Go into the VBE window, and click on ThisDocument on the left. Then where you see (General) click the button down and select Document. On the left of this select Open. Place any code in here and it will run when the doc opens.
 
And if you want the code to run when ANY document opens (not just a specific document), make an AutoOpen Sub in a module - not the ThisDocument module.

Gerry
 
Hi Molby ( and fumei),
in the previous before last post Molby said:"Go into the VBE window, and click on ThisDocument on the left. Then where you see (General) click the button down and select Document. On the left of this select Open. Place any code in here and it will run when the doc opens.
"
I am not very good at this but running VB 6.3, no matter what VBE window and view I tried can I find your reference to "ThisDocument" and clicking on "General" does not allow me to choose anything either.
Could you be perhaps be more explicit in your instructions to help me out on this?

thanks
 
Are you in word with an open document ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This is the VBA forum. If you are doing all this as VB, maybe you should address it there.

However, I believe it can be cleared up fairly easily.

1. Please clarify if you are do this from VB, as it seems you are.

2. If you are, please detail exactly what you are doing.

If it is VB, then I assume you are making an instance of Word, and loading a document. Are you saying there is code in that document you want to run? If you are running VB code, and loading an existing Word document, your code can run before the load, but if the code is not actually in te document, it will not fire with the load.

The ThisDocument module is visible in the VBE, the editor that is in VBA (and specifically when viewing VBA for Word).

If you are not in the VBA editor, in Word, you are not going to see ThisDocument.

As previously posted, Word VBA code will fire on document load from either the ThisDocument_Open (in the ThisDocument module), or in one of the reserved Auto macros (AutoOpen, AutoNew, AutoExec, which can be writen into any code module).

Please give a bit more detail as to what you are trying to do, and how.

Gerry
 
Thanks folks,
First off, yes I am in Word with an open doc. This doc. has a number of macros.
When I either go Tools/vbeditor or go Alt/F11, either way I get myself in the editor.
Can change to the various views, eg. View/Code, Object, Object Browser or Project Explorer etc.etc.
However nothing I have tried got me to see what you earlier described.
Last but not least, correct me if I am wrong VBA forum is for VB for M$ Aps right, like Word, Access etc.? VB would be generic VB, but don't both use the same VB editor user interface?
Granted, even though I don't get much practice in VB/VBA I am doing alot of lurking and adding some functionality to a Word form I am creating.
Still struggling with that dynamically filled drop-down code., which I am applying to several drop-downs one is loaded from Word doc and an other from Access table. Thought the code would be essentially same but apparently not.
This saga will continue.......
 
The VBA editor GUI is similar to VB's, but the interface is a bit different, and the projects viewed are different. If you load the VBA editor from Excel, you will see different project structure, thasn the ones you see if you load it from Word.

In any case, you are in Word, and you got to the VBE.

Press Ctrl-R to get the Project Explorer
Find your document. It will be listed as Project(document
name)

If not visible, click the plus arrow beside the project, to see Microsoft Word Objects. Expand it to see ThisDocument, which is a code module applicable to, well, this document.

Ricght click ThisDocument, and select View Code.

At the top of the Code Window, there are two drop down lists. Select the one of the left and change it to Document (rather than General). From the right hand side dropdown, select Open. This will make an empty sub for the Document_Open event.

Put your code in there, and it will fire on document opem.

Gerry
 
Thanks Gerry,
Believe due to the large number of modules I had accumulated (could not see the forest for the trees) was not easy to find, have now cleaned up significantly and yes it is where you said it is.

Thanks again for the basics that some of us selftaught ones have never bothered with.
 
Being self taught is no excuse. In fact, it is even more of a reason for getting the basics. Please bother with it. It makes things much easier as you move along. I am self taught, and yes, I agree that we tend to jump past some things. However, it is really does help to have feet on the ground while you wander through things like the.....gulp, Word Object Model.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top