find/replace and bookmarks

D

Dragon

Hi. I have three questions:

1) I have a document that has various places where I need to insert
different autotext. For this type of function, is it better to mark a place
with a word and use Find/Replace for each instance or mark it with a
bookmark and its references? By better, I mean faster and less burdensome
on the program.

2) Is there a direct statement for Replacing a Found item with autotext? In
other words, I don't have text that will fit nicely in the Replacement.Text
= " " line - it needs to be autotext, but I can't figure out how this would
work.

3) At each instance of replacment, the specific autotext will need to be
inserted multiple times (depending on a number the user chooses from a drop
down in a userform). Where I am having problems with this while using
Find/Replace is how to get the program to find each instance of the
particular word (let's say it is "find") and then insert the autotext X
amount of times. I am using a For....Next statement , but it is only
halfway working.

Sorry for the long questions.....thank you for any input.

Dragon
 
P

Peter Hewett

Hi Dragon

It' far easier to do what you want using bookmarks. When it comes to inserting the
AutoText just set up a range object and collapse it to the end of range for each
insertion. Something like this:

Public Sub InsertATMultiples()
Const BOOKMARK_NAME As String = "TestLocation"
Const AUTOTEXT_ENTRY_NAME As String = "TestInfo"

Dim rngLocation As Word.Range
Dim tplAttached As Word.Template
Dim lngIndex As Long

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

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

' Insert the AutoText 3 times
For lngIndex = 1 To 3
Set rngLocation = tplAttached.AutoTextEntries(AUTOTEXT_ENTRY_NAME) _
.Insert(rngLocation, True)
rngLocation.Collapse wdCollapseEnd
Next
End Sub

HTH + Cheers - Peter
 
D

Dragon

Peter,

thank you. One question I have so far - at the end it says "set rnglocation
=" the autotext name, however right before it, it says to set the
rnglocation to a range. I was thinking this might be a type, but wanted to
verify. Also, there is a .insert(rnglocation, true) towards the end, but I
am not sure what the .insert is referring to?

Thanks!
 
P

Peter Hewett

Hi Dragon

Without meaning to be rude you really need to explore Words object model, make sure you've
got the VBA online help files installed. Also use the IDE you can use the Object Browser
(F2) and the Locals pane to see what's going on when you step through the code.

I'll explain about the code that's causing confusion. The " _" at the end of the line
denotes that the code continues on the next line. So the following two lines of code are
in fact just one statement:

Set rngLocation = tplAttached.AutoTextEntries(AUTOTEXT_ENTRY_NAME) _
.Insert(rngLocation, True)

This inserts the specified AutoText entry (AUTOTEXT_ENTRY_NAME) from the specified
template (tplAttached) at the location specified by the Range object rngLocation. The
AutoTextEntries Insert method returns a Range object which maps to the AutoText entry that
was inserted.

HTH + Cheers - Peter
 
D

Dragon

Thanks Peter. I hadn't seen the _ because the newsgroup wrapping had moved
it over to the far left. What you said makes sense. So, I will be setting
rngLocation to tplattached.autotext (etc) in one instance and to
ActiveDocument.Bookmarks(BOOKMARK_NAME).Range in another instance? I have
never re-set an object in a sub before, so I just wanted to verify that it
was rngLocation that I am setting in both instances.

Hope this makes sense.
 
P

Peter Hewett

Hi Dragon

The fact that the Range Object is in a Sub is irrelevant as the object is only declared
and manipulated within the Sub. In other words the variable is local in scope.

The range object rngLocation is set in three separate line of code. It's set to the
bookmark location that defines where the repeated AutoText entries will be inserted. It's
reset to map the AutoText Entry that's inserted. It's collapsed to the end of the last
AutoText entry that was inserted so that it becomes the insertion point where you insert
the same AutoText entry the next time.

Step through the code and in the Immediate window once the range objects been initialised
try something like:
rngLocation.Select

This will cause Word to Select whatever is mapped by the range object so that it's easy
for you to see what's going on. You can then toggle backwards and forwards between Word
and the VBA IDE to step through your code and see what it's actually doing to the
document.

HTH + Cheers - Peter
 
D

Dragon

Peter -

just tested this. It does beautifully with inserting the autotext multiple
times in the one spot. It still needs to input that same autotext in the
other places that have that same bookmark. In other words, BookMarkA
appears in 2 places in the document. BookMarkB appears in 2 places in the
document. AutotextA needs to be inputted a user-specified amount of times
at BookMarkA(instance1) and inputted a user-specified amount of times at
BookMarkA(instance2) - I am using a bookmark reference for this spot. Then,
AutotextB needs to be inputted a user-specified amount of times at
BookMarkB(instance1) and inputted a user-specified amount of times at
BookMarkB(instance2). This happens for 4 different bookmarks. I have
gotten Find to do this, but it is incredibly slow. I am hoping the
bookmarks would be faster.

Any suggestions you have are appreciated. Thank you so much for your time
on this.

Dragon
 
P

Peter Hewett

Hi Dragon

Bookmarks are implicitly unique, you can't have two BookmarkA's. So use something like
BMA1, BMA2 etc.

HTH + Cheers - Peter
 

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