0

请有人以编程方式帮助 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
4

1 回答 1

0

如果您只想格式化第 2 页上的内容控件,则应检查Information内容控件的属性Range,例如:

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

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

于 2015-12-02T10:20:18.803 回答