Problems with AutoNew in Normal.dot

S

Sunny

Hi All,

I am getting some frustrating problems with the AutoNew procedure when
I use it inside Normal.Dot. I am using Word 10 (XP).

I have put the following code into a new module in Normal.dot:

Sub AutoNew()
MsgBox "You are in AutoNew"
End Sub

When I open Word I get a new blank document but no code runs in the
AutoNew procedure. If I then open a New Blank Document the code runs
!!!!!!

Does anyone know why the code does not run when I open Word and the
initial document is displayed? (Surely this IS a new blank document?)

More importantly does anyone know HOW to get this code to run when Word
is opened?

Any help much appreciated......

Cheers,
Sunny
 
G

Greg Maxey

Sunny,

The "why" I don't know.

For the "how," try:

Sub AutoExec()
MsgBox "You are in AutoNew but really AutoExec"
End Sub
 
S

Sunny

Greg,

Thanks for the response - I have tried using AutoExec to get the
required functionality but had 2 problems

1) The code I am actually running must only run in New documents and
AutoExec will run even when opening existing documents.

2) When launching Word the AutoExec will fire before the blank document
is displayed and I need to hook into this document in the code.

I could use AutoExec if I could find a way of testing if a document is
new or not and this would overcome problem 1 but problem 2 would be
much harder to get round.

Cheers,
Sunny
 
J

Jean-Guy Marcil

Sunny was telling us:
Sunny nous racontait que :
Hi All,

I am getting some frustrating problems with the AutoNew procedure when
I use it inside Normal.Dot. I am using Word 10 (XP).

I have put the following code into a new module in Normal.dot:

Sub AutoNew()
MsgBox "You are in AutoNew"
End Sub

When I open Word I get a new blank document but no code runs in the
AutoNew procedure. If I then open a New Blank Document the code runs
!!!!!!

Does anyone know why the code does not run when I open Word and the
initial document is displayed? (Surely this IS a new blank document?)

More importantly does anyone know HOW to get this code to run when
Word is opened?

AutoNew (now deprecated) and Document_New in the ThisDocument class module
do not run when Word is first launched. The first blank document you get
when you launch Word (Document 1) is not so much a New document as it is a
"default" document that you have no say over its creation.

What are you trying to achieve?
Normally, code in Normal.dot is not really recommended. What kind of code do
you need to execute whenever any document based on Normal.dot is created?

Also, note that documents based on other templates will not benefit form
this code in Normal.dot. If you want to have an impact whenever any document
is created, the route is a little more complicated.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Sunny was telling us:
Sunny nous racontait que :
Greg,

Thanks for the response - I have tried using AutoExec to get the
required functionality but had 2 problems

1) The code I am actually running must only run in New documents and
AutoExec will run even when opening existing documents.

2) When launching Word the AutoExec will fire before the blank
document is displayed and I need to hook into this document in the
code.

I could use AutoExec if I could find a way of testing if a document is
new or not and this would overcome problem 1 but problem 2 would be
much harder to get round.

See
http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sunny

Jean-Guy,

I have tried the approach outlined in the link you posted: Here is
what I did:

Created a .dot file in the word startup folder and put the following
VBA code in:

*************************************************************************************
Class1

Public WithEvents oApp As Word.Application

Private Sub Class_Initialize()
MsgBox "initialise"
End Sub

Private Sub oApp_DocumentOpen(ByVal Doc As Document)
MsgBox "Doc Open"
End Sub

Private Sub oApp_NewDocument(ByVal Doc As Document)
MsgBox "New Doc"
End Sub
*************************************************************************************
Module1

Dim oAppClass As New Class1

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
MsgBox oAppClass.oApp.Documents.Count
End Sub

*************************************************************************************
When I launch Word I get the pop-up from the class_initialise event and
a 1 from the document count pop-up but nothing else. If I then open a
new blank document I get the desired "New Doc" message!!!!

It would appear that even if this application level event handling is
used in an add-in that one cannot get a new document event for the
'default' document which is displayed when Word is started?!?

Do you have any other ideas how to do this?

- Background: I need to populate a document variable in the header of
every word document created at my firm without leaving any VBA code in
the document that gets produced. The company templates I already have
a solution for but it does not work for blank documents created from
normal.dot because of this issue with the new document event.

Cheers,
Sunny
 
J

Jean-Guy Marcil

Sunny was telling us:
Sunny nous racontait que :
Jean-Guy,

I have tried the approach outlined in the link you posted: Here is
what I did:

Created a .dot file in the word startup folder and put the following
VBA code in:

*************************************************************************************
Class1

Public WithEvents oApp As Word.Application

Private Sub Class_Initialize()
MsgBox "initialise"
End Sub

Private Sub oApp_DocumentOpen(ByVal Doc As Document)
MsgBox "Doc Open"
End Sub

Private Sub oApp_NewDocument(ByVal Doc As Document)
MsgBox "New Doc"
End Sub
*************************************************************************************
Module1

Dim oAppClass As New Class1

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
MsgBox oAppClass.oApp.Documents.Count
End Sub

*************************************************************************************
When I launch Word I get the pop-up from the class_initialise event
and a 1 from the document count pop-up but nothing else. If I then
open a new blank document I get the desired "New Doc" message!!!!

It would appear that even if this application level event handling is
used in an add-in that one cannot get a new document event for the
'default' document which is displayed when Word is started?!?

Do you have any other ideas how to do this?

- Background: I need to populate a document variable in the header of
every word document created at my firm without leaving any VBA code in
the document that gets produced. The company templates I already have
a solution for but it does not work for blank documents created from
normal.dot because of this issue with the new document event.

OK, I see.

Well, looking at the code you posted, it does not seem to be like the one
that is suggested that you use at:
http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm

Your AutoExec code should look like:

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
oldNoOfOpenDocs = 0
FirstNewDoc = True
End Sub

But you have:

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
MsgBox oAppClass.oApp.Documents.Count
End Sub

???

You are supposed to use a "PsuedoAutoNew" (sic!)...
Read that page attentively again.
(
Under point 2, it is stated:
<quote>
When you start Word, a new blank document is created; the PsuedoAutoNew
macro runs, but neither AutoNew nor the (Word 2000-specific) NewDocument
events are triggered.
<\quote>
)

So, unless I have misunderstood the author's intentions, it should work.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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