开发者

Help Modifying MS Word Macro to use string rather than replace dialogue box

I found this macro for Word that replaces text anywhere (headers, footers, body, text boxes) which is what I need, but the only problem is it uses the replace dialogue box. What I need is to use a string in the macro for the replacement instead. But as much as I try I can't get it to work.

Would someone be willing to help? Here's the original macro (From MVP) that I need to be modified to use a string rather than the replacement dialogue box (input box):

Public Sub FindReplaceAnywhere()
  Dim rngStory As Word.Range
  Dim pFindTxt As String
  Dim pReplaceTxt As String
  Dim lngJunk As Long
  Dim oShp As Shape
  pFindTxt = InputBox("Enter the text that you want to find." _
    , "FIND" )
  If pFindTxt = "" Then
    MsgBox "Cancelled by User"
    Exit Sub
  End If
  TryAgain:
  pReplaceTxt = InputBox( "Enter the replacement." , "REPLACE" )
  If pReplaceTxt = "" Then
    If MsgBox( "Do you just want to delete the found text?", _
     vbYesNoCancel) = vbNo Then
      GoTo TryAgain
    ElseIf vbCancel Then
      MsgBox "Cancelled by User."
      Exit Sub
    End If
  End If
  'Fix the skipped blank Header/Footer problem
  lngJunk = ActiveDocument.Sections( 1 ).Headers( 1 ).Range.StoryType
  'Iterate through all story types in the current document
  For Each rngStory In ActiveDocument.StoryRanges
    'Iterate through all linked stories
    Do
      SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
      On Error Resume Next
      Select Case rngStory.StoryType
      Case 6 , 7 , 8 , 9 , 10 , 11
        If rngStory.ShapeRange.Count > 0 Then
          For Each oShp In rngStory.ShapeRange
            If oShp.TextFrame.HasText Then
              SearchAndReplaceInStory oShp.TextFrame.TextRange, _
                  pFindTxt, pReplaceTxt
            End If
          Next
        End If
      Case Else
        'Do Nothing
      End Select
      On Error GoTo 0
      'Get next linked story (if any)
      Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
  Next
End Sub
Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
    ByVal strSearch As String , ByVal strReplace As String )
  With rngStory.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End Sub

And here is a sample of what I mean by using a string to replace text rather than the dialogue box (I want to replace multiple things at one time - StrOld is replaced by StrNew):

Sub MultiReplaceAtCursor()
Dim StrOld As String, StrNew As String
Dim RngFind As Range, RngTxt As Range, i As Long
StrOld = "°|©|H"
StrNew = "¾|¶|e"
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrOld, "|"))
  Set RngFind = RngTxt.Duplicate
  With RngFind.Find
    .ClearFormatting开发者_如何学运维
    .Replacement.ClearFormatting
    .Text = Split(StrOld, "|")(i)
    .Replacement.Text = Split(StrNew, "|")(i)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
Next
End Sub

Thanks for your help! -Nathan


Public Sub FindReplaceAnywhere()
Dim oldString, newString As String ' The two strings you want
Dim rngStory As Word.Range
Dim pFindTxt As String
Dim pReplaceTxt As String
Dim lngJunk As Long
Dim oShp As Shape
oldString = "Your string to find" ' Replace this value by the string to find
newString = "Your replacement" ' Replace this value string by the replacement you want
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections( 1 ).Headers( 1 ).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
     'Iterate through all linked stories
     Do
       SearchAndReplaceInStory rngStory, oldString, newString
       On Error Resume Next
       Select Case rngStory.StoryType
       Case 6 , 7 , 8 , 9 , 10 , 11
           If rngStory.ShapeRange.Count > 0 Then
               For Each oShp In rngStory.ShapeRange
                   If oShp.TextFrame.HasText Then   
                       SearchAndReplaceInStory oShp.TextFrame.TextRange, _ oldString, newString
                   End If
               Next       
           End If       
       Case Else         'Do Nothing 
       End Select      
       On Error GoTo 0       'Get next linked story (if any)   
       Set rngStory = rngStory.NextStoryRange    
   Loop Until rngStory Is Nothing  
Next
End Sub

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _ ByVal strSearch As String , ByVal strReplace As String )  
With rngStory.Find
     .ClearFormatting
     .Replacement.ClearFormatting
     .Text = strSearch
     .Replacement.Text = strReplace
     .Wrap = wdFindContinue
     .Execute Replace:=wdReplaceAll
   End With
End Sub 


if I understand you correctly, the minimum you can do is to replace

pFindTxt = InputBox("Enter the text that you want to find.", "FIND" )

with

pFindTxt = "string to find"

and

pReplaceTxt = InputBox( "Enter the replacement." , "REPLACE" )

with

pReplaceTxt = "replacement"

in your first example where "string to find" is what you're replacing and "replacement" is the text you wish to replace it with. This will remove the dialog boxes and explicitly set what you want to find/replace. Is it that simple?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜