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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a bookmark between two existing bookmarks 1

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
This issue is related to the problem I had at link:

I'm using VBA in a Visual Basic 6 app.

I want to Insert a bookmark between two existing bookmarks which look like:
[Here is the first book mark's text of the BKDestination Doc which is a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]
[Here is the second book mark's text of the BKDestination Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]

I want to take a BK from another document (BKSource Doc) and insert it between these two BKs (after the first BK). This BK consists of similar text as the previous two:
[Here is the book mark's text of the BKSource Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]

Code:
BKSource.Bookmarks(strBookmark).Range.Copy
Set RangeInsertAfter = BKDestination.Bookmarks(strBookMarkInsertAfter).Range

RangeInsertAfter.Collapse Direction:=wdCollapseEnd
RangeInsertAfter.Paste
RangeInsertAfter.Bookmarks.Add strBookmark

This works but unfortunately inserts the new BK (blue brackets) and it's text inside the BK of the Destinations BK (red brackets) so now it looks like:
[Here is the first book mark's text of the BKDestination Doc which is a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]
[[Here is the book mark's text of the BKSource Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]
Here is the second book mark's text of the BKDestination Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]

There are other BKs in the document being inserted, some are between two existing BKs some are not. I don't have a problem where they're not not next to each other.

I've tried copying the range of the problem BK into another object so I can add it after I add the new BK but when I do this it still looks like the same mess as above.

There is code later on which deletes BKs so as you can see if the second BK from the Destination Doc gets deleted it'll take the new BK with it. I cannot delete first and add later because the one being deleted may be a BK needed to use to add a new BK after.

Any help will be greatly appreciated.
 
You should keep things that are together in one thread. In other words, you should be posting this as a continuation of that other thread.

Although I can see why you may think of this as a different issue...but it is not.

The problem is that if two bookmarks are butt up against each other...seen below in red:

[[/color red]Here is the first book mark's text of the BKDestination Doc which is a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark][/color red]
[[/color red]Here is the second book mark's text of the BKDestination Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark][/color red]

then the Bookmark.End of the first one, is the SAME value as the Bookmark.Start of the second one.

You can NOT put anything between them. They are the same value.

If Bookmark1.End = 184

and

Bookmark2.Start = 184

You can not insert anything between them. You can not insert anything at 184.5, there is no 184.5.

Anything you insert will be either in Bookmark1 (at the end), OR, in Bookmark2 (at the beginning). Precisely as you have got.

You can not put anything between the bookmarks either manually, or by code. The End and Start values are the same.

That being said, this intrigued me, and...there is a way. I have successfully inserted a bookmark (including its name) from a different document between two bookmarks that do butt up to each other. Here is how:

ASSUMPTIONS:

a) both documents are open
b) for my test, the source document = Source.doc, and destination document = Destination.doc
c) the two bookmarks in Destination.doc are named DestinationBM1 and DestinationBM1

My test gets the bookmark SourceBM1 (in Source.doc) and inserts it including its name between DestinationBM1 and DestinationBM1 in Destination.doc.

Is that clear?

Here are the variables used:
Code:
Dim oBM As Bookmark
Dim SourceDoc As Document
Dim DestinationDoc As Document
Dim r As Range
Dim TempRange As Range
Dim strBookMarkInsertAfter As String
Dim j As Long
Dim k As Long

The key is using TWO separate and independent range objects - r and tempRange.

1. I notice you do not seem to be using Document objects, but I find it is easier to work with them. So, first off, declare both documents as document objects. I used (after of course declaring the objects...):
Code:
Set SourceDoc = Documents("Source.doc")
Set DestinationDoc = Documents("Destination.doc")
2. you are passing a string variable (strBookMarkInsertAfter) that appears to be the name of the bookmark you want to insert after. Correct?

So Step 2 is getting the Long values (Start and End) of that bookmark. I simply declared and used two Long variables (j and k).
Code:
j = DestinationDoc.Bookmarks(strBookMarkInsertAfter).Range.Start
k = DestinationDoc.Bookmarks(strBookMarkInsertAfter).Range.End

3. This is crucial. Make a range object of the bookmark you want to insert after. Now delete the bookmark!

What is happening? Well, you have in the range object the contents of the bookmark, but the bookmark itself is now gone.
Code:
Set r = DestinationDoc.Bookmarks(strBookMarkInsertAfter).Range
DestinationDoc.Bookmarks(strBookMarkInsertAfter).Delete
BECAUSE the bookmark is deleted, you can insert a (temporary) paragraph mark. So you collapse the range, and do that.

4. you use a bookmark object to get the bookmark in source doc.
Code:
Set oBM = SourceDoc.Bookmarks("SourceBM1")

5. you set the OTHER range object to the values of the collapsed range:
Code:
Set TempRange = DestinationDoc.Range( _
   Start:=r.End, End:=r.End)
6. now use that range (TempRange) to bring in the bookmark from the source doc. You make its .Text the contents (which makes its range expand to cover that content) of the bookmark object.
Code:
TempRange.Text = oBM.Range.Text
7. add the bookmark name, using the range for its contents:
Code:
DestinationDoc.Bookmarks.Add oBM.Name, TempRange

8. RESET the original range object (since collapsed and a paragraph mark added) back to its original values using the j and k values. Then recreate the original bookmark, using the same name:
Code:
Set r = ActiveDocument.Range( _
   Start:=j, End:=k)

DestinationDoc.Bookmarks.Add strBookMarkInsertAfter, r
The original bookmark is now recreated. It is followed by the NEW bookmark brought in from the source document. Notice that there is no copy and pasted used (or required).

9. because a new (temporary) paragraph mark was inserted in order for this to work, I deleted it with a RESET of the range object, and a delete:
Code:
Set r = ActiveDocument.Range( _
   Start:=TempRange.End, End:=TempRange.End + 1)
r.Delete

Done.

I used your actual examples, and executed it. The result (showing the bookmark "tags")?

[[/color red]Here is the first book mark's text of the BKDestination Doc which is a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark]
[[/color red]Here is the book mark's text of the BKSource Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark][/color red]
[[/color red]Here is the second book mark's text of the BKDestination Doc which is also a couple paragraphs and includes docvariables and the paragraph mark at the end which is located on the next line.
paragraph mark][/color red]

That was an interesting exercise, and it can be done as I suggested in your other thread...by fully using ranges.

Gerry
 
I am curious about one thing.

the paragraph mark at the end which is located on the next line.

Why is it on the next line?

Gerry
 



Not only does Gerry arrive in Styles, but he has far Ranging abilities in Word, or should I phrase it, Fully Ranging abilities: One of the Incredibles!

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Oh, and that was so much fun, I have now added it as an lab exercise in my Word VBA course. It is a bit tricky, and I seriously doubt many students will actually fully work it out, but they may (hopefully, if they have been paying attention!) get close. It certainly makes one get down and dirty with ranges and the use of objects.

Thanks. I enjoyed that.

Gerry
 


I share your euphoria, or, as Victor Borge would say in his Inflationary language, ”I share your euphiveia.”

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Love this place. Someone who quotes Victor Borge...in context.

Gerry
 
Fumei
Thanks for the reply.

Yes, everything is clear and I am using object variables, I just figured it would be obvious. Sorry for any confusion.

The bookmark extents to the next line, after the paragraph mark, due to the document being built as dynamically as possible. Some BKs eventually get deleted so the blank line has to be included so extra blank lines don't exist when a BK is deleted.

BKs are added to a generic document depending on which state I am working with. Later in the application BKs are deleted (both existing and newly added) depending on what customers choose to have included. Instead of writing code to determine whether certain BKs get added is cumbersome and would cause more work and potentially be confusing to the next person who works on this app.

I have loops inserting new BKs and replacing existing BKs. Then I use this doc and run through another loop deleting BKs for individual customers.

I actually sorta solved the problem by adding one to the next BK.Start method when it and the End of the previous BK were the same. For instance:
BEFORE INSERT BK1.Start = BK2.End:
[BK1
p]
[BK2
p]

WHEN A NEW BK IS TO BE ADDED I ADD 1 TO Start OF SECOND BK:
[BK 1
P]
B[K2
P]

AFTER NEW BK IS ADDED:
[BK 1
P]
[NEW BK
P]
B[K2
P]

AFTER NEW BK IS ADDED I SUBTRACT 1 FROM Start:
[BK 1
P]
[NEW BK
P]
[BK2
P]

Then after I inserted the new BK I just subtracted 1 and it worked. But I'm still testing this. I will look over your code and use it if possible - once again I thank you for your reply and hard work.





 
Some BKs eventually get deleted so the blank line has to be included so extra blank lines don't exist when a BK is deleted."

my bolding.

No, sorry, but I am not buying the "has to be". You seem to be confusing (and you are in good company) bookmarks themselves with their contents.

"extra blank lines" (actually, paragraph marks) are content.

I can delete a bookmark and not change a darn thing of the content that was bookmarked. Of course you CAN change content, but technically, it has nothing to do with the fact it is/was bookmarked.

Besides, proper use of Styles - ahem - eliminates the need for ANY "extra" paragraph marks.

However, I have made that rant far too often.

I am quite curious as to the structure of what you seem to be doing.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top