PDA

View Full Version : Solved: create bookmarks like this 1,2 .. 1,2 .. 3,4 and so one



white_flag
05-12-2010, 06:38 AM
Hello (today it is raining pretty bad)
I would like to have something like this:

bmk1 bmk2 tmk1 tmk2 bmk3 bmk4 tmk3 tmk4 ..etc
and my code it is like this:

Sub createbookmarks()
Dim i As Integer
With ActiveDocument
For i = 1 To 18

.Bookmarks.Add "bmk" & i, .Characters.Last
.Range.InsertAfter Chr(160) & Chr(160) & Chr(160)
.Bookmarks.Add "bmk" & i + 1, .Characters.Last
.Range.InsertAfter Chr(160) & Chr(160) & Chr(160)
.Bookmarks.Add "tmk" & i, .Characters.Last
.Range.InsertAfter Chr(160) & Chr(160) & Chr(160)
.Bookmarks.Add "tmk" & i + 1, .Characters.Last
.Range.InsertAfter Chr(160) & Chr(160) & Chr(160)

Next
End With
End Sub


but It is acting like this: bmk1 tmk1 bmk2 tmk2 ..etc
what can I do?

TonyJollans
05-12-2010, 09:41 AM
You start the loop with i=1, then create bmk1, bmk2, tmk1, tmk2, then you loop and i is incremented to i=2, and then you create bmk2, bmk3, tmk2, tmk3, overwriting the original bmk2 and tmk2, and so on.

Change the loop to be:

For i = 1 to 18 Step 2

white_flag
05-12-2010, 09:50 AM
Tony, thank you very much. now it is acting in the way that I like.

fumei
05-12-2010, 10:02 AM
1. it seems very odd naming - bmk and tmk. Is there some purpose for this?

2. did you ever try to Step Through and watch what happens?

3. bookmarks MUST have unique names. You are not giving unique names.

If you Step Through you can watch it happen.

1. Start the loop - i = 1 (IMPORTANT!)
2 bmk1 (bmk & i) is created...fine
3. bmk2 (bmk & 1 + 1) is created. WATCH this.
4. tmk1 (tmk & i) is created.
5. tmk2 (tmk & i + 1) is created.

Status? bmk1, bmk2, tmk1, tmk2


6. increment the loop, therefore i = 2
7. bmk2 (bmk & i) is created....but bmk2 already exists, and you can NOT have identical names.

What happens? If you Step Through (something I highly recommend as a standard debugging procedure), you can easily see that:

Status? bmk1 tmk1 tmk2 bmk2

Word takes you instruction to create bmk2 (at the end) and does EXACTLY what you tell it to do. However...since you can NOT have two bookmarks with the same name..it deletes the first one. So

bmk1, bmk2, tmk1, tmk2

becomes

bmk1, tmk1, tmk2, bmk2

and so on.

It is doing precisely what you tell it to do.

Although this still seems a very odd thing to do, what can you do about it. Figure out the logic of determining if bmk2 exists already, and then decide...

Do you want bmk3 - even though i = 2? Or what? YOU must determine exactly, precisely, what you want to happen. The coding will not be difficult, just figure out - precisely - the logic.

fumei
05-12-2010, 10:03 AM
As usual, that darn Tony, so brief and to the point.

white_flag
05-12-2010, 10:11 AM
Gerry thx for the explanation. I was following the the logic ..but I didn't know how the world will delete the bookmarks (now I know). what I do not know ..it is: how to follow an variable (to see what it is happening there) ..thank you :)

TonyJollans
05-12-2010, 10:26 AM
LOL! You give much more complete answers than I do, Gerry - they must take time to type.

fumei
05-12-2010, 10:47 AM
Well, I DO type fast.