Macro to replace Style of text with certain font
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Macro to replace Style of text with certain font

  1. #1
    Join Date
    Dec 2011
    Posts
    3

    Macro to replace Style of text with certain font

    Hello all and thank you for this amazing forum.
    I am not a code guru, but please bare with me for a moment cause I really need your help.

    I am trying to create a macro for word to help me replace text of certain font and size with certain Style.
    Practically, I want to replace text of "Tahoma" "19" to "Heading 1" in... more than 600 documents.
    I recorded a macro on word, but it doesn't work. The recorded macro is this:

    Code:
    Sub WordMacro()
        ActiveWindow.DocumentMapPercentWidth = 15
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 1")
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    After searching, reading and trying, I came up with this version:

    Code:
    Sub MyMacro()
    With Selection.Find
        .Text = ""
        .Font.Name = "Tahoma"
        .Font.Size = "19"
        .Wrap = wdFindStop
    End With
    With Selection.Find.Replacement
        .Text = ""
        .Style = ActiveDocument.Styles("Heading 1")
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    Yes, I know it's wrong and you can laugh at it (or me), but I would really appreciate your help if you could please tell me how to correct this. Thank you so much in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,951

    Re: Macro to replace Style of text with certain font

    I would use VB6 to find the 600 docs first, and then load them one by one.
    Macros work with one doc at a time, usually.

    AutoRum macro's can be added, as can a custom DOT file, but not sure if it can do what you want.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2011
    Posts
    3

    Re: Macro to replace Style of text with certain font

    Quote Originally Posted by dglienna View Post
    I would use VB6 to find the 600 docs first, and then load them one by one.
    Macros work with one doc at a time, usually.

    AutoRum macro's can be added, as can a custom DOT file, but not sure if it can do what you want.
    This is a great idea, thank you very much. I didn't know I can do that...
    I'll ask for help... but, in case I won't be able to find someone to help me, can you please help me with the macro, and I can do the documents one by one?

  4. #4
    Join Date
    Dec 2011
    Posts
    3

    Re: Macro to replace Style of text with certain font

    I did it, and now it works!
    My mistake was that since the whole document is in Tahoma, I should focus just on the font size.

    Code:
    Sub Members()
    With Selection.Find
        .Text = ""
        .Font.Size = "19"
        .Wrap = wdFindStop
    End With
    With Selection.Find.Replacement
        .Text = ""
        .Style = ActiveDocument.Styles("Heading 1")
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    Now I am writing an applescript to run this macro on all documents in a folder.
    Wish me luck guys.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center