Consulting

Results 1 to 7 of 7

Thread: Solved: Macro to change all styles to a specific language

  1. #1

    Solved: 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

    [vba]
    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
    [/vba]

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,669
    Location
    Try:
    [vba]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[/vba]
    Cheers
    Paul Edstein
    [MS MVP - Word]

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

    Here at least your code needs error handling:

    [VBA]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
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,669
    Location
    Oops. I'd probably handle errors a little differently though:
    [VBA]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[/VBA]
    Cheers
    Paul Edstein
    [MS MVP - Word]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,882
    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
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,669
    Location
    Fair enough.
    Cheers
    Paul Edstein
    [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
  •