Consulting

Results 1 to 7 of 7

Thread: Find and Replace within Word Tables

  1. #1
    VBAX Regular
    Joined
    May 2015
    Posts
    44
    Location

    Find and Replace within Word Tables

    Hi guys,

    Any ideas why the below find and replace code isn't finding '£' located in tables within a word document?

    The code finds and replaces all of the £ into € that are in the body of the document, but for some reason it doesn't find all of the '£' that are in word tables with the document.

    Private Sub Test()
      If ComboBox14.Text <> "€" Then
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "£"
            .Replacement.Text = "€"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = False
            .CorrectHangulEndings = False
            .HanjaPhoneticHangul = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = False
            .MatchFuzzy = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    Any ideas?

    Thanks,
    AJHEZ

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim t As Table
        
        For Each t In ActiveDocument.Tables
            t.Range.Find.Execute FindText:="aa", ReplaceWith:="bb", Replace:=wdReplaceAll
        Next
    
    End Sub

  3. #3
    VBAX Regular
    Joined
    May 2015
    Posts
    44
    Location
    Thanks, mana.

    Would this also find the values outside the tables?

    Both values inside and outside the tables need to be found and replaced - is it possible to do this in one macro?

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    ???
    >Title : Find and Replace within Word Tables

    Sub test2()
       ActiveDocument.Range.Find.Execute FindText:="aa", ReplaceWith:="bb", Replace:=wdReplaceAll
    End Sub

  5. #5
    VBAX Regular
    Joined
    May 2015
    Posts
    44
    Location
    I think the issue may actually be that it is failing to change £ to € where the text has a number straight after it.

    e.g. it will change £ 1,000 (where there is a space) but it will not change £1,000...

  6. #6
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Minor changes


    Sub Test()
        Selection.WholeStory
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = "£"
                .Replacement.Text = "€"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchByte = False
                .CorrectHangulEndings = False
                .HanjaPhoneticHangul = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchWildcards = False
                .MatchFuzzy = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
            End Sub
    The code Mana posted works perfect as well when you put your characters in.

    Sub test2()
        ActiveDocument.Range.Find.Execute FindText:="£", ReplaceWith:="€", Replace:=wdReplaceAll
    End Sub
    Last edited by Kilroy; 07-25-2017 at 06:24 AM.

  7. #7
    VBAX Regular
    Joined
    May 2015
    Posts
    44
    Location
    Thanks, both - this has been a massive help!

    AJHEZ

Posting Permissions

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