Hi,
Is there any method to identify content outside of content controls in Word?
Thank you!
How to Identify Content outside of content controls in Word
-
- 3StarLounger
- Posts: 318
- Joined: 23 Aug 2016, 07:43
- Location: Sri Lanka
-
- 5StarLounger
- Posts: 641
- Joined: 10 Jan 2016, 15:56
- Location: Madison, Wisconsin
Re: How to Identify Content outside of content controls in W
"Identify" how? For what purpose?
Bookmarks?
Bookmarks?
![Cheers :cheers:](./images/smilies/cheers.gif)
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
-
- 3StarLounger
- Posts: 318
- Joined: 23 Aug 2016, 07:43
- Location: Sri Lanka
Re: How to Identify Content outside of content controls in W
Hi Charles,
Identify means highlight or apply Word comment for all content outside of content controls in Word. Purpose is my documents using averagely 200 content controls for 50 pages. I have to ensure manually all content inside the content control. So, that's why I'm looking for this type of macro.
I have attached sample document and expected result document for more clarity. Thank You!
Identify means highlight or apply Word comment for all content outside of content controls in Word. Purpose is my documents using averagely 200 content controls for 50 pages. I have to ensure manually all content inside the content control. So, that's why I'm looking for this type of macro.
I have attached sample document and expected result document for more clarity. Thank You!
You do not have the required permissions to view the files attached to this post.
-Sampath-
-
- Administrator
- Posts: 78920
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to Identify Content outside of content controls in W
The following will be extremely slow but I don't know a better way to do it.
Code: Select all
Sub HighlightOutsideContentControls()
Dim i0 As Long
Dim i As Long
Dim n As Long
Dim f As Long
Application.ScreenUpdating = False
n = ActiveDocument.Characters.Count
For i = 1 To n
If ActiveDocument.Characters(i).Information(wdInContentControl) Then
If Not f Then
If i > 1 Then
ActiveDocument.Range(i0, i - 1).HighlightColorIndex = wdYellow
End If
f = True
End If
Else
If f Then
i0 = i
f = False
End If
End If
Next i
If Not f Then
ActiveDocument.Range(i0, n).HighlightColorIndex = wdYellow
End If
Application.ScreenUpdating = True
End Sub
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 318
- Joined: 23 Aug 2016, 07:43
- Location: Sri Lanka
Re: How to Identify Content outside of content controls in W
Thank you Hans.
I have check your code in Word 2010. I got this run-time error.
Code: 4608
Error: Value out of range.
I have check your code in Word 2010. I got this run-time error.
Code: 4608
Error: Value out of range.
-Sampath-
-
- Administrator
- Posts: 78920
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to Identify Content outside of content controls in W
The code works for me in your sample document...
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 318
- Joined: 23 Aug 2016, 07:43
- Location: Sri Lanka
Re: How to Identify Content outside of content controls in W
Hi Hans,
I have check the code in Word 2016 and it worked. But it's not work in 2010 version. It would be great if possible to replicate this for 2010.
I have check the code in Word 2016 and it worked. But it's not work in 2010 version. It would be great if possible to replicate this for 2010.
-Sampath-
-
- Administrator
- Posts: 78920
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to Identify Content outside of content controls in W
I see that the code indeed doesn't run in Word 2010 - the constant wdInContentControl was introduced in Word 2013. I don't know of a way around this that would perform acceptably.
One more reason to avoid content controls like the plague...
One more reason to avoid content controls like the plague...
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78920
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to Identify Content outside of content controls in W
Perhaps this will work. I have only tested it on your document; I cannot guarantee that it will work on more complicated documents.
Code: Select all
Sub HighlightOutsideContentControls()
Dim cs As Long
Dim ce As Long
Dim s() As Long
Dim e() As Long
Dim i As Long
Dim j As Long
Dim n As Long
Dim f As Boolean
Dim t As Long
' Initialize arrays for first content control
With ActiveDocument.ContentControls(1).Range
n = 1
ReDim s(1 To 1)
s(1) = .Start
ReDim e(1 To 1)
e(1) = .End
End With
' Loop through the rest of the content controls
For i = 2 To ActiveDocument.ContentControls.Count
With ActiveDocument.ContentControls(i).Range
cs = .Start
ce = .End
End With
f = True
For j = 1 To n
If cs >= s(j) And ce <= e(j) Then
f = False
Exit For
End If
Next j
If f Then
n = n + 1
ReDim Preserve s(1 To n)
s(n) = cs
ReDim Preserve e(1 To n)
e(n) = ce
End If
Next i
' Sort the arrays
For i = 1 To n - 1
For j = i + 1 To n
If s(i) > s(j) Then
t = s(i)
s(i) = s(j)
s(j) = t
t = e(i)
e(i) = e(j)
e(j) = t
End If
Next j
Next i
' Highlight ranges outside content controls
Application.ScreenUpdating = False
' Before first content control
If s(1) > 1 Then
ActiveDocument.Range(1, s(1) - 1).HighlightColorIndex = wdYellow
End If
' Between content controls
For i = 1 To n - 1
ActiveDocument.Range(e(i) + 1, s(i + 1) - 1).HighlightColorIndex = wdYellow
Next i
' After last content control
If e(n) < ActiveDocument.Content.End Then
ActiveDocument.Range(e(n) + 1, ActiveDocument.Content.End).HighlightColorIndex = wdYellow
End If
Application.ScreenUpdating = True
End Sub
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 318
- Joined: 23 Aug 2016, 07:43
- Location: Sri Lanka
Re: How to Identify Content outside of content controls in W
Thank you Hans,
This version working with Word 2010.
I have tested with my documents and this macro worked on more complicated documents. Thank you again!
This version working with Word 2010.
I have tested with my documents and this macro worked on more complicated documents. Thank you again!
-Sampath-