Microsoft Word 2013宏 - 将内容控件添加到多个项目

时间:2015-08-05 18:37:07

标签: vba ms-word word-vba

我需要做的是将大文本内容控件添加到大型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是什么,似乎都无法将一些内容控件投射到其上。

1 个答案:

答案 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