Word- VBA- 为什么在内容控件中输入值后占位符文本发生变化?

Word- VBA- Why Does Placeholder Text Change After I Enter Values Into Content Control?

以下代码成功插入内容控件富文本,其中占位符文本“test1”为 Arial、8 号字体、红色和斜体。但是,一旦您在内容控件中输入文本,占位符的字体、大小和颜色就会变回默认设置。

如何防止占位符文本改回默认设置?

Dim oTable As Table
Dim oCell As Cell
Dim oCC As ContentControl
Dim oNewRow As Row
    Set oTable = ActiveDocument.Tables(1)
    Set oNewRow = oTable.Rows.Add
    Set oCell = oNewRow.Cells(1)
    Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, oCell.Range)
    With oCC
        .DefaultTextStyle = "Style1"
        .Tag = "Test1"
        .Setplaceholdertext , , "test1"
        If oCC.ShowingPlaceholderText Then
            With oCC.Range.Font
                .Name = "Arial"
                .Size = 8
                .ColorIndex = wdRed
                .Italic = True
            End With
        End If
    End With
End Sub

占位符文本的格式由 'Placeholder Text' 样式定义。

如果您希望内容控件的文本在输入文本后具有特定格式,您可以为 DefaultTextStyle 属性.

指定样式
Dim oTable As Table
Dim oCell As Cell
Dim oCC As ContentControl
Dim oNewRow As Row
    Set oTable = ActiveDocument.Tables(1)
    Set oNewRow = oTable.Rows.Add
    Set oCell = oNewRow.Cells(1)
    Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, oCell.Range)
    With oCC
        .DefaultTextStyle = "Style1"
        .Tag = "Test1"
        .Setplaceholdertext , , "test1"
        If oCC.ShowingPlaceholderText Then
            **With ActiveDocument.Styles("Placeholder Text").Font**
                .Name = "Arial"
                .Size = 8
                .ColorIndex = wdRed
                .Italic = True
            End With
        End If
    End With
End Sub