• Default template other than normal.dot (Word VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Default template other than normal.dot (Word VBA)

    • This topic has 11 replies, 6 voices, and was last updated 23 years ago.
    Author
    Topic
    #366795

    After reading multiple questions from people who wanted their default template to be something other than normal.dot, and working up code that automatically closed a new document that was based on a global template, I came up with the following. So far as I can tell it works. I’m sure there are drawbacks but so far all I’ve seen are:

    1) The screen flicker as the documents are switched and
    2) The new document numbers will be messed up and will skip numbers

    I have tried generating labels with this in place but haven’t tried a mailmerge.

    The code is placed in the “ThisDocument” object of normal.dot. The advantage is that normal.dot is still out there influencing things and available as a resource. However, you can’t create a new document based on normal.dot. It has to be based on another template.

    In this case, the other template is presumed to be named “normal2.dot.”

    Here’s the code:

    Private Sub Document_New()
    ' Procedure written by Charles Kenyon 12 February 2002
    ' Intended to close new document created from normal template
    ' Offers user choice to open the template
    '
    ' This sub goes in the ThisDocument Object of normal.dot
    '
    ' Declare variables
        Dim sTemplateName As String
        Dim sPath As String
        Dim sTempName As String
    '
        On Error GoTo Oops
    '
    '   Fill in variables
        sPath = Application.Options.DefaultFilePath(wdUserTemplatesPath) & ""
    '
    ' Following is alternative if the template is in the Workgroup Templates folder
    '    sPath = Application.Options.DefaultFilePath(wdWorkgroupTemplatesPath) & ""
    '
    ' Name of the alternative template - crucial!
        sTemplateName = "Normal2.dot"
        sTempName = ActiveDocument.FullName
    '
        Application.Documents.Add Template:=sPath & sTemplateName
        Application.Windows(sTempName).Activate
    '
    ' Close the new document based on normal.dot without saving changes
        ActiveDocument.Close SaveChanges:=False
        Exit Sub
    Oops:
        ' skips closing of new document based on Normal if error in procedure
        ' most likely error is that the alternative template isn't found
    End Sub
    

    Thoughts? I’ve tested with Word 2000 and Word 97, both running under Win 98se.

    Viewing 2 reply threads
    Author
    Replies
    • #570051

      It seems the most common way someone might create a new document based on Normal.dot is to click the new document toolbar button, which runs FileNewDefault. By putting some of this code in FileNewDefault, I suspect you could get around the two documents/new number problem, and the other code would only need to kick in if the user affirmatively chose File, New… blank document. But… I haven’t tested it.

      This is a silly question, but can you just rename the new document with the name of the document you are closing? This might disguise your switcheroo until the next document is created. grin

      • #570052

        If what they want is only to intercept the icon, you are right. A simple macro to create the new document and switch the toolbar icon to go to that macro.

        This procedure will not let the user base a new document on normal.dot. It creates a new document from the alternative template when Word is opened, when normal.dot is selected as the base template for a new doc, and when “blank document” is selected under File => New. It even gets Ctrl-N.

    • #570075

      There’s no way to stop a user from basing a document on the Normal template, unless one makes some very significant changes to lots of Word commands.

      Nor do I see any purpose for precluding the use of the Normal template.

      Word allows for user specified templates. It is then up to the local user police force to educate and force users to use the appropriate templates.

      If one wishes to ENFORCE a ban on the Normal template, I would first try adding an AutoOpen and an AutoClose to the Normal template that does the following:

      1. In AutoOpen, if the Normal template is Attached, then run a macro that does not allow the user to proceed without changing the attached template.

      2. In AutoClose, do the same check, in case the user misbehaves. Repeat offenders should be terminated.

      One could also muck around with AutoNew in the Normal template.

      This is really a people management problem. If a user cannot be trained to, or will not, do the right thing, terminate the user’s employment.

      • #570178

        You forgot . smile

        I didn’t read the original requests, but perhaps management takes the view that it’s an IT problem. People have to remember a lot of things to use software and to do their jobs, and in most cases it is a poor decision to complicate the latter by having them focus on the former. Management might decide to “work around” a wide variety of issues with automation. I can’t say this is irrational; not everyone is a power user. And if not for this, what would keep the Microsoft Office automation developers gainfully employed? wink

        • #570286

          I did not forget that most management is both clueless and aliterate (remember this is not the same as illiterate).

          But if they refuse to have staff that knows what’s going on, or hire qualified consultants, they get what they deserve.

    • #578881

      I have worked with clients in the legal industry who want exactly the same thing. One big reason for this choice is so that they can update company styles, autotext entries, page layout settings, etc. without overwriting Normal.dot. That way the user can own their own Normal.dot and save their own autotext and autocorrect entries, macros etc.

      However, there is an easier way to do what you are doing. This is how I have done it for my clients:

      You create a new template called something like “Blank.dot”. In this template you place any styles and autotext entries that you want to be available to all documents created from this template.

      In a standard module in either Normal.dot or a global template add-in (such as “companyname.dot”), you place the following code:

      Sub NewBlankDocument()
      Documents.Add Template:= _
      Application.Options.DefaultFilePath(wdUserTemplatesPath) _
      & “Blank.dot”, NewTemplate:=False, DocumentType:=0
      End Sub

      By the way, I also do not customize menus or toolsbars in Normal.dot. I usually create a company global template (such as TPW.dot). In that template is where customized toolbars, menus and global macros are stored.

      You replace the New Blank Document icon on the toolbar with a new button linked to the above macro. You assign the keyboard shortcut Ctrl+N to the above macro. You can also, if you wish, add an item to the File menu called “New Blank Document”.

      On all shortcuts used to open Word (desktop icon, start menu items, etc) type the following in the Start in field:

      “C:Program FilesMicrosoft OfficeOffice” /n /t”C:Program FilesMicrosoft OfficeTemplatesBlank.dot” (I believe this is the correct syntax for this option. Word’s help file is not very explanatory on the use of these options).

      Replace “C:” with whatever drive the program files and the template files are stored in. The information after the /t should be in quotation marks and should contain the complete path and filename of the template that you want a new document created from upon opening Word. The /n will prevent the opening of a document based on Normal.dot. The /t part will open a document on the template you want to use and will say “Document 1” in the title bar. No document numbers are skipped.

      Using the above methods, the only way they can use Normal.dot to create a new document now is use the File, New menu to open the new templates dialog box. This, however, can be changed also. You could create a macro with a custom dialog to show only the templates that the user should be using (Normal.dot would not show in the list) and replace the File, New menu choice with this macro.

      Anne
      anne.foxpa@verizon.net

      • #578891

        Thank you for the contribution to the discussion. Like you, I avoid putting any customizations in normal.dot, especially any that I might want to share.

      • #587640

        Hi I am new to this topic but may have something useful to add.
        I recently tried putting a template in the word STARTUP and found that Marcos I had in the template ran.
        I am trying to implement a FileSaveAs replacement to tag files put into records folders with the date and update others according to where they are stored.
        I have got all this working but I don’t know what the ramifications are of putting the template in Word Startup.
        It seems to work, and even protects the template but styles are not available and an auto open macro placed in a the template doesn’t work either.
        But it is a simple way to replace commands without tampering with Normal.dot.
        Do you know if there is a way to automatically run a macro from this template.

        • #587786

          The AutoOpen macro in a global template will only run if you directly open the template for editing. When Word opens a document, it will run an AutoOpen macro in the document itself, or in the document’s attached template, or in Normal.dot; if there is an AutoOpen macro in more than one of those places, Word is supposed to execute the first one it finds in that order.

          What code do you need to run when a document opens? Maybe there is another way to implement it.

          • #588111

            What I am after is a way I can implement an “add in”, say called Save & update under File | Save As
            This will call the other code you just helped me with to update headers and footers with revision details etc before the save, using an API call, the H & F detail also depend on the path.
            I don’t want to use the normal.dot template as this must remain accessable to the user.
            I have all the code working now but only from normal.dot or the attached template.
            I would realy like to add the “add in” from a template placed in Startup.
            That way I can add it to personal profiles and it will be reasonably transparant and protected from the general users.

            Any Ideas

        • #587878

          As Jefferson has already pointed out, you can’t get an AutoOpen in a global template to respond to the opening of a specific document, the way you can with an AutoOpen in the template upon which the specific document is based.

          There is a way to get the equivalent of AutoOpen to run in a global template – to do this you must set up an application-level event handler and have an application-level DocumentOpen event procedure. In this event procedure you can run code that checks for the name of the template which is attached to the document that is opening, and then branch to run appropriate “AutoOpen” code, appropriate to the specific template.

          Gary

    Viewing 2 reply threads
    Reply To: Default template other than normal.dot (Word VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: