我发现Word的这个宏可以替换任何地方的文本(页眉,页脚,正文,文本框),这是我需要的,但唯一的问题是它使用了替换对话框。我需要的是在宏中使用字符串代替。但是,尽管我尝试了,但我无法让它发挥作用。
有人愿意帮忙吗? 这是我需要修改为使用字符串而不是替换对话框(输入框)的原始宏(From 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
感谢您的帮助! -Nathan
答案 0 :(得分:1)
如果我理解正确,你可以做的最小就是替换
pFindTxt = InputBox("Enter the text that you want to find.", "FIND" )
与
pFindTxt = "string to find"
和
pReplaceTxt = InputBox( "Enter the replacement." , "REPLACE" )
与
pReplaceTxt = "replacement"
在您的第一个示例中,"string to find"
是您要替换的内容,而"replacement"
是您要替换它的文本。这将删除对话框并明确设置您要查找/替换的内容。这很简单吗?
答案 1 :(得分:1)
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