我有一个变量 (strLastname),用于将字符串发送到书签。这很好用。
我还希望使用该变量来替换长文档中的临时文本“名称>”。
这就是我现在所拥有的。
Sub cmdOK_Click()
Dim strLastname As String ' from dialogue box "BoxLastname" field
strLastname = BoxLastname.Value
....
End sub
不起作用的宏:
Sub ClientName()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Name>"
.Replacement.Text = strLastname??????
'Selection.TypeText (strLastname) ????
'How to use the variable from the Dialogue Box - strLastname????
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
我试过了
.
Replacement.Text = strLastname and .Replacement.Text = BoxLastname.Value
但没有人工作。
最佳答案
谷歌的快速搜索找到了这个链接
http://msdn.microsoft.com/en-us/library/office/aa211953(v=office.11).aspx
我在一个简单的文档上尝试了这个来查找和替换文本
With ActiveDocument.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute FindText:="Text", ReplaceWith:="Tax", Replace:=wdReplaceAll
这用税收取代了所有出现的文本......这就是你所追求的吗??
也适用于变量
OldWord = "VAT"
NewWord = "Text"
With ActiveDocument.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute FindText:=OldWord, ReplaceWith:=NewWord, Replace:=wdReplaceAll, Matchcase:=True
End With
添加 , Matchcase:=True 在最后修复案例问题(现在修改上面)
第 3 次修改导致此
Dim strLastname As String ' from dialogue box "BoxLastname" field
strLastname = BoxLastname.Value
OldWord = "Name"
NewWord = strLastName
With ActiveDocument.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute FindText:=OldWord, ReplaceWith:=NewWord, Replace:=wdReplaceAll, Matchcase:=True
End With
关于vba - MS WORD VBA 使用变量查找替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918761/