Group:  Microsoft Word ยป microsoft.public.word.vba.beginners
Thread: Problems with AutoNew in Normal.dot

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Problems with AutoNew in Normal.dot
"Sunny" <sunnybudd[ at ]gmail.com> 14.07.2006 17:18:01
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

Re: Problems with AutoNew in Normal.dot
"Greg Maxey" <gmaxey[ at ]mvps.org> 14.07.2006 17:39:44
Sunny,

The "why" I don't know.

For the "how," try:

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


Sunny wrote:
[Quoted Text]
> 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

Re: Problems with AutoNew in Normal.dot
"Sunny" <sunnybudd[ at ]gmail.com> 14.07.2006 17:56:51
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


Greg Maxey wrote:
[Quoted Text]
> Sunny,
>
> The "why" I don't know.
>
> For the "how," try:
>
> Sub AutoExec()
> MsgBox "You are in AutoNew but really AutoExec"
> End Sub
>
>
> Sunny wrote:
> > 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

Re: Problems with AutoNew in Normal.dot
"Jean-Guy Marcil" <NoSpam[ at ]LeaveMeAlone> 14.07.2006 18:02:45
Sunny was telling us:
Sunny nous racontait que :

[Quoted Text]
> 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
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Re: Problems with AutoNew in Normal.dot
"Jean-Guy Marcil" <NoSpam[ at ]LeaveMeAlone> 14.07.2006 20:10:43
Sunny was telling us:
Sunny nous racontait que :

[Quoted Text]
> 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
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Re: Problems with AutoNew in Normal.dot
"Sunny" <sunnybudd[ at ]gmail.com> 17.07.2006 21:36:05
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


Jean-Guy Marcil wrote:
[Quoted Text]
> 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
> jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org

Re: Problems with AutoNew in Normal.dot
"Jean-Guy Marcil" <NoSpam[ at ]LeaveMeAlone> 20.07.2006 02:13:48
Sunny was telling us:
Sunny nous racontait que :

[Quoted Text]
> 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
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net