Consulting

Results 1 to 12 of 12

Thread: AutoExec Module and Syntax Stuff

  1. #1

    AutoExec Module and Syntax Stuff

    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)

    [vba]Sub Main()
    With RecentFiles(1)
    Documents.Open .Path & "\" & .Name
    End With
    End Sub[/vba]
    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:


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

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Johnny, Johnny, Johnny.

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

    Second...I DO hope you have IntelliSense turned on. If you do,[vba]Sub OpenRecent_Document()
    With RecentFiles(1)
    'Documents.Open .Path & "\" & .Name
    MsgBox .FullName
    .
    End With
    End Sub[/vba]what 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[vba] With RecentFiles(1)
    'Documents.Open .Path & "\" & .Name
    Documents.Open .FullName
    End With [/vba]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).[vba] With RecentFiles(1)
    Documents.Open .Path & "\" & .Name
    End With [/vba]is simply a shortened way of writing[vba]Documents.Open Filename:=RecentFiles(1).Path & _
    "\" & RecentFiles(1).Name[/vba]
    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.[vba]Sub AutoExec()
    Application.OnTime When:="10:00", _
    Name:="Normal.Sales.Create_Report"
    End Sub[/vba]Say 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?

  3. #3
    Hey much thanks for that detailed explanation. It does make sense. However that last part about document events and template made my head spin!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Great stuff, Gerry
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by mdmackillop
    Great stuff, Gerry
    Agreed!
    Had fun reading it!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Gerry - wow

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    People. I humbly accept your accolades (I am all covered with blushes), but they are rather unwarranted.

  8. #8
    Ok there's a lot of information there. To take one piece, let me ask you about the sales module example you gave.

    [VBA]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[/VBA]

    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?

  9. #9
    Maybe Gerry missed my question above. Do you any of you experts out there have an answer?

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Are you sure that[vba]FileName:="""Lurchy's algebra hints.doc""", [/vba]will properly find the file?

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    The reason I ask is that[vba]Sub 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 Sub[/vba]indeed, 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?

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Johnny?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •