Answered by:
How to Find the Exact Position of a Content Control

Question
-
I have an Access application that creates Word documents. I have used Styles to make subheadings. When a subheading splits a page I programmed the app to add a paragraph at the end of the previous page and add a copy of the subheading with the word "...continued" added. The problem occurs when a Content Control is split across two pages. When attempting to add a paragraph (.TypeParagraph) to the end of the previous page I get an error. So I added code that would move the cursor to the point just before the ContentControl and add the paragraph at this point. However, this only works if there is one Content Control on the last line of the previous page and the Content Control is at the end of the line. But if the Content Control is at the beginning of the line this causes an error when some of the content of the Content Control is also in the second line from the bottom of the previous page.
It seems my best solution is to determine if the Content Control is at the end of the last line of the previous page. Is there any way to determine where the Content Control is in a line or sentence? Or better still determine if the cursor is inside of a Content Control?
- Edited by kay_some Wednesday, November 25, 2020 3:37 PM
Wednesday, November 25, 2020 1:49 PM
Answers
-
Rich,
Here your code throws an error. Wouldn't it be best to test to see if the CC spans pages?
Sub EvaluateCC()
Dim oDoc As Word.Document
Dim oRng As Word.Range, oRngS As Word.Range, oRngE As Word.Range
Dim lngIndex As Long, oCC As Word.ContentControl
Set oDoc = ActiveDocument
If Selection.Information(wdInContentControl) Then
For lngIndex = 1 To oDoc.ContentControls.Count
Set oCC = oDoc.ContentControls(lngIndex)
If Selection.Range.InRange(oCC.Range) Then
Debug.Print "In one"
Set oRng = oCC.Range
oRng.MoveStart Word.WdUnits.wdCharacter, Count:=-1
oRng.MoveEnd Word.WdUnits.wdCharacter, Count:=1
Set oRngS = oRng.Duplicate
Set oRngE = oRng.Duplicate
oRngS.Collapse wdCollapseStart
oRngE.Collapse wdCollapseEnd
Select Case True
Case oRngS.Information(wdActiveEndPageNumber) <> oRngE.Information(wdActiveEndPageNumber)
MsgBox "The cursor is in a CC and the CC spans two or more pages"
Case Else
MsgBox "The cursor is in a CC and the CC does not span pages"
End Select
End If
Next
End If
End Sub
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
- Marked as answer by kay_some Wednesday, December 2, 2020 7:37 PM
Saturday, November 28, 2020 2:13 PM
All replies
-
To find if your current selection point is within a content control, you have to compare ranges. Here is some example code for how it could be done.
Sub InContentControl() Dim doc As Word.Document, rng As Word.Range, i As Integer, cc As Word.contentControl Set doc = ActiveDocument If doc.ContentControls.Count = 0 Then Exit Sub For i = 1 To doc.ContentControls.Count Set cc = doc.ContentControls(i) If Selection.Range.InRange(cc.Range) Then Debug.Print "In one" Set rng = cc.Range rng.MoveStart Word.WdUnits.wdCharacter, Count:=-1 rng.MoveEnd Word.WdUnits.wdCharacter, Count:=1 If Asc(rng.Characters.First) = 13 Then 'the character before the Content Control is a Page Break 'The Content Control is at the beginning of a new paragraph 'Now do something ElseIf Asc(rng.Characters.Last) = 13 Then 'the character following the Content Control is a Page Break 'the Content Control is at the end of a paragraph 'Now do something Else 'the Content Control is in the middle of a paragraph 'now do something End If End If Next End Sub
Hope it helps
Kind Regards, Rich ... http://greatcirclelearning.com
Friday, November 27, 2020 7:36 PM -
Rich,
Here your code throws an error. Wouldn't it be best to test to see if the CC spans pages?
Sub EvaluateCC()
Dim oDoc As Word.Document
Dim oRng As Word.Range, oRngS As Word.Range, oRngE As Word.Range
Dim lngIndex As Long, oCC As Word.ContentControl
Set oDoc = ActiveDocument
If Selection.Information(wdInContentControl) Then
For lngIndex = 1 To oDoc.ContentControls.Count
Set oCC = oDoc.ContentControls(lngIndex)
If Selection.Range.InRange(oCC.Range) Then
Debug.Print "In one"
Set oRng = oCC.Range
oRng.MoveStart Word.WdUnits.wdCharacter, Count:=-1
oRng.MoveEnd Word.WdUnits.wdCharacter, Count:=1
Set oRngS = oRng.Duplicate
Set oRngE = oRng.Duplicate
oRngS.Collapse wdCollapseStart
oRngE.Collapse wdCollapseEnd
Select Case True
Case oRngS.Information(wdActiveEndPageNumber) <> oRngE.Information(wdActiveEndPageNumber)
MsgBox "The cursor is in a CC and the CC spans two or more pages"
Case Else
MsgBox "The cursor is in a CC and the CC does not span pages"
End Select
End If
Next
End If
End Sub
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
- Marked as answer by kay_some Wednesday, December 2, 2020 7:37 PM
Saturday, November 28, 2020 2:13 PM -
Excellent improvement Greg.
Thanks
Kind Regards, Rich ... http://greatcirclelearning.com
Saturday, November 28, 2020 2:58 PM -
Thank you, thank you, thank you!!!! This was so simple. It was the "Selection.Information(wdInContentControl)" statement that solved the issue. I just needed to know if the last character of the last line on the page was in a content control. Here's the code:
If wDoc.Application.Selection.Characters(1) <> Chr(13) Then
If wDoc.Application.Selection.Information(wdInContentControl) Then
'If the cursor is in a Content Control
wDoc.Application.Selection.Bookmarks("\Line").Select
wDoc.Application.Selection.ContentControls(wDoc.Application.Selection.ContentControls.Count).Range.Select
wDoc.Application.Selection.Collapse Direction:=wdCollapseStart
wDoc.Application.Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdMove
Else
wDoc.Application.Selection.Collapse Direction:=wdCollapseStart
wDoc.Application.Selection.MoveEnd Unit:=wdLine, Count:=1
wDoc.Application.Selection.Collapse Direction:=wdCollapseEnd
'wDoc.Application.Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdMove
End If
wDoc.Application.Selection.TypeParagraph
Else
wDoc.Application.Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
End IfWednesday, December 2, 2020 7:51 PM