【问题标题】:Format content control on specific page特定页面上的格式内容控件
【发布时间】:2016-03-06 10:11:30
【问题描述】:

能否请人以编程方式帮助处理 MS Word 的 ContentControl 格式设置。 我编写了一个代码,根据其标签转到特定的 ContentControl。通过转到由其标签指定的每个 ContentControl,此代码运行良好。但是我只需要在第 2 页上格式化 ContentControls。

我尝试将循环限制为仅针对特定书签、页面、表格,但它不起作用。我不需要遍历文档中的每个ContentContorl,它需要太多时间,这与宏的目的相反。我正在创建这个宏来加快报告格式。

这是我的代码:

    Sub EditCCbyTag()

    Dim cc As ContentControl
    Dim strText As String

    Dim oThisdoc As Word.Document
    Dim oCC1 As ContentControl
    Dim oCCs1 As ContentControls
    Dim oCC2 As ContentControl
    Dim oCCs2 As ContentControls
    Dim oCC3 As ContentControl
    Dim oCCs3 As ContentControls

    Set oThisdoc = ActiveDocument
    Set oCCs1 = oThisdoc.SelectContentControlsByTag("DgDocDate01")

    For Each oCC1 In oCCs1
        If oCCs1.Count > 0 Then
        oCC1.Range.Select
        Dialogs(wdDialogContentControlProperties).Show

            strText = InputBox("Please enter the DATE of report")

            Set cc = ActiveDocument.SelectContentControlsByTag("DgDocDate01")(1)
            cc.Range.Text = strText
        End If

     Next oCC1

    ' the next CC DgDnvReportNo01

    Set oThisdoc = ActiveDocument
    Set oCCs2 = oThisdoc.SelectContentControlsByTag("DgDnvReportNo01")

    For Each oCC2 In oCCs2

        If oCCs2.Count > 0 Then

        oCC2.Range.Select
        Dialogs(wdDialogContentControlProperties).Show

            strText = InputBox("Please enter the NUMBER of report")

            Set cc = ActiveDocument.SelectContentControlsByTag("DgDnvReportNo01")(1)
            cc.Range.Text = strText
        End If
    Next oCC2

    ' the next CC DgRevNo01

    Set oThisdoc = ActiveDocument
    Set oCCs3 = oThisdoc.SelectContentControlsByTag("DgRevNo01")

    For Each oCC3 In oCCs3

        If oCCs3.Count > 0 Then

        oCC3.Range.Select
        Dialogs(wdDialogContentControlProperties).Show

            strText = InputBox("Please enter the REVISION of report")

            Set cc = ActiveDocument.SelectContentControlsByTag("DgRevNo01")(1)
            cc.Range.Text = strText

        End If

    Next oCC3

    MsgBox "Done!"

    End Sub

【问题讨论】:

  • " 试图限制循环只针对特定的书签、页面、表格,但它不起作用" 它怎么不起作用?这肯定是最有效的方法,我很确定它可以工作......

标签: vba ms-word word-contentcontrol


【解决方案1】:

如果你只想格式化第2页的内容控件,你应该检查内容控件的RangeInformation属性,例如:

Dim cc as ContentControl
If cc.Range.Information(wdActiveEndPageNumber) = 2 Then
    ... Do work ...
End If

wdActiveEndPageNumber 常量指的是物理页。如果您需要逻辑页(例如,如果 Word 已被指示重新开始页码编号),则应使用 wdActiveEndAdjustedPageNumber 常量。有关来源和详细信息,请参阅this link

【讨论】:

  • 感谢 DanL 的回复!但我想不通,对不起。我试图将它合并到代码中,但 VBA 给了我一条错误消息,指出未定义对象变量或未定义块变量。你能扩展一下代码吗?我要编辑的内容控件仅在第 1 页和表格中。我尝试将表格设置为范围以及所有其他内容,但仍然无法弄清楚。
  • 您应该在问题中包含用于设置表格的代码。然后,您应该指定代码在哪一行失败。这个错误非常不具体——它只是说明你没有定义一些东西。
  • 您好 DanL,已解决。首先选择表,然后通过 selection.information 识别选择页面,然后运行 ​​if then 语句。它成功了。感谢您的帮助!
  • @Opensoul 很高兴听到。如果它解决了您的问题,请标记为答案:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多