macro to find position in dosument and see if it is inside a comme

S

spencer

Hello,

I hope that you can help me, I'm afraid that I can't work this out for
myself. I am trying to create a macro that works out the current position of
the beam cursor in the active document. It would then take this position and
loop through all comments in the active document and if the current postion
was inside some text in the document that had a comment associated with it it
would then get the text of that comment. I have not been able to find out
how to work out the position of the focus (in any format that I can compare
to comments characters, line - col will do) or to find out from the comment
the position in the document that this comment applies to. If I could get a
method for geting focus location and getting the start point and the end
point of the comment in the document that would be great.

This may sound like a strange request but it is related to someone using
screenreader software to read a document.

I hope that this makes sense and that someone can offer me some pointers
thanks in advance
Spencer
 
J

Jay Freedman

spencer said:
Hello,

I hope that you can help me, I'm afraid that I can't work this out for
myself. I am trying to create a macro that works out the current
position of the beam cursor in the active document. It would then
take this position and loop through all comments in the active
document and if the current postion was inside some text in the
document that had a comment associated with it it would then get the
text of that comment. I have not been able to find out how to work
out the position of the focus (in any format that I can compare to
comments characters, line - col will do) or to find out from the
comment the position in the document that this comment applies to.
If I could get a method for geting focus location and getting the
start point and the end point of the comment in the document that
would be great.

This may sound like a strange request but it is related to someone
using screenreader software to read a document.

I hope that this makes sense and that someone can offer me some
pointers thanks in advance
Spencer

You don't need to know the position (line/col) of the cursor to do this.
Instead, you can loop through the comments (if any) in the paragraph that
contains the cursor, checking whether each comment's .Scope (the range
highlighted by the comment) contains the cursor:

Dim oComm As Comment
Dim oRg As Range

Set oRg = Selection.Range
oRg.Collapse wdCollapseEnd ' in case Selection is extended

For Each oComm In oRg.Paragraphs(1).Range.Comments
If oRg.InRange(oComm.Scope) Then
MsgBox oComm.Range.Text
End If
Next

If the scope does contain the cursor, then the messagebox shows the text of
the comment. Of course, instead of a messagebox you can do anything else
with the comment that's appropriate for your application.

Note that there can be more than one comment with the same scope (or
overlapping scopes), so you could see more than one message. Conversely, if
there are no comments that contain the cursor, the For Each loop simply does
nothing.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

spencer

Thank you very much for your answer. That looks like a great solution to my
problem and much more elegant than how I was trying to accomplish it. Thanks
again

Spencer
 

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