Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Changing Template w/ macro (Word 2007)

    I have an old WordBasic macro that I use to change a document quickly by substituting one template or another. I tried taking this to Word 2007 and it tells me that the template can't be found. Then it runs through and makes some changes to the text.

    Here is the code it uses to change templates:

    Rem ******Attach the correct template and update the styles********

    WordBasic.StartOfDocument
    Rem If ActiveDocument.AttachedTemplate
    If WordBasic.[Files$](Template$) <> "" Then
    ActiveDocument.AttachedTemplate = "_SetSMC11X.dot"
    With ActiveDocument
    ActiveDocument.CopyStylesFromTemplate Template:=Template$
    ActiveDocument.UpdateStyles
    WordBasic.ViewFootnotes
    ActiveDocument.CopyStylesFromTemplate Template:=Template$
    ActiveDocument.UpdateStyles
    WordBasic.ViewFootnotes
    End With

    Else
    WordBasic.MsgBox "Cannot locate " + Template$ + " on this system. Please ensure it resides in the same directory as _SetSMC11.dot", "Journal Template Not Found"

    End If

    I've tried substituting this, but I get the same error:

    Rem ******Attach the correct template and update the styles********

    If WordBasic.[Files$](Template$) <> "" Then
    With ActiveDocument
    .UpdateStylesOnOpen = True
    .Documents.Add Template:="_SetSMC11X.dot"
    .XMLSchemaReferences.AutomaticValidation = True
    .XMLSchemaReferences.AllowSaveAsXMLWithoutValidati on = False
    End With

    ------

    I could plug the pathway in explicitly, but I need it to be portable from one machine to another. Any ideas?

    Thanks in advance for any help.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Changing Template w/ macro (Word 2007)

    Is the template stored in the user templates path? If so, does it help if you use

    ActiveDocument.AttachedTemplate = Options.DefaultFilePath(wdUserTemplatesPath) & "_SetSMC11X.dot"

    Note the backslash before the filename. If the template is stored in the workgroup template path, use wdWorkgroupTemplatesPath instead of wdUserTemplatesPath.

  3. #3
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Changing Template w/ macro (Word 2007)

    Yes, its stored in Word's Template folder.

    I tried that line to call the template and still got the error message and the file I'm testing it on doesn't take on the characteristics of the other template. Its the one under the "Else" line.

    Here is the code as it stands now:

    -----

    Rem ************************************************** ***************
    Rem * Name: ConvertToBookproof *
    Rem * Purpose: Change Article From Editing Format To Journal Format *
    Rem * Date: 24/Apr/98 *
    Rem ************************************************** ***************

    WordBasic.ViewNormal
    If WordBasic.ViewFootnotes() = -1 Then WordBasic.ViewFootnotes
    WordBasic.StartOfDocument

    Rem Set Hardcoded Variables
    TMarg$ = "72"
    BMarg$ = "72"
    LMarg$ = "72"
    RMarg$ = "72"
    HeaderSize$ = "72"
    FooterSize$ = "72"
    Rem Template$ = WordBasic.[DefaultDir$](2) + "_SetSMC11X.dot"
    Rem Documents.Add Template:="_SetSMC11.dot"
    Template$ = "_SetSMC11X.dot"

    Rem Add Unit Text to Hardcoded Variables
    TMarg$ = TMarg$ + " pt"
    BMarg$ = BMarg$ + " pt"
    LMarg$ = LMarg$ + " pt"
    RMarg$ = RMarg$ + " pt"
    HeaderSize$ = HeaderSize$ + " pt"
    FooterSize$ = FooterSize$ + " pt"

    ChangeMargins:
    Rem ******Change the Margin size to fit the book*******
    WordBasic.FilePageSetup TopMargin:=TMarg$, BottomMargin:=BMarg$, LeftMargin:=LMarg$, RightMargin:=RMarg$, Gutter:="0 pt", ApplyPropsTo:=4, HeaderDistance:=HeaderSize$, FooterDistance:=FooterSize$, OddAndEvenPages:=1, DifferentFirstPage:=1

    ChangeChar:
    Rem ******Change Bold and Underline in the text and footnotes******
    ChangeChar
    WordBasic.ViewFootnotes
    If WordBasic.SelInfo(25) = -1 Then
    ChangeChar
    WordBasic.ViewFootnotes
    End If

    Template_:
    Rem ******Attach the correct template and update the styles********

    WordBasic.StartOfDocument
    Rem If ActiveDocument.AttachedTemplate
    If WordBasic.[Files$](Template$) <> "" Then
    With ActiveDocument
    .UpdateStylesOnOpen = True
    ActiveDocument.AttachedTemplate = Options.DefaultFilePath(wdUserTemplatesPath) & "_SetSMC11X.dot"
    Rem .AttachedTemplate = "_SetSMC11.dot"
    .XMLSchemaReferences.AutomaticValidation = True
    .XMLSchemaReferences.AllowSaveAsXMLWithoutValidati on = False
    End With

    Rem ActiveDocument.AttachedTemplate = "_SetSMC11X.dot"
    Rem With ActiveDocument
    Rem ActiveDocument.CopyStylesFromTemplate Template:=Template$
    Rem ActiveDocument.UpdateStyles
    Rem WordBasic.ViewFootnotes
    Rem ActiveDocument.CopyStylesFromTemplate Template:=Template$
    Rem ActiveDocument.UpdateStyles
    Rem WordBasic.ViewFootnotes
    Rem End With

    Else
    WordBasic.MsgBox "Cannot locate " + Template$ + " on this system. Please ensure it resides in the same directory as _SetSMC11X.dot", "Journal Template Not Found"
    End If

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Changing Template w/ macro (Word 2007)

    The error might be caused by the line

    If WordBasic.[Files$](Template$) <> "" Then

    What happens if you change it to

    If Dir(Options.DefaultFilePath(wdUserTemplatesPath) & "_SetSMC11X.dot") <> "" Then

  5. #5
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Changing Template w/ macro (Word 2007)

    Unfortunately, the same thing. I think the error might be in this line and it isn't recognizing it.

    Template$ = "_SetSMC11X.dot"


    Basically, what I need it to do is to take a file, change the template, substitute the styles from the one to the other and then update. Its intended to take a file and make it doublespaced, editable copy, that can then be translated back to be printed.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Changing Template w/ macro (Word 2007)

    The line

    Template$ = "_SetSMC11X.dot"

    assigns a value to a string variable - nothing more, nothing less. Do you get an error message on that line? If so, what does it say?

  7. #7
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Changing Template w/ macro (Word 2007)

    I'm getting the error message built-in to the macro: "Cannot locate " + Template$ + " on this system. Please ensure it resides in the same directory as _SetSMC11X.dot"

    I've checked to make sure both templates are in the same folder. I have the folder that they are in listed under Workgroup templates. That should be ok, I think.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Changing Template w/ macro (Word 2007)

    Did you change

    If WordBasic.[Files$](Template$) <> "" Then

    to

    If Dir(Options.DefaultFilePath(wdUserTemplatesPath) & "_SetSMC11X.dot") <> "" Then

    If so, insert the following line above the line If Dir(...

    MsgBox Options.DefaultFilePath(wdUserTemplatesPath) & "_SetSMC11X.dot"

    When you run the code, you should see a message box. Does it display the correct path and filename?

  9. #9
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Changing Template w/ macro (Word 2007)

    Duh! I should have known that. I've been looking at this too long.

    It was a pathway issue. I changed the path for User templates and it works!

    Thanks much, Hans!

Posting Permissions

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