我需要做的是将大文本内容控件添加到大型word文档中的所有“xx”。
我在突出显示多个实例时看到了类似的线程。我基于迄今为止我尝试过的解决方案:
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "target1"
.Replacement.Text = "target1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
现在只是单词测试以查看它用于单独添加内容控件的命令我找到了这个命令:
Selection.Range.ContentControls.Add (wdContentControlRichText)
我想出的是:
Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Range.ContentControls.Add (wdContentControlRichText)
With Selection.Find
.Text = "xx"
.Replacement.Text = "xx"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
使用Selection.Range
替换Selection.Find
只会导致“未找到方法或数据字段”或类似内容。我认为Range
指的是当前突出显示的字符范围,而find
指的是with
阻止的任何内容......好吧,发现。
无论find
是什么,似乎都无法将一些内容控件投射到其上。
答案 0 :(得分:0)
你很亲密:
Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "xx"
'.Replacement.Text = "xx"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
'Selection.Text = "" 'uncomment if you want to remove xx
Selection.Range.ContentControls.Add (wdContentControlRichText)
Loop
End Sub