Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Text Object Model to store location in a Word document? 2

Status
Not open for further replies.

PatternNut

Programmer
Jul 29, 2007
5
As part of an MS Word add-in that I am constructing, I need to store several locations within a Word document. To date, I have done that using Word bookmarks. However, storing hundreds of bookmarks is intolerably slow. My understanding is that I could use an iTextRange to do this much faster. However, how do I set an itextrange equal to a range in the activedocument?

Microsoft provide much guidance on textobjectmodel, but not much support on integrating text object model into VBA. The best example I found was the superb post from strongm in this thread:


Is there a way to modify that approach so that it takes a Word range in the activedocument and stores that as an itextrange. Can I then call that iTextRange from another module in my add-in?

Thanks in advance,

PatternNut
 
Is there a way to modify that approach so that it takes a Word range in the activedocument and stores that as an itextrange
Stores it where? In the Word document? I am not sure Word can do that. Essentially that is exactly what bookmarks are. I do not know of a way for Word to store your itextrange objects. However, I would be very interested in finding out that it can be done.

What is exactly slow about using your bookmarks?



faq219-2884

Gerry
My paintings and sculpture
 
Hi, thanks for the quick reply.

The software I'm working on is for checking... and I use Word bookmarks when it finds an error. That's fine in a short document. But when the document size gets to be more than about 50 pages or the number of errors nears 100, you end up with so many bookmarks that the software goes really slow.

I had the idea of using iTextRange instead because I read this on MSDN:

"Multiple text ranges can be active and work cooperatively on the same story and evolve with the story. For example, if one text range deletes specified text before another text range, the latter tracks the change. In this sense, text ranges are similar to Microsoft Word bookmarks, which also track editing changes."


If I could understand how to use iTextRange, I could probably do other things faster than Word ranges. However, I can't figure out how to reach that functionality from VBA.

Does that make sense? If so, what I think I need to do is set an itextrange equal to a word range in VBA. Any advice on how to do that?

Thanks again,

PatternNut
 
How do you expect to replace bookmarks with RichEdit control(s) ?
 
If your software goes very slowly at only 50 pages and/or 100 bookmarks I would suggest you have a problem somewhere else - those are relatively small numbers which Word can easily cope with.

However, you only really need Bookmarks if they are to be saved with the Document - and if that's the case, your options are limited. If that isn't the case, have you tried simply saving the Ranges themselves?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Damnit Tony!

That's an excellent suggestion. I am not a natural programmer and sometimes solutions are just staring me in the face. That's so simple. Thank you.

The only potential problem is that marking errors needs to be robust. The good thing about a bookmark is that users can make changes anywhere else in the document and the bookmark is unaffected. Is that true for a range? If so, what's the best way to pass (potentially) hundreds of ranges between modules, etc.

Thanks again,

PatternNut
 
1. I certainly agree with Tony, if you are having problems with 50 pages/100 bookmarks, then something else is having affect. Word should be able to handle that with little problem.

2. "The good thing about a bookmark is that users can make changes anywhere else in the document and the bookmark is unaffected. Is that true for a range?" Bookmarks are simply named ranges. Flexible ranges. Content added/extracted from within a bookmark automatically resizes the range values of that bookmark.

3. You need to clarify your requirements about passing ranges. You certainly can define ranges and pass them as range objects. However, unless they are bookmarks, they will not be stored. And it is the storage of these that was asked about. I think.

I believe (but I may be wrong) that when Tony mentions "have you tried simply saving the Ranges themselves", he is talking about saving the Start/End values. Which are of course just numbers.

Error would happen if changes were made to the document. Unlike actual bookmarks, an array of values would remain fixed.

I do not know of way to "save" Ranges outside of a document, except as numeric values.

Tony, what do you mean exactly by saving the Ranges themselves?

faq219-2884

Gerry
My paintings and sculpture
 
Sorry, I wasn't clear enough.

If you only want to maintain the information during the course of your process running, then you can save ranges the same way you can save any other variables, something like ...
Code:
Dim MyRanges(1 to 25) As Word.range

Set MyRanges(1) = Selection.Range.Duplicate
Set MyRanges(2) = ActiveDocument.Range(11,111)
etc.
The Ranges will be dynamic, that is adjust according to user editing, just like Bookmarks. All you need to do is make sure your array has scope enough for your use. As you don't know how big the array will be you probably want to make it dynamic but that doesn't affect how the Ranges work.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
That's awesome Tony. Thank you. I'll run a test to see how this compares to the bookmarks in terms of speed. It's not that 50 - 100 bookmarks is super-slow, it's just that people expect software to be slick and a second or two seems an awful long time when you're waiting.

Unfortunately, you were also right about the bookmarks not being the main culprit! They account for a little bit, but mostly it's another section. The help on this thread has been truly outstanding. I'll start a new one when I figure out exactly how to explain the other problem.

Thank you all so much, this was my first post here and the answers have been really useful and well thought out.

Cheers,

PatternNut


 
That is what I thought. Using them as temporary...well, bookmarks. Which are just, again, named ranges.

They are only "saved" within the scope of the procedure (unless it is a global variable, which would make more sense), and the document itself being open.

Either that, or, the numeric values can be saved to a text file. They could be brought back into the document and Re-Set.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top