PDA

View Full Version : AutoExec Module and Syntax Stuff



JohnnyBravo
07-18-2006, 12:47 AM
I picked up a book on the basics of VBA. One of the examples the author gives is opening a document from the 'Recent Files' list. It is as follows:

("Absolute Beginner's Guide to VBA" - Paul McFedries. Pg. 113. Que Books. 1994)

Sub Main()
With RecentFiles(1)
Documents.Open .Path & "\" & .Name
End With
End Sub
The author states if you want it to run automatically when Word starts, save the routine in a new module called AutoExec. This part I can intuitively understand. It's something that's going to automatically execute Word is opened. OK But I noticed a few pecularities.

One is that when I changed the macro name to something else say "OpenRecent_Files" for example, the macro does not get automatically triggered. Obviously the word Main itself has special meaning to VBA. Is it like naming a macro "AutoRun"? And are there other similar "trigger" words like it?

#2) Why is there a space in between .Open .Path???

#3) I don't understand why he has .Path and .Name. I thought that to get the property .Fullname provides you with both the document path & name.

So I changed his routine to:


Sub OpenRecent_Document()
With RecentFiles(1)
'Documents.Open .Path & "\" & .Name
Documents.Open .FullName
End With
End Sub
And of course as a young grasshoppa - I was proven wrong! So what gives?

fumei
07-18-2006, 08:35 AM
Johnny, Johnny, Johnny.

First off...good on you for going for it.

Second...I DO hope you have IntelliSense turned on. If you do,Sub OpenRecent_Document()
With RecentFiles(1)
'Documents.Open .Path & "\" & .Name
MsgBox .FullName
.
End With
End Subwhat happens when you type in that dot - below "Msgbox FullName" - which I will get to??

You get (if you have IntelliSense on) a drop down list of all properties and method of the object. In this case, the object is RecentFiles(1).

Guess what? There IS no FullName property!!!! The RecentFiles object does not have a FullName property. So I am guessing With RecentFiles(1)
'Documents.Open .Path & "\" & .Name
Documents.Open .FullName
End With returned that "Method or data member not found" error? Hmmmmm. Yes?

The RecentFiles object has a Path, and it has a Name. So if you are going to use it, bingo...you gotta use both.

FullName is a property of the ActiveDocument object.

Next, the space between .Open and .Path. Again, look at existing code.

Documents.Open always has a space between it and the document path/name to be opened. Technically it is:

Documents.Open Filename:=path/name

Notice the space.

.Open.Path would be stating that .Path is a property of .Open. It is not. It is an identifier, or more accurately, it is a parameter that is passed to the method .Open.

.Something.SomethingElse explicitly states there is a child relationship between Something and SomethingElse.

.Path (or fully RecentFiles(1).Path) IS in a child relationship with RecentFiles(1). With RecentFiles(1)
Documents.Open .Path & "\" & .Name
End With is simply a shortened way of writingDocuments.Open Filename:=RecentFiles(1).Path & _
"\" & RecentFiles(1).Name
One is that when I changed the macro name to something else say "OpenRecent_Files" for example, the macro does not get automatically triggered. Obviously the word Main itself has special meaning to VBA. Is it like naming a macro "AutoRun"? And are there other similar "trigger" words like it?You can have a procedure OpenRecent_Files...but it is not going to run automatically. The answer is YES, there are trigger procedures. These run automatically when Word starts. There are a few.

I never use AutoExec, or AutoNew, or AutoOpen anymore. These are holdovers from WordBasic, as is naming procedures "Main".

AutoExec still has some uses though. AutoExec fires when you start Word - as you know. However, be aware that you can have AutoExec in a global template as well. AutoExec in normal.dot fires first.

BTW: do not EVER put an instruction in an AutoExec that makes another instance of Word. Think about it. THAT new instance will also fire AutoExec....which will make anew instance that fires AutoExec...which makes a new instance which fires AutoExec...which makes a new instance which fires AutoExec...until the computer runs out of memory.

One use of AutoExec is to set up another procedure to run at a specific time.Sub AutoExec()
Application.OnTime When:="10:00", _
Name:="Normal.Sales.Create_Report"
End SubSay you started Word at 8 in the morning (8:00). As long as the computer is still on (and of course Word is still open - it does NOT have to be the active application), at 10:00 Word will fire the procedure Create_Report in the Module Sales.

Cool huh?

Now...you have to be careful with the automatic procedures (AutoExec, AutoNew, AutoOpen, AutoClose, AutoExit) and the document events (Document_New, Document_Open, and Document_Close). There is a very specific hierarchy to firing.

The Document events in normal.dot ONLY fire if normal.dot is the attached template. This can be very significant.

Also it should be noted that Document events in a document and events in the attached template do NOT interfer with each other.

Document_Open in the template, and Document_Open in the document will BOTH fire.

AutoOpen in the template and AutoOpen in the document DO interfer with each other. The document procedure fires, the template procedure will not.

Are we having fun yet?

JohnnyBravo
07-18-2006, 09:14 AM
Hey much thanks for that detailed explanation. It does make sense. However that last part about document events and template made my head spin! :wot

mdmackillop
07-19-2006, 12:10 AM
Great stuff, Gerry

MOS MASTER
07-19-2006, 12:03 PM
Great stuff, Gerry

Agreed!
Had fun reading it! :thumb

geekgirlau
07-20-2006, 01:34 AM
Gerry - wow :clap:

fumei
07-20-2006, 03:38 AM
People. I humbly accept your accolades (I am all covered with blushes), but they are rather unwarranted.

JohnnyBravo
07-20-2006, 01:02 PM
Ok there's a lot of information there. To take one piece, let me ask you about the sales module example you gave.

Sub AutoExec()
Application.OnTime When:="2:55", _
Name:="Normal.Test1.Gerrys_Example"

End Sub
Sub Gerrys_Example()
Documents.Open FileName:="""Lurchy's algebra hints.doc""", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Selection.TypeText Text:="this is a test "
End Sub

In the normal template, I inserted a new module called Test1, but the macro did not fire at 2:55 pm. So I tried military time, 14:55, and it still did not work. What did I miss?

JohnnyBravo
07-24-2006, 11:19 AM
Maybe Gerry missed my question above. Do you any of you experts out there have an answer?

fumei
07-24-2006, 05:43 PM
Are you sure thatFileName:="""Lurchy's algebra hints.doc""", will properly find the file?

fumei
07-24-2006, 05:49 PM
The reason I ask is thatSub AutoExec()
Application.OnTime When:=Now + TimeValue("00:00:30"), _
Name:="Normal.Test1.Test"
End Sub

Sub Test()
Documents.Open FileName:="C:\Test\testdoc0.doc"
End Subindeed, WILL open the file testdoc0.doc after 30 seconds of the Word application opening.

So I suspect that your path/filename is not functioning. Does the Sub Gerrys_Example run by itself?

fumei
07-25-2006, 12:06 PM
Johnny?