vba - MS WORD VBA 使用变量查找替换文本

标签 vba ms-word

我有一个变量 (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/

相关文章:

vba - VB代码: Import userform data to excel sheet

excel - 问题制作日历

vba - 如何使用宏关闭跟踪更改?

excel - 书签特殊粘贴,Excel 在同一文件夹中查找

vba - 在一百个工作簿中应用宏,然后将结果复制到主 Excel 文件中

vba - 在 Outlook (VBA) 中 append 主题标题

php - 如何在 Linux 服务器上抓取 MS Word 文档文本?

vba - 如何为word文档中的单个单词设置样式

vba - “Runtime error 5692”是什么意思?

ms-word - 将自定义功能区组添加到 Word 2007 中现有的自定义功能区组