This project has moved and is read-only. For the latest updates, please go here.

Compare "reference equality" of COM proxies?

Dec 17, 2011 at 10:55 PM

I would like to be able to:

  1. compare Word Interop COM proxies on a "reference equality" basis; and
  2. map from a specific object (say a paragraph) to the collection it comes from, OR at least
  3. determine whether two paragraphs are from the same section and which one comes relatively before the previous one

Why do I want to do this? I am trying to build a Word Add-In that acts similarly to a spell-checker in the sense that it runs in the background (by background I mean by regularly stealing time from the main Word thread using SendMessage) and scans the document for certain text "tokens". I want to be able to keep a collection of the tokens around and update them as the document changes. A specific example of this is if the user edits a given paragraph, I want to rescan the paragraph and update my data structure which points to that paragraph. If there is no way to map between the paragraph the user edited in (i.e. the paragraph where the start of the selection range is) and a paragraph that I have "stored" in a data structure, I can't do this.

Example Code for item #1, above

If I write the following VBA code:


Dim Para1 As Paragraph
Dim Para2a As Paragraph
Dim Para2b As Paragraph
Set Para1 = ActiveDocument.Paragraphs(1)
Set Para2a = Para1.Next
Set Para2b = Para1.Next.Next.Previous
If Para2a Is Para2b Then
    Debug.Print ("Para2a Is Para2b")
    Debug.Print ("Para2a Is Not Para2b")
End If

Then I am getting the output:

"Para2a Is Not Para2b"

Which is perhaps physically true (different COM proxies) but not logically true. I need to be able to compare those paragraphs and determine if they are logically the same paragraph.

For items 2 and 3 above, hopefully they will be self-explanatory. Say I have a paragraph (interop proxy) reference. I want to figure out "where" it is in the document. Does it belong to Section 1? Is it in a footer? Without this ability, all I can reasonably do to obtain an idea of where things come from is rescan the entire document every time it changes, which is of course absurdly inefficient and won't be timely enough for the app user.

Any thoughts greatly appreciated! I'm happy to post additional information as needed.

Dec 20, 2011 at 4:12 PM


i have no experience with paragraphs, i see the definition and found the properties ID and ParaID but these propterties are only available in word 14.
the index position in the Paragraphs collection can be helpfull i believe.
Any paragraph has a Range, maybe you see here the position of your paragraph.