Vba 以编程方式替换标记的令牌
我试图用Vba 以编程方式替换标记的令牌,vba,ms-word,Vba,Ms Word,我试图用dictionary(“SomeVar”) 这就是我目前所拥有的 Public Sub ReplaceToken(ByVal range As range, ByVal dictionary As Scripting.dictionary) With range.Find .Text = "[*]" .Wrap = wdFindContinue .Forward = True .Wrap = wdFindContinue .Form
dictionary(“SomeVar”)
这就是我目前所拥有的
Public Sub ReplaceToken(ByVal range As range, ByVal dictionary As Scripting.dictionary)
With range.Find
.Text = "[*]"
.Wrap = wdFindContinue
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
range.Find.Execute
While range.Find.Found
' Nested Range ?????
' Range.Text = dictionary(Range.Text.withoutsquarebrackets) ????
Wend
End Sub
我不确定如何执行实际的替换,因为它似乎意味着对范围
的嵌套调用-如果我ReplaceAll
(我想这样做),会发生什么
很抱歉提出这样的问题,但我现在不怎么做VBA,休,您可以使用
Find.Execute
Replace()
参数:
Public Sub ReplaceTokens(ByVal range As range, ByVal dictionary As Scripting.dictionary)
For Each Key In dictionary.Keys
With range.Find
.Wrap = wdFindContinue
.Text = Key
With .Replacement
.Text = dictionary.Item(Key)
End With
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Public Sub TestReplace()
Dim MyDict As New Scripting.dictionary
MyDict.Add Key:="REPLACE_ME", Item:="Replacement"
MyDict.Add Key:="REPLACE_ME_2", Item:="Replacement2"
Dim rng As range
Set rng = ThisDocument.range(0, 0)
ReplaceTokens rng, MyDict
End Sub
如果可以的话,我希望避免循环浏览字典项,因为我希望很少有匹配项,但是有很多字典项