Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Mar 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AutoOpen Routine (VBA XP)

    I have some documents which are attached to an incorrect template. I need to have an autoopen routine fire when these documents are opened (actually when all documents are opened since I won't know if it is one of these documents are not). The autoopen routine is no biggie, it just parses out the template it is looking for and reattaches it to an appropriate one. The problem is that Word's AutoOpen routine doesn't fire unless it is attached to the document template the document was created from. Since these are existing documents, there isn't a way for me to retroactively have this happen... Does anyone know of a way to work around this?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: AutoOpen Routine (VBA XP)

    Well... adding an AutoOpen macro to your Normal.dot template should work, unless it is preempted by an AutoOpen macro in the document template. At least this is the way it worked in the past; I'm not sure about XP. I'm not sure exactly what the code would be, because when Word can't find a document's template, it might report that the attached template's name is Normal.dot, rather than the original name. (I haven't tested this, but I think that's what you see if you open the Templates and add-ins dialog).

    Would this work? Copy your new template to the location of the old template and give it the old template's name. Unless that path is gone, this would seem to be the easiest method.

  3. #3
    New Lounger
    Join Date
    Mar 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AutoOpen Routine (VBA XP)

    Well, I didn't quite give the whole scoop in the original post. Basically, when our implementation of XP was rolled out, the systems engineer deployed templates into the user's directory (i.e., Documents and Settings/USERID/Application Data/Microsoft/Templates) instead of into a path that exists on all machines. So, what is happening is that UserA will create a document. UserB then opens that document and their copy of the template exists in THEIR D&S directory and, thus, the document cannot find the template since it is looking for UserA's USERID directory.

    I have tested and Word does NOT return normal.dot when it cannot find the template so I can parse out the correct template and then reattach to the template in the correct path. We will not use a network shared directory and, while the long term solution is to move the templates into the All Users directory, I still have these thousands of documents out there that will be attached to templates with paths indicating the UserID.

    We do not use normal.dot for anything and I was hopeful there would be some other way than to create a routine in normal.dot though, if that's my only chance, I guess that's where it goes.

    EDITED: Well, nix that... when I moved the routine into normal.dot, it now returns normal.dot when it doesn't find the template... if you look at Templates and Add-Ins, however, it does reflect the right path until you actually call it with the macro *sigh*

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: AutoOpen Routine (VBA XP)

    SF,

    We've been through something similar at least twice at our firm - once when the network templates were moved from C drive to D drive, and once when we decided to move some of our templates into different subdirectories (so users would see them under different tabs in File>New than previously).

    Below are excerpts from code we used in the latter situation, but you should be able to adapt it for your needs.
    The first thing you need is a global template in the startup directory, so that it will always be loaded when Word launches.
    In this global template you need to create a class module that will house an application-level Document Open event procedure (for instructions on how to set up an application-level event procedure, see the recent star post by carbonnb <!post=here,196505>here<!/post>).

    Here are chunks of code you may be able to adapt (I've left stuff out and changed some names to protect privacy):

    <pre>Public WithEvents App As Word.Application

    Private Sub App_DocumentOpen(ByVal objDoc As Document)
    Dim sOldTemplatePath As String
    Dim sNewTemplatePath As String
    Dim sTemplateName As String
    Dim sBuffer As String
    Dim sTempDirectory As String
    Dim diaTemplates As Dialog

    On Error GoTo ERROR_ReAttachedTemplate

    Set diaTemplates = Dialogs(wdDialogToolsTemplates)
    sOldTemplatePath = diaTemplates.Template
    Set diaTemplates = Nothing

    If bCheckTemplateExists(sOldTemplatePath) = False Then
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    sTemplateName = objFSO.GetFileName(sOldTemplatePath)

    If sTemplateName <> "normal.dot" Then
    sNewTemplatePath = Options.DefaultFilePath(wdWorkgroupTemplatesPath)
    If Right(sNewTemplatePath, 1) <> "" Then
    sNewTemplatePath = sNewTemplatePath & ""
    End If

    sTempDirectory = LCase(objFSO.GetFileName(objFSO.GetParentFolderNam e(sOldTemplatePath)))
    Select Case sTempDirectory
    Case "firm"
    sNewTemplatePath = sNewTemplatePath & "FirmWide" & sTemplateName

    Case "uk"
    Select Case LCase(sTemplateName)
    Case "offeringcircular.dot"
    sNewTemplatePath = sNewTemplatePath & "FirmWide" & "offeringcircular.dot"
    Case Else
    sNewTemplatePath = sNewTemplatePath & "UK" & sTemplateName
    End Select
    End Select
    'Attach the new template path to the document
    If bCheckTemplateExists(sNewTemplatePath) = True Then
    ActiveDocument.AttachedTemplate = sNewTemplatePath
    End If

    Set objFSO = Nothing</pre>

    And the code for bCheckTemplateExists (which can be put in a standard code module in the same global template) is:

    <pre>Public Function bCheckTemplateExists(sTemplatePath As String) As Boolean
    On Error Resume Next
    If Len(Dir(sTemplatePath)) > 0 Then
    bCheckTemplateExists = True
    Else
    bCheckTemplateExists = False
    End If
    End Function
    </pre>

    The first batch of code is carved out of a longer procedure, so I can't guarantee the code will compile as posted, but it should give you some ideas to get started with.

    Hope this helps,
    Gary

  5. #5
    New Lounger
    Join Date
    Mar 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AutoOpen Routine (VBA XP)

    Gary, Thank you... this was the point I needed in the right direction... I didn't even think that if I used the dialogs, it would return the string to me unlike the attachedtemplate property. GREAT!

    The only problem I encountered in doing my code was that this does not work if I put it in a startup template. I have a global startup template that houses all of our global functions and it did not run on document open; however, as soon as I put it into normal.dot (by simply copying the functions over), it runs and works as hoped.

    I'm assuming this must be an XP change -- that these events don't fire from global templates anymore. Does anyone know this for sure?

    Thanks so much.
    Julie

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: AutoOpen Routine (VBA XP)

    Julie,

    Glad that was some help.

    I don't have XP so can't say for sure, but I'd be surprised if app events no longer work in global templates.

    You're best off not putting anything into Normal.dot, so it's worth trying to get it to run in the global template in startup.
    One thing to try is to ensure is that the app-level event class is being initialized - to ensure that happens whenever Word launches, you need to have an AutoExec routine (in a standard code module) in the global template.

    The code could look something like this:

    <pre>Option Explicit
    Public objAppEvents As New clsAppEvents

    Public Sub AutoExec()
    'Create a global instance of objAppEvents
    Set objAppEvents.App = Word.Application
    End Sub
    </pre>


    Gary

  7. #7
    New Lounger
    Join Date
    Mar 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AutoOpen Routine (VBA XP)

    Thanks for all your help. I don't like touching normal.dot and we allow our users to save their custom items there.... I was envisioning having to write code to create a module and stick my code in... Fortunately, I found that another one of our global add-ins was pre-empting my AutoExec routine (this is why people other than myself should never roll out templates but I digress). Once I got that eliminated, my code worked just fine. My only outstanding concern is having this object sitting there in Word's memory space the entire working session. I made sure to axe it on AutoExit and I'm hopeful it's small enough that we won't see any resource problems. Regardless, there isn't any way around fixing the problem so it has to work!!

    I was going to try moving this into a DLL and seeing if it was a smaller overhead but I haven't had the time yet. Perhaps I'll try that next week.

    Do you actually have a job doing this stuff? I'm always interested when I meet people who seem to know their stuff really well in Word/VBA.

    Happy Thanksgiving
    Julie
    julie@kremerfamily.com

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: AutoOpen Routine (VBA XP)

    Julie,

    It would be interesting if there were some way to quantify it, but my guess is that you don't have to worry about the overhead of the application object. I wouldn't bother converting it to a .dll just to reduce overhead, but if you do get a chance to try it and get some measurable benefit, please post back and let us know!

    And yes, I'm fortunate to be doing this for a living - sounds like you are too, no?

    Have a good holiday,
    Gary

  9. #9
    New Lounger
    Join Date
    Mar 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AutoOpen Routine (VBA XP)

    Like the true geek I am, I'll likely try it at some point. I already have 2 custom DLLs with a lot of global functionality in them so I have somewhere to wrap this function up in.

    Yes, I work in this now and have for a number of years now. I work part-time since I've had two small children and am looking at trying to re-enter a full-time career sometime in the near future or distant future whatever works out...

    Julie

  10. #10
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: AutoOpen Routine (VBA XP)

    Hey Julie this is Kyle. I was poking around the lounge today in hopes of trying to learn something about application events and I saw your little post [img]/forums/images/smilies/smile.gif[/img]. I'm curious about the AutoExit that you mentioned in this post. I have three or four application events in my global template now. Are you suggesting that I also have an AutoExit in my global template that sets my applications events (class module) to nothing when Word exits?

Posting Permissions

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