帮助修改 MS Word 宏以使用字符串而不是替换对话框

发布于 2024-11-08 11:06:53 字数 3129 浏览 0 评论 0原文

我发现这个 Word 宏可以替换任何地方的文本(页眉、页脚、正文、文本框),这正是我所需要的,但唯一的问题是它使用替换对话框。我需要的是使用宏中的字符串进行替换。但尽管我尽力了,我还是无法让它发挥作用。

有人愿意帮忙吗? 这是原始宏(来自 MVP),我需要对其进行修改以使用字符串而不是替换对话框(输入框):

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

这是我使用字符串替换文本而不是对话框的意思的示例(我想一次替换多个内容 - StrOld 被 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

谢谢感谢您的帮助! -内森

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-11-15 11:06:53
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 
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 
忆沫 2024-11-15 11:06:53

如果我理解正确的话,您至少可以做的就是

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

替换为

pFindTxt = "string to find"

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

with

pReplaceTxt = "replacement"

在第一个示例中 ,其中 "string to find" 是您要替换的内容,"replacement" 是您想要替换的文本。这将删除对话框并明确设置您想要查找/替换的内容。真有这么简单吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文