Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Search and replace across Word table columns

    Hi all,
    I'd like to be able to replace the contents of the header row in a Word table using search and replace vs. touching each cell to replace the words (these are large tables and they seem to want to pause and update themselves whenever you touch the contents of a cell).

    Assume a 3 x 2 table with "aa" in cell(1,1) and "bb" in cell(1,2), I selected and copied these two cells and used advanced find to find the copied string and it fails which makes me worry that this isn't possible... However, copying the contents, deleting the contents and pasting does restore them so Word can at least sort that part out.

    If I select the top row of the test table and use ?asc(mid(selection,X,1)) to sequence through the chars in the immediate window I get:
    97 97 13 7 98 98 13 7 13 7

    Strangely searching for "aa" & chr$(13) & chr$(7) doesn't find anything but "aa" & chr$(7) does (?).

    Searching for "aa" & chr$(7) & "bb" doesn't work (nor does "aa" & chr$(13) & chr$(7) & "bb").

    Anybody know if/how you can search for the contents of two contiguous cells?

    Thanks,
    Eric

    Example find code:
    Code:
    Sub test()
    Dim olTableRng As Range
    
    Set olTableRng = selection.Tables(1).Range
    
    With olTableRng.Find
        .Text = "aa" & Chr$(7)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
    
    If olTableRng.Find.Found Then
        DoEvents
    End If
    
    Set olTableRng = Nothing
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    The most efficient way to process large tables, or many tables, is to switch off their AutoFit behaviour, with .AllowAutoFit = False, or .AutoFitBehavior(wdAutoFitFixed).

    To conduct a search spannig two cells, you would need to use code like:
    Code:
    Sub Demo()
    'Application.ScreenUpdating = False
    Dim StrFnd As String
    StrFnd = InputBox("What is the text to Find, split with |")
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = Split(StrFnd, "|")(0)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .Information(wdWithInTable) Then
          .MoveEnd wdCharacter, Len(Split(StrFnd, "|")(1)) + 1
          If .Characters.Last.Information(wdWithInTable) Then
            If InStr(.Text, Split(StrFnd, "|")(1)) > 0 Then MsgBox "!"
          End If
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    You would still have to replace each cell's contents individually, though.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Paul,
    Unfortunate that replace doesn't work. I'm doing something similar now (thought not quite as robust as what you showed) so I'll update it.

    I'd did find that, strictly speaking, you don't have to individually update the cells, this works if you select the cells:

    Code:
    Set clip_bd = New DataObject
    
    clip_bd.Clear
    clip_bd.SetText "ccc" & chr$(9) & "ddd"
    clip_bd.PutInClipboard
    
    selection.paste
    but it probably isn't worth it if the autofit change speeds things up enough.

    Thanks,
    Eric

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    Quote Originally Posted by RunDeep View Post
    Unfortunate that replace doesn't work.
    Of course not - there's no replacement code in the macro. It was only a demo of how to FIND text spanning mutliple cells. As I said:
    You would still have to replace each cell's contents individually, though
    Given that this doesn't get around the problem of updating individual cells, you'd be back at square one regarding the updating delays. As for using selections, I expect that'll make things even worse - you'll then have the selection overheads as well!
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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