johndavidson
07-25-2016, 12:32 AM
Both of the following return the screen position of the cursor:
MsgBox Selection.Information(wdVerticalPositionRelativeToTextBoundary)
MsgBox Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToTextBo undary)
This is fine unless there are, for example, some endnotes on the screen in, say, Print or Web view. The text boundary among the endnotes or in the main text following is then calculated relative to the start and end of the endnotes area, not to the top of the screen.
Is there a way of calculating the position of the text cursor relative to the top of the screen in Print view when there are endnotes somewhere on the screen? What is needed is a What is needed is a 'Selection.Information.wdVerticalPositionRelativeToScreenEdge', but that does not exist
This is required after doing various searches and text manipulations to reposition the on-screen text and cursor in the same place as it was before starting.
This is the routine I'm using:
Sub rePosCursor(Optional rePos As Boolean)
' Save (=True)/Restore (=False) cursor and screen position using screen-text metrics
' Public vPosPub As Long
' Public sStartPub As Long
' Public sEndPub As Long
Dim vInt As Integer
' Test code - 'Text Boundary' means edge of pane, not screen, so endnotes restart at 0 even if in the middle of a screen,
' and likewise the main text following the endnotes. This includes Print and Web view
' So this routine does not work when in endnotes or when the cursor is below endnotes in Print or Web View
' wdVerticalPositionRelativeToPage gives the position relative to the edge of the document page, so that is of no use here
' What is needed is a 'wdVerticalPositionRelativeToScreenEdge', but that does not exist ...
' MsgBox Selection.Information(wdVerticalPositionRelativeToTextBoundary)
' MsgBox Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToTextBo undary)
If rePos = True Then
vPosPub = Selection.Information(wdVerticalPositionRelativeToTextBoundary)
sStartPub = Selection.Start
sEndPub = Selection.End
Else
' Now try and put the text back the way it was in the place it was on screen
' The surest way to do this is not to use FindAwordAddAuto, but one of the other FindWordAdd routine
' that don't need to move the position of the cursor to find a tag character.
' Later. No need to do this when using the range object
' Keep code in case it is useful
Selection.Start = sStartPub
Selection.End = sEndPub ' This locates the line with the selection at the top of the Word text screen
Selection.MoveRight Unit:=wdCharacter, count:=1 ' This brings the cursor onto the screen after various text searches etc.
' Try & calculate where vPos would be in terms of lines from the top of the page
' 17.3" notebook is 0 to 425 high
' 125% has 35 lines/ 12.14 Line Height at 12pt
' 135% has 32 lines/ 13.28 Line Height at 12 pt; likewise 140%
' 145% has 30 lines/ 14.17 Line Height at 12 pt
' 150% has 29 lines/ 14.65 Line Height at 12pt
' As a compromise, an average line height of 14 works quite well.
If vPosPub > 12 Then ' Is it the first line?
vInt = (vPosPub) / 20 ' Calculate the no. of lines from the top of the page
Selection.MoveUp Unit:=wdLine, count:=vInt ' Move the cursor up accordingly
Selection.Start = sStartPub ' And reselect the text
Selection.End = sEndPub ' This seems to work most of the time
End If
End If
End Sub
Thanks
MsgBox Selection.Information(wdVerticalPositionRelativeToTextBoundary)
MsgBox Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToTextBo undary)
This is fine unless there are, for example, some endnotes on the screen in, say, Print or Web view. The text boundary among the endnotes or in the main text following is then calculated relative to the start and end of the endnotes area, not to the top of the screen.
Is there a way of calculating the position of the text cursor relative to the top of the screen in Print view when there are endnotes somewhere on the screen? What is needed is a What is needed is a 'Selection.Information.wdVerticalPositionRelativeToScreenEdge', but that does not exist
This is required after doing various searches and text manipulations to reposition the on-screen text and cursor in the same place as it was before starting.
This is the routine I'm using:
Sub rePosCursor(Optional rePos As Boolean)
' Save (=True)/Restore (=False) cursor and screen position using screen-text metrics
' Public vPosPub As Long
' Public sStartPub As Long
' Public sEndPub As Long
Dim vInt As Integer
' Test code - 'Text Boundary' means edge of pane, not screen, so endnotes restart at 0 even if in the middle of a screen,
' and likewise the main text following the endnotes. This includes Print and Web view
' So this routine does not work when in endnotes or when the cursor is below endnotes in Print or Web View
' wdVerticalPositionRelativeToPage gives the position relative to the edge of the document page, so that is of no use here
' What is needed is a 'wdVerticalPositionRelativeToScreenEdge', but that does not exist ...
' MsgBox Selection.Information(wdVerticalPositionRelativeToTextBoundary)
' MsgBox Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToTextBo undary)
If rePos = True Then
vPosPub = Selection.Information(wdVerticalPositionRelativeToTextBoundary)
sStartPub = Selection.Start
sEndPub = Selection.End
Else
' Now try and put the text back the way it was in the place it was on screen
' The surest way to do this is not to use FindAwordAddAuto, but one of the other FindWordAdd routine
' that don't need to move the position of the cursor to find a tag character.
' Later. No need to do this when using the range object
' Keep code in case it is useful
Selection.Start = sStartPub
Selection.End = sEndPub ' This locates the line with the selection at the top of the Word text screen
Selection.MoveRight Unit:=wdCharacter, count:=1 ' This brings the cursor onto the screen after various text searches etc.
' Try & calculate where vPos would be in terms of lines from the top of the page
' 17.3" notebook is 0 to 425 high
' 125% has 35 lines/ 12.14 Line Height at 12pt
' 135% has 32 lines/ 13.28 Line Height at 12 pt; likewise 140%
' 145% has 30 lines/ 14.17 Line Height at 12 pt
' 150% has 29 lines/ 14.65 Line Height at 12pt
' As a compromise, an average line height of 14 works quite well.
If vPosPub > 12 Then ' Is it the first line?
vInt = (vPosPub) / 20 ' Calculate the no. of lines from the top of the page
Selection.MoveUp Unit:=wdLine, count:=vInt ' Move the cursor up accordingly
Selection.Start = sStartPub ' And reselect the text
Selection.End = sEndPub ' This seems to work most of the time
End If
End If
End Sub
Thanks