Thread: [Solved] Find Replace
View Single Post
 
Old 03-01-2013, 12:49 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Adriano,

You could use a macro like:
Code:
Sub TableFindConvert()
Application.ScreenUpdating = False
Dim StrTxt As String, StrSty As String, Stl As Style, bSty As Boolean
StrTxt = InputBox("What is the Text to Find", "Text Selection")
StrSty = Trim(InputBox("What is the Style to Find", "Style Selection", "Table-1"))
bSty = False
If StrSty <> "" Then
  For Each Stl In ActiveDocument.Styles
    If Stl.NameLocal = StrSty Then
      bSty = True
      Exit For
    End If
  Next
  If bSty = False Then GoTo errExit
End If
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = StrTxt
    If StrSty <> "" Then .Style = StrSty
    .Replacement.Text = ""
    .Format = bSty
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) = True Then
      .Duplicate.Tables(1).ConvertToText Separator:=vbTab
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Exit Sub
errExit:
Beep
Application.ScreenUpdating = True
End Sub
Note that, as coded, you can only search for one string at a time, though you could change the Find parameters to allow wildcard searches, which can be more flexible. Also, with the way I've coded the macro, you could omit the text to find and just have Word find the Style - or you could omit the Style and have Word find just the text; it's up to you.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote