multiple duplicate bookmarks

D

Drake

I know the title isn't the best, but I couldn't think of another way to
title this.

I need a macro to look for each instance of bookmark starting with bmkA.
There will be a bmkA(1) and bmkA(2)...etc. I want it to go to each of those
instances of bmkA and perform the same action. I also need it to be able to
handle if sometimes only bmkA(1) is there and there isn't any bmkA(2).

I hope this makes sense. Can anyone help me?

Drake
 
M

Malcolm Smith

Drake

You need to go through the bookmarks collection and then get the name of
each bookmark. Then if the bookmark name starts with a certain string
then you need to go there.

If the start characters are always the same then a simple set of buttons
on the toolbar would do the trick, else a modeless dialog box.

To be honest the code could be rather convoluted and would take you about
an hour or so to make it rather robust.

But the things you ought to look for is the Bookmarks collection and the
..Name property of each bookmark.

I would normally hack out some code here but I seem to be writing some
code when I should be down the pub as I have a deadline to meet.

- Malc
www.dragondrop.com
 
A

AW

Drake

Don't know if this helps
(Courtesy of previos post in usergroup)
____________________
Dim myBookmark as bookmark
for each myBookmark in ActiveDocument.Bookmarks
If Left(myBookmark.Name,4)=bmkA then

end if
next myBookmark
______________________

Al@n
 
D

Drake

Al@n -

this is exactly the kind of thing I was looking for! I have been working
with the code you gave me and need it to go to each instance where

left(mybookmark.name, 4) = bmkA

I have been trying

selection.goto mybookmark

to no avail. I was also trying to

set bmkname = left(mybookmark.name, 4),

but no luck there either. Here is what I have:
--------------------------------
Dim myBookmark As Bookmark

For Each myBookmark In ActiveDocument.Bookmarks

If Left(myBookmark.name, 4) = bmkA Then
Selection.GoTo myBookmark

For lngindex = 1 To CInt(ComboChildren.Value)
Temps(crtSumm).AutoTextEntries("notice").Insert
Where:=Selection.Range, RichText:=True
Selection.Collapse wdCollapseEnd
next
End If
Next myBookmark
 
A

AW

Drake

I don't know what you're trying to achieve and don't fully
understand the code...never used Auto text entries.
Try something like:
________________________
Dim myBookmark as bookmark
Dim x=integer
for each myBookmark in ActiveDocument.Bookmarks
If Left(myBookmark.Name,4)=bmkA then

Let x=comboChildren.value
For lngindex = 1 To x
myBookmark.Range.text= Temps(crtSumm).AutoTextEntries("notice").
next

end if
next myBookmark
________________________

HTH

Al@n
 
D

Drake

Sorry Alan. Here is what I am trying to do:

The user answers questions in a userform. Depending on the answers to their
questions, a document is populated in different ways. One of the questions
asks the user to choose a number between 2 and 12. I have a series of
bookmarks in my document. For bookmarks A1, A2, and A3, I want to insert
Autotext1 as many times as the number the user choose (between 2 and 12).
For bookmarks B1, B2, and B3, I want to insert Autotext2 as many times as
the number the user choose between 2 and 12. This happens for 3 or 4 types
of bookmarks. I am able to do this with Find/Replace, but thought bookmarks
might help the program run faster.

I tried your suggestion below and the program acts like it doesn't even go
to the bookmarks bmkA1 and bmkA2.

I appreciate any suggestions you have.

Drake
 
P

Peter Hewett

Hi Drake

You've started a new thread, the old one you posted using the nick Dragon. I provided you
with both the code and the answers to your questions - so why are you going over the same
ground again? The code worked, so can you explain the problem? It would have been
preferable for you to have continued with the original thread rather than starting another
on the same topic.

It's not particularly polite to waste people time in this way. If you don't understand it
say so.

HTH + Cheers - Peter
 
D

Drake

Hi Peter. I was not intending to waste anyone's time. It was a new
question, so I thought it best to post it as a new question. The reason for
the name is after I posted the original thread, I read the FAQs that said it
was preferrable if people use their real name rather than nicknames.

Okay, that being said, I understood the code you gave me and I greatly
appreciate it. What that code was not able to do, which was part of my
original question in that thread, was how to get it to treat similarly named
bookmarks in the same way. That was when I asked you about the bookmarks.
As you pointed out when I asked that question of you, bookmarks are unique.
That is why I started the new thread.

Again, not trying to waste anyone's time, but if anyone has any suggestions
or thoughts, please let me know.

Drake
 
P

Peter Hewett

Hi Drake

Following on from the previous thread then...

This code should do what you want and I'm glad you took the time to read the FAQ.

Public Sub InsertAutoTextAtBMs(ByVal strBookmarkName As String, _
ByVal strAutoTextName As String, _
ByVal lngOccurences As Long)
Dim rngLocation As Word.Range
Dim tplAttached As Word.Template
Dim lngIndex As Long
Dim lngBMCount As Long
Dim strBM As String

' This is the template that contains the autotext
Set tplAttached = ActiveDocument.AttachedTemplate

' Generate the first bookmark name
lngBMCount = 1
strBM = strBookmarkName & CStr(lngBMCount)

' Insert AutoText for each bookmark
Do While ActiveDocument.Bookmarks.Exists(strBM)

' This is where the AutoText will be inserted
Set rngLocation = ActiveDocument.Bookmarks(strBM).Range

' Insert the AutoText N times
For lngIndex = 1 To lngOccurences
Set rngLocation = tplAttached.AutoTextEntries(strAutoTextName) _
.Insert(rngLocation, True)
rngLocation.Collapse wdCollapseEnd
Next

' Generate the next bookmark name in the series
lngBMCount = lngBMCount + 1
strBM = strBookmarkName & CStr(lngBMCount)
Loop
End Sub

You pass the code the base bookmark name (the bookmark name without its numeric suffix),
the AutoText Entry name and the number of times the AutoText should be inserted at each
bookmark. Bookmarks MUST be numbered consecutively (e.g.: bmkA1, bmkA2, bmkA3 and bmkA4)
as the code stops as soon as it generates a bookmark name that does not exist.

So call it like this:
InsertAutoTextAtBMs "bmkA", "AutotextEntryA", 2
InsertAutoTextAtBMs "bmkX", "AutotextEntryX", 8

HTH + Cheers - Peter
 
D

Drake

Thanks Peter. I just got back from vacation this morning. This looks
great. I plan to give it a go in the next few days. I'll let you know how
it goes. Thanks again!

Drake
 
D

Drake

Peter - I was finally able to do some work on this this week. Thank you so
much! This code ROCKS! It is perfect!
 

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