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

    Trouble updating macro to Word 2016

    I have a macro that takes regular Word copy and changes it to an edit copy with double spacing, underline for italics, bold for cap and small cap, etc. This is done using a second template with all those attributes and the template is attached to the document to update the file with the new styles. It works fine in Word 2010/2013, but it can't find the default template folder in Word 2016 and fails to attach the template.

    Here is the code used to do that:


    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 ***** Add Template *****
    Rem Template$ = WordBasic.[DefaultDir$](2) + "\_SetJAL16X.dotm"
    Template$ = "\_SetJAL16X.dotm"


    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********
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove


    If Dir(Options.DefaultFilePath(wdUserTemplatesPath) & "\_SetJAL16X.dotm") <> "" Then
    Rem If Dir(Options.DefaultFilePath(wdWorkgroupTemplatesPa th) & "\_SetJAL16X.dotm") <> "" Then
    With ActiveDocument
    .UpdateStylesOnOpen = True
    .AttachedTemplate = "\_setJAL16X.dotm"
    .XMLSchemaReferences.AutomaticValidation = True
    .XMLSchemaReferences.AllowSaveAsXMLWithoutValidati on = False
    End With


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

    With ActiveDocument
    .UpdateStylesOnOpen = True
    End With




    There's also a corresponding macro that changes it back, but it uses the same language and switches the template back to the original.

    Obviously I'm using some old code to do this. Any help would be appreciated.
    Last edited by Yuwipi Woman; 2016-07-21 at 10:03.

  2. #2
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I'm pretty sure its a pathway issue. The macro can't find the place where the template resides. In this case, it should be the template folder. I could hard key it, but I have more than one person who's going to be using it. Here is where I think the issue lies:


    Rem Template$ = WordBasic.[DefaultDir$](2) + "\_SetJAL16X.dotm"
    Template$ = "\_SetJAL16X.dotm"

    or here:

    If Dir(Options.DefaultFilePath(wdUserTemplatesPath) & "\_SetJAL16X.dotm") <> "" Then
    Rem If Dir(Options.DefaultFilePath(wdWorkgroupTemplatesPa th) & "\_SetJAL16X.dotm") <> "" Then
    With ActiveDocument
    .UpdateStylesOnOpen = True
    .AttachedTemplate = "\_setJAL16X.dotm"
    .XMLSchemaReferences.AutomaticValidation = True
    .XMLSchemaReferences.AllowSaveAsXMLWithoutValidati on = False
    End With

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You are testing Options.DefaultFilePath(wdUserTemplatesPath) contains the template but then don't use that folder to assign the template. It doesn't really matter what path you tell Word the template is in as long as you can attach the template. But I think Word does need the full path spelled out in order to attach the template.

    Once the template is attached, other users can open the file and enjoy the benefits of the attached template as long as Word can find a template of the same name in one of 4 different folders:
    - UserTemplates
    - WorkgroupTemplates
    - Same folder as the document
    - The actual path specified in the .AttachedTemplate setting.

    So ultimately, it doesn't matter too much if they don't have the same folder path as long as they have that template name in any one of those other folders.
    Last edited by Andrew Lockton; 2016-07-24 at 21:17. Reason: Thanks to Charles pointing out my error
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Yuwipi Woman (2016-07-25)

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Charles, yes you are correct. I'll go back and edit that post to correct my clumsiness.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Andrew Lockton View Post
    You are testing Options.DefaultFilePath(wdUserTemplatesPath) contains the template but then don't use that folder to assign the template. It doesn't really matter what path you tell Word the template is in as long as you can attach the template. But I think Word does need the full path spelled out in order to attach the template.

    Thanks for the replies.

    That line is remmed out because it doesn't work any more in the newest version of Word. Not sure what should go in its place.

  7. #6
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    It has been a very long time since I used WordBasic and had forgotten that some of it at least is still included even in the latest Word versions. How long that might last is impossible to say. It would be better to re-write the macro to use VBA commands throughout. It might be better still if the document was created originally from a template that had the required parameters as templates are not intended to be added to documents. That would eliminate the need for much of the macro.

    While it is not clear from your code what ChangeChar does to your footnotes, the following should be closer to what you require:
    Code:
    Option Explicit
    Sub ProcessDocument()
    Dim oFootnote As Footnote
    Dim strTemplate As String
    Dim oFSO As Object
        strTemplate = Options.DefaultFilePath(wdUserTemplatesPath) & "\_SetJAL16X.dotm"
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        If Not oFSO.FileExists(strTemplate) Then
            MsgBox "The template" & vbCr & strTemplate & vbCr & "is not available"
            GoTo lbl_Exit
        End If
        With ActiveDocument.PageSetup
            .TopMargin = 72
            .BottomMargin = 72
            .LeftMargin = 72
            .RightMargin = 72
            .HeaderDistance = 72
            .FooterDistance = 72
        End With
        For Each oFootnote In ActiveDocument.Footnotes
            'do something with the footnote
        Next oFootnote
        With ActiveDocument
            .UpdateStylesOnOpen = True
            .AttachedTemplate = strTemplate
        End With
    lbl_Exit:
        Set oFSO = Nothing
        Set oFootnote = Nothing
        Exit Sub
    End Sub
    Graham Mayor - Word MVP
    http://www.gmayor.com

  8. The Following User Says Thank You to Graham Mayor For This Useful Post:

    Yuwipi Woman (2016-07-26)

  9. #7
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you, that's very helpful.

    Yes, getting rid of the old Word basic code is pretty much what I've been working on.

    I agree that there might be a better approach than the one that I'm taking. This macro basically turns a formatted printable file to an edit-copy version and back again by running a macro that switches out templates. I'm not certain what that approach would be best. I thought about discarding the two templates and just going with the printable version template and do all of the switching back and forth with a macro.

Posting Permissions

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