Could please someone help with MS Word's ContentControl formatting programmatically. I wrote a code that goes to specific ContentControl according to its tag. And this code works well by going to each ContentControl that is specified by its Tag. However I need only to format only ContentControls on the page number 2.
I tried to limit the loop only for the specific bookmark, page, table, but it does not work. I don't need to go through each ContentContorl in the document, it takes too much time, which is opposite of the purpose of the macros. I am creating this macros to speed up report formatting.
Here is my code:
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
If you only want to format content controls on page 2, you should check the Information
property of the content control's Range
, e.g.:
Dim cc as ContentControl
If cc.Range.Information(wdActiveEndPageNumber) = 2 Then
... Do work ...
End If
The wdActiveEndPageNumber
constant refers to the physical page. If you desire the logical page (e.g. if Word has been instructed to restart page numbering) instead, you should use the wdActiveEndAdjustedPageNumber
constant. See this link for source and details.