Determine visible text on screen

J

John Rodgers

Is it possible with the Word object model to determine what text is
visible onscreen at any particular time?

Thanks,
John
 
K

Klaus Linke

John Rodgers said:
Is it possible with the Word object model to determine
what text is visible onscreen at any particular time?


Hi John,

Not easily, as far as I can see. The code below doesn't work too well: It's off by a few lines, and doesn't perfectly catch what's visible of those lines. Maybe I have made an error you can correct, or maybe ..RangeFromPoint is a bit buggy.

Greetings,
Klaus


Dim pLeft As Long
Dim pTop As Long
Dim pWidth As Long
Dim pHeight As Long
Dim rngStart As Range, rngEnd As Range
Dim myDoc As Document
Set myDoc = ActiveDocument
With myDoc.ActiveWindow
pLeft = PointsToPixels(.Left)
pTop = PointsToPixels(.Top)
pWidth = PointsToPixels(.Width)
pHeight = PointsToPixels(.Height)

Set rngStart = .RangeFromPoint(pLeft, pTop)
Set rngEnd = .RangeFromPoint(pLeft + pWidth, pTop)
rngStart.End = rngEnd.End
MsgBox rngStart.Text, , "First line"
Set rngStart = .RangeFromPoint(pLeft, pTop + pHeight)
Set rngEnd = .RangeFromPoint(pLeft + pWidth, pTop + pHeight)
rngStart.End = rngEnd.End
MsgBox rngStart.Text, , "Last line"
End With
 
K

Klaus Linke

It's off by a few lines [...]

My screen was set to 120 dpi. After I changed to 96 dpi, the results were much better.
Don't know if there's a setting for the screen resolution that I have missed, or if you'd have to do some additional calculations with 120 dpi.

Regards,
Klaus
 
J

John Rodgers

Klaus said:
Hi John,

Not easily, as far as I can see. The code below doesn't work too well: It's off by a few lines, and doesn't perfectly catch what's visible of those lines. Maybe I have made an error you can correct, or maybe .RangeFromPoint is a bit buggy.

Greetings,
Klaus


Dim pLeft As Long
Dim pTop As Long
Dim pWidth As Long
Dim pHeight As Long
Dim rngStart As Range, rngEnd As Range
Dim myDoc As Document
Set myDoc = ActiveDocument
With myDoc.ActiveWindow
pLeft = PointsToPixels(.Left)
pTop = PointsToPixels(.Top)
pWidth = PointsToPixels(.Width)
pHeight = PointsToPixels(.Height)

Set rngStart = .RangeFromPoint(pLeft, pTop)
Set rngEnd = .RangeFromPoint(pLeft + pWidth, pTop)
rngStart.End = rngEnd.End
MsgBox rngStart.Text, , "First line"
Set rngStart = .RangeFromPoint(pLeft, pTop + pHeight)
Set rngEnd = .RangeFromPoint(pLeft + pWidth, pTop + pHeight)
rngStart.End = rngEnd.End
MsgBox rngStart.Text, , "Last line"
End With
Thanks Klaus,

I'll give it a try later today - I totally missed the PointsToPixels /
RangeFromPoint when searching thru the documentation (guess my eyes must
have glazed over...)

Thanks,
John
 
K

Klaus Linke

John said:
I'll give it a try later today - I totally missed the PointsToPixels /
RangeFromPoint when searching thru the documentation
(guess my eyes must have glazed over...)


Don't think I ever used -- or even noticed -- RangeFromPoint either!
What I have used a few times is ScrollIntoView to make sure a certain Range or Shape is on screen.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top