帮助修改 MS Word 宏以使用字符串而不是替换对话框
我发现这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您至少可以做的就是
替换为
和
with
在第一个示例中 ,其中
"string to find"
是您要替换的内容,"replacement"
是您想要替换的文本。这将删除对话框并明确设置您想要查找/替换的内容。真有这么简单吗?if I understand you correctly, the minimum you can do is to replace
with
and
with
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?