none
Search and Replace Macro RRS feed

  • Question

  •  

    Can anyone please tell me what is wrong with the macro below (it is the first time when I am using a macro)?

    What I need to do is to search for elements given in HTMLChars array and replace them with elements from SpecialChars array. The two arrays have an unknown length; it will be updated from time to time. To simplify I only used two elements.

    Thank you

    #############################################

     

    Sub SearchReplace()
    '
    ' SearchReplace Macro
    ' SearchReplace
    '
    Dim SpecialChars() As String = New String() {"ö","ü"}
    Dim HTMLChars() As String = New String() {"ö","ü"}

    For Each item As String In HTMLChars
      Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = HTMLChars
            .Replacement.Text = SpecialChars
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next
    End Sub


    • Edited by SearchReplace Tuesday, September 27, 2011 10:02 AM
    • Moved by Liliane Teng Friday, September 30, 2011 10:50 AM more appropriate forum (From:Visual Basic General)
    Tuesday, September 27, 2011 9:57 AM

Answers

  • Hi SearchReplace,

    Sorry for the delay.

    Below code works fine on my side. Please have a try.

    Sub SearchReplace()
    
    Dim SpecialChars(2) As String
    SpecialChars(0) = "ö"
    SpecialChars(1) = "ü"
    
    Dim HTMLChars(2) As String
    HTMLChars(0) = "ö"
    HTMLChars(1) = "ü"
    
    Dim item As Integer
    item = 0
    
    For item = 0 To 1
      Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = HTMLChars(item)
            .Replacement.Text = SpecialChars(item)
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next
    
    End Sub

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Edited by Liliane Teng Monday, October 3, 2011 10:59 AM
    • Marked as answer by Liliane Teng Thursday, October 6, 2011 9:08 AM
    Monday, October 3, 2011 10:41 AM

All replies

  • Hi SearchReplace,

    Sorry for the delay.

    Below code works fine on my side. Please have a try.

    Sub SearchReplace()
    
    Dim SpecialChars(2) As String
    SpecialChars(0) = "ö"
    SpecialChars(1) = "ü"
    
    Dim HTMLChars(2) As String
    HTMLChars(0) = "ö"
    HTMLChars(1) = "ü"
    
    Dim item As Integer
    item = 0
    
    For item = 0 To 1
      Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = HTMLChars(item)
            .Replacement.Text = SpecialChars(item)
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next
    
    End Sub

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Edited by Liliane Teng Monday, October 3, 2011 10:59 AM
    • Marked as answer by Liliane Teng Thursday, October 6, 2011 9:08 AM
    Monday, October 3, 2011 10:41 AM
  • Hi SearchReplace,

    I have updated above reply. Please have a try and let us know the situation on your side. If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 11:01 AM