2

我有一个变量(strLastname),用于将字符串发送到书签。效果很好。我还希望使用该变量来替换长文档中的临时文本“Name>”。

这就是我现在所拥有的。

  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 但没有人工作。

4

1 回答 1

3

从谷歌快速搜索发现这个链接 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

这将所有出现的 Text 替换为 Tax .... 这就是您所追求的吗?

也适用于变量

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 以解决案例问题(现在在上面修改)

第三次修改导致这个

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
于 2013-09-20T14:21:16.527 回答