【问题标题】:Cant modify the text in the content control because of the placeholder text由于占位符文本,无法修改内容控件中的文本
【发布时间】: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 中的范围是否为空还是有更好的方法?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    我对您的问题的理解是,您想要替换或添加内容控件的文本,具体取决于控件中的现有文本。

    要检查内容控件是否仍包含其占位符文本,只需将控件 Range.Text 与其 PlaceholderText 属性进行比较。为此,您的 AddText 例程需要使用实际的内容控件,而不仅仅是其范围。

    Sub RunExample()
      AddTextToContentControl ActiveDocument, "Test Content", "Hello World"
    End Sub
    
    Sub AddTextToContentControl(WorkDoc As Document, CCTitle As String, TextToAdd As String)
      Dim ctrl As ContentControl
      Set ctrl = GetContentControlByTitle(WorkDoc, CCTitle)
      If Not ctrl Is Nothing Then
        If ctrl.Range.Text = ctrl.PlaceholderText Then
          'replace the placeholder text
          ctrl.Range.Text = TextToAdd
        Else
          'add to the existing text
          ctrl.Range.Text = ctrl.Range.Text & " " & TextToAdd
        End If
      End If
    End Sub
    
    Function GetContentControlByTitle(SearchDoc As Document, CCTitle As String) As ContentControl
      Dim ctrl As ContentControl
      For Each ctrl In SearchDoc.ContentControls
        If ctrl.Title = CCTitle Then
          Set GetContentControlByTitle = ctrl
          Exit For
        End If
      Next
    End Function
    

    【讨论】:

      【解决方案2】:

      因为您正在折叠范围,宏会尝试将您的文本附加到 CC 中的默认文本中,这是不可能的。只需取出这一行:

      SlaveRange.Collapse Direction:=wdCollapseEnd
      

      然后它按预期运行并替换默认值。

      【讨论】:

      • 我希望我的addText 子例程将数据添加到内容控件范围的末尾,而不是替换它。
      • 但在您的问题中,您建议在添加文本之前先检查 CC 是否为空。为什么要将文本附加到默认占位符文本?
      【解决方案3】:

      我建议使用 .ShowingPlaceholderText,例如:

      ' This assumes there is at least one CC titled "TestContent"
      With ActiveDocument.SelectContentControlsByTitle("TestContent")(1)
        If .ShowingPlaceholderText Then
          .Range.Text = "Hello World"
        Else
          .Range.InsertAfter "Hello World"
        End If
      End With
      

      否则,如果 CC 当前包含与占位符文本相同的文本,则会出现设计问题。您是否希望将与占位符文本相同但为真实文本的文本替换为实际占位符文本,还是要附加“Hello World”?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2014-06-13
        • 1970-01-01
        相关资源
        最近更新 更多