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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word automation StartUp folder 1

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

I have a template in the startUp folder of word. This means that everytime I open a word document or I open a new word document I should be able to run some code of the template in the startUp folder.

Is this right?

If yes how do I do it?

Thanks
 
when I open a new word document, some VBA code in the template (placed in the startup folder) should automatically run.

I read about AutoOpen() and AutoExec() procedures. The problem is that this procedures run only if open the template itself, and not everytime I open a word document.

 
The only way to make code in a global template run directly on document open or document creation is to use application events. Depending on what other templates you use, you might be able to put some code in an AutoOpen macro in your document template that then called code in the global template, but it's more convoluted and less reliable.

If you do go for Application Events you will need to initialise them in an AutoExec macro in the global template (which will run when the global template is loaded).

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
I understand right?
If I add an AutoExec to my template. The code inside the AutoExec will be run by every time I open a existing or a new word document.
 
No, no, no!

Code in an AutoExec will be run once, when the template is loaded (at Word startup in your scenario). In that code you must put code to initialise (and run) application event code, something like this ...

Create a Class Module. Call it, say AppEventClass, and code this in it:

Code:
Option Explicit[blue]

Dim WithEvents tjApp As Word.Application

Private Sub Class_Initialize()
    Set tjApp = Application
End Sub

Private Sub tjApp_NewDocument(ByVal Doc As Document)

[green]    ' This code will run whenever a New Document is created
    ' Do whatever you want to do here[/green]

End Sub

Private Sub tjApp_DocumentOpen(ByVal Doc As Document)

[green]    ' This code will run whenever a Document is opened
    ' Do whatever you want to do here[/green]
    
End Sub[/blue]

Now create a new Module, and in this, code this:

Code:
Option Explicit[blue]

Dim tjEventClass As AppEventClass

Sub AutoExec()

    Set tjEventClass = New AppEventClass

    [green]' Code whatever else you want to do on initial load, here[/green]
End Sub[/blue]



Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Or you could use Normal.dot Document_New. Although I am certainly no fan of using Normal.dot for anything.

Note also that IF you do have something in Document_New (in Normal), AND something in Tony's class module code:
Code:
Sub tjApp_NewDocument(ByVal Doc As Document)
BOTH will execute. Normal first, then the class module in the other global.

Further, if you have:

Document_New (in Normal)
tjApp_NewDocument (in the global)
Document_New in some other template - .DOT file - you are using via File > New

then......

Document_New in Normal does NOT execute, as it is superceded by Document_New in the template. This is...ummmm, normal.

Document_New in the template executes.
tjApp_NewDocument also executes.

In that order.

Out of curiosity, what do you need to execute every time you create ANY new document?

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Are you going to answer my question?

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
ehm. Sorry, yes I will answer:

actually I need to run code only on Open and not on New. I need to check if there is a CustomDocumentProperty and Compare its value to a value contained in a AccessDB.

 
Thanks for answering. And the result is....what? If there is NOT the CustomDocumentProperty, you do...what? If the value is X compared to Y in the DB, you do...what?


OK, whatever, but on every single document ever opened? It seems a high overhead, but then I do not know your actual situation.

Thanks again for answering though.



"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
I know it sounds strange but believe me: there are situations where it is necessary.

 
Oh that I believe, which is actually my point. Some situations, but you are talking about ALL documents that are opened.

Still, on second thought, it is actually not that much overhead. At least from the Word side of things - checking for the existence of a specific CustomDocumentProperty, and if it exists, what is its value. The part I wonder about is the overhead of making a connection to the Access DB for every single Word file that is ever opened.

Will you keep that instance to Access going as a persistent one? Make a new one each time? If the latter, then, hmmmm, that could possibly get hairy.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Well, the Access connection is made only if it is usefull, meaning only if the CustomDocumentProperty exists and it has a value..
 
Yikes. Well, I suppose it will only make a possible difference if the users open files a lot.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top