• Get rid of (renaming) built-in styles (Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Get rid of (renaming) built-in styles (Word 2000)

    Author
    Topic
    #363289

    At times it is a nuisance that built-in styles (like Normal or heading 1) can’t be renamed in Word.

    Say you want to copy parts of one document into another with different style definitions, but want to keep the formatting. This is nearly impossible if both documents use built-in styles, because you can not rename the styles in one of the documents. So a paragraph formatted in Normal style will acquire the formatting of Normal in the document you paste into, and there is not much you can do about it (short of using “paste special” as a Word document object).

    Another case where I found this a nuisance is when I take Word documents into a DTP program. Say the chapter headings in the DTP program are supposed to have style H1. Then I have to rename heading 1 in the DTP program, which often has to be done manually.
    A macro solution in Word would be preferable.

    The following macro renames all styles in the document by appending a * to the style name (heading 1 is changed to heading 1* …).
    Since heading 1* isn’t recognized as a built-in style anymore, I can then rename it to anything I want (for example to H1).

    The macro works by saving the file in RTF format, and changing all style names in the header of the RTF file.

    Hope somebody else will find this useful, too.
    The usual disclaimer: Use the macro at your own risk wink

    cheersKlaus

    Sub ChangeStyleNames()
    ' The macro appends a * to all style names
    ' It thus changes built-in styles to ordinary styles
      Dim myRange As Range
      Dim MsgText
      Dim myFileName
      
      MsgText = "Cancel if you have not saved the file"
      If MsgBox(MsgText, vbExclamation + vbOKCancel, "Danger") = vbCancel Then
        End
      End If
      
      myFileName = ActiveDocument.Name
      If InStr(1, myFileName, ".") > 0 Then
        myFileName = Left$(myFileName, InStr(1, myFileName, ".")) & "RTF"
      Else
        myFileName = myFileName & ".RTF"
      End If
      ActiveDocument.SaveAs _
        FileName:=myFileName, _
        FileFormat:=wdFormatRTF
      ActiveDocument.Close
      Documents.Open _
        FileName:=myFileName, _
        ConfirmConversions:=False, _
        Format:=wdOpenFormatText
      Set myRange = ActiveDocument.Content
      myRange.Find.Execute _
        FindText:="{stylesheet*}}", _
        MatchWildcards:=True
      myRange.Find.Execute _
        FindText:=";}", _
        ReplaceWith:="*^&", _
        MatchWildcards:=True, _
        Replace:=wdReplaceAll
      ActiveDocument.Save
      ActiveDocument.Close
      Documents.Open _
        FileName:=myFileName
    End Sub
    Viewing 1 reply thread
    Author
    Replies
    • #554104

      Attached is your macro as a zipped .bas file. When I tried pasting from the board listing, all of the carriage returns were stripped out. Figured I would save someone else the trouble of putting them back in.

      BTW, WOPR includes a utility to duplicate a style.

      • #554380

        For future reference, someone here on the lounge once commented that if you want to copy code from the lounge to the VBE, you should just paste it into an ordinary Word doc first, then copy it out again and into the VBE.

        Works perfectly!

    • #554156

      Nice routine. Thanks for posting this Klaus.

      I’ve been postponing the task of copying some styles (created by a user) into a template. Before I could let her know NOT to use the built-in H1, etc., she finished. Now it’s my problem.

      So, I look forward to trying this out… cool

    Viewing 1 reply thread
    Reply To: Get rid of (renaming) built-in styles (Word 2000)

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

    Your information: