Results 1 to 7 of 7

Thread: Macro to change all styles to a specific language

  1. #1

    Macro to change all styles to a specific language

    I'm having some difficulty with the VBA language. What I want to do is to select all Styles in a template and set them all to Canadian English. At the same time I know there might be some errors because some Styles do not have languages at all. Here is my current code

     
    Sub ChangeStyle()
        Set tempDoc = ActiveDocument.AttachedTemplate.OpenAsDocument
        Set stylish = tempDoc.Styles
        For Each stylish In tempDoc
            stylish.LanguageID = wdEnglishCanadian
        Next
        tempDoc.Close SaveChanges:=wdSaveChanges
        ActiveDocument.UpdateStyles
    End Sub
    I'm having a rather difficult time wrapping my head around Collections and using For Each with it. If someone could help me out, that would be fantastic. I don't mind if someone gives me the answer right away, but I would also like to learn
    Last edited by Aussiebear; 01-26-2025 at 11:39 AM.

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Try:
    Sub ChangeStyle()
        Dim oDoc As Document, oSty As Style
        Set oDoc = ActiveDocument.AttachedTemplate.OpenAsDocument
        With oDoc
            For Each oSty In .Styles
                oSty.LanguageID = wdEnglishCanadian
            Next
            .Close SaveChanges:=wdSaveChanges
        End With
        ActiveDocument.UpdateStyles
    End Sub
    Last edited by Aussiebear; 01-26-2025 at 11:40 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Paul,

    Here at least your code needs error handling:

    Sub ChangeStyle()
        Dim oDoc As Document, oSty As Style
        Set oDoc = ActiveDocument.AttachedTemplate.OpenAsDocument
        With oDoc
            For Each oSty In .Styles
                On Error GoTo Err_Handler
                oSty.LanguageID = wdEnglishUS
                Err_ReEntry:
            Next
            .Close SaveChanges:=wdSaveChanges
        End With
        ActiveDocument.UpdateStyles
        Exit Sub
        Err_Handler:
        Resume Err_ReEntry
    End Sub
    Last edited by Aussiebear; 01-26-2025 at 11:41 AM.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Oops. I'd probably handle errors a little differently though:
    Sub ChangeStyle()
        Dim oDoc As Document, oSty As Style
        Set oDoc = ActiveDocument.AttachedTemplate.OpenAsDocument
        With oDoc
            For Each oSty In .Styles
                On Error Resume Next
                oSty.LanguageID = wdEnglishCanadian
                On Error GoTo 0
            Next
            .Close SaveChanges:=wdSaveChanges
        End With
        ActiveDocument.UpdateStyles
    End Sub
    Last edited by Aussiebear; 01-26-2025 at 11:42 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Paul,

    I did it the way I did so I could step through and see which actually passed. In practice I would use the Resume Next as well.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Fair enough.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    This is an excellent solution. Thank you so much for the help! I shall mark the thread as solved.

Posting Permissions

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