【发布时间】:2020-01-02 01:57:41
【问题描述】:
我有一个名为TestContent 的富文本内容控件。示例:
在我的代码中,子 RunExample 初始化了所述内容控件的范围,并使用 AddText 子将示例文本写入范围的末尾:
Option Explicit
Dim TestContentRange As Word.Range
Sub RunExample()
'Initialize the range as the range of Content Control
Set TestContentRange = ActiveDocument.SelectContentControlsByTitle("TestContent")(1).Range
'Write the "Hello World" to the content Control
AddText "Hello World"
End Sub
Sub AddText(TextBit As String)
Dim SlaveRange As Word.Range
Set SlaveRange = TestContentRange
SlaveRange.Collapse Direction:=wdCollapseEnd
SlaveRange.Text = TextBit
End Sub
我收到运行时错误 6124:您不能编辑此选择,因为它受到保护。
据我了解,这是因为当内容控件为空时,占位符文本会妨碍您。并且占位符被禁止直接编辑,因此错误。例如,如果我将一些文本像TestContentRange.Text = "!" 那样放入内容控件中,则代码运行良好。示例:
Option Explicit
Dim TestContentRange As Word.Range
Sub RunExample()
Set TestContentRange = ActiveDocument.SelectContentControlsByTitle("TestContent")(1).Range
TestContentRange.Text = "!"
AddText "Hello World"
End Sub
Sub AddText(TextBit As String)
Dim SlaveRange As Word.Range
Set SlaveRange = TestContentRange
SlaveRange.Collapse Direction:=wdCollapseEnd
SlaveRange.Text = TextBit
End Sub
我的问题是 - 如何避免使用占位符文本?我应该检查AddText sub 中的范围是否为空还是有更好的方法?
【问题讨论】: