在特定页面上格式化内容控件
Format content control on specific page
能否以编程方式帮助处理 MS Word 的 ContentControl 格式设置。
我写了一段代码,根据它的标签转到特定的 ContentControl。通过转到由其标签指定的每个 ContentControl,此代码运行良好。但是我只需要在第 2 页上格式化 ContentControls。
我试图限制循环仅针对特定书签、页面 table,但它不起作用。我不需要遍历文档中的每个 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
如果您只想格式化第 2 页上的内容控件,则应检查内容控件 Range
的 Information
属性,例如:
Dim cc as ContentControl
If cc.Range.Information(wdActiveEndPageNumber) = 2 Then
... Do work ...
End If
wdActiveEndPageNumber
常量指的是物理页。如果您需要逻辑页(例如,如果指示 Word 重新开始页码),您应该使用 wdActiveEndAdjustedPageNumber
常量。有关来源和详细信息,请参阅 this link。
能否以编程方式帮助处理 MS Word 的 ContentControl 格式设置。 我写了一段代码,根据它的标签转到特定的 ContentControl。通过转到由其标签指定的每个 ContentControl,此代码运行良好。但是我只需要在第 2 页上格式化 ContentControls。
我试图限制循环仅针对特定书签、页面 table,但它不起作用。我不需要遍历文档中的每个 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
如果您只想格式化第 2 页上的内容控件,则应检查内容控件 Range
的 Information
属性,例如:
Dim cc as ContentControl
If cc.Range.Information(wdActiveEndPageNumber) = 2 Then
... Do work ...
End If
wdActiveEndPageNumber
常量指的是物理页。如果您需要逻辑页(例如,如果指示 Word 重新开始页码),您应该使用 wdActiveEndAdjustedPageNumber
常量。有关来源和详细信息,请参阅 this link。