Word- VBA-为什么会出现运行时错误451“属性让过程未定义且属性获取过程未返回对象”?

Mohamad Bachrouche

尽管给了我运行时错误451“属性未定义过程,并且属性获取过程未返回对象”,但以下代码仍可成功运行。

调试器标记With oCC.Range.Style("Placeholder Text").Font为罪魁祸首。

我需要将一种样式应用于占位符文本,以便在输入或删除值时不会重置该样式。

什么东西少了?

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.Style("Placeholder Text").Font
                                    
                   .Name = "Arial"
                   .Size = 8
                   .ColorIndex = wdRed
                   .Italic = True
            End With
        End If
    End With
Mohamad Bachrouche

所以我想通了。

使用With ActiveDocument.Styles("Placeholder Text").Font以下设置整个文档的占位符文本框架,并防止重置。

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

然后,以我With oCC.Range.Font为例,对于文档中每个后续的占位符文本,我可以分别使用如下所示修改其格式

    Set ocell = oNewRow.Cells(2)
    Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, ocell.Range)
    With oCC
        .DefaultTextStyle = "Style2"
        .Tag = "test2"
        .SetPlaceholderText , , "test2"
        If oCC.ShowingPlaceholderText Then
            **With oCC.Range.Font**
                .Name = "Arial"
                .Size = 12
                .ColorIndex = wdBlack
                .Italic = True
            End With
        End If
    End With

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章