Insert autotext at bookmark

S

Sammy

Hello:

Surprise! My macro doesn't work. I have three autotext entries named
Text1, Test2 and Test3. I have three bookmark locations in my document,
bkHere1, bkHere2 and bkHere3. I could probably make this happen by going to
the bookmark and inserting the autotext but I wanted to try using the Range
object instead.

As you can see I'm trying to define the ranges and then insert the autotext.
In my final macro the user will be able to select one, two or all three of
the autotext entries from a dialog box (I have this part of the macro). The
autotext entries need to come in order, regardless of which ones they select
(so if they choose 2 and 3 they need to appear in that order).

Can you help? THANKS!


Sub InsertAutoTextEntry()
Dim rngDoc1 As Range
Dim rngDoc2 As Range
Dim rngdoc3 As Range

Set rngDoc1 = ActiveDocument.Range.Bookmarks("bkHere1")
Set rngDoc2 = ActiveDocument.Range.Bookmarks("bkHere2")
Set rngdoc3 = ActiveDocument.Range.Bookmarks("bkHere3")

ActiveDocument.AttachedTemplate.AutoTextEntries("test1").Insert _
Where:=rngDoc1, RichText:=True

End Sub
 
J

Jean-Guy Marcil

Sammy said:
Hello:

Surprise! My macro doesn't work. I have three autotext entries named
Text1, Test2 and Test3. I have three bookmark locations in my document,
bkHere1, bkHere2 and bkHere3. I could probably make this happen by going to
the bookmark and inserting the autotext but I wanted to try using the Range
object instead.

As you can see I'm trying to define the ranges and then insert the autotext.
In my final macro the user will be able to select one, two or all three of
the autotext entries from a dialog box (I have this part of the macro). The
autotext entries need to come in order, regardless of which ones they select
(so if they choose 2 and 3 they need to appear in that order).

Can you help? THANKS!


Sub InsertAutoTextEntry()
Dim rngDoc1 As Range
Dim rngDoc2 As Range
Dim rngdoc3 As Range

Set rngDoc1 = ActiveDocument.Range.Bookmarks("bkHere1")
Set rngDoc2 = ActiveDocument.Range.Bookmarks("bkHere2")
Set rngdoc3 = ActiveDocument.Range.Bookmarks("bkHere3")

ActiveDocument.AttachedTemplate.AutoTextEntries("test1").Insert _
Where:=rngDoc1, RichText:=True

End Sub

In your code, you are trying to insert "test1", but above you wrote that it
is called "text1"
Also, in your code, you are Dimming a range, but when you set it, you are
not explicitly using the range property.
Finally, when posting such a message, it would be extremely helpful to
describe the observed behaviour. Just stating "My macro does not work" is not
very helpful.

Next time, describe the observed results, if any.
If none, you must be getting error messages or the code must stop on a
particular line when you debug.
Let us know about those error messages or the code line that is highlighted
by the debugger.

For now, try this version of your code, it works here on Word 2003:


Sub InsertAutoTextEntry()

Dim rngDoc1 As Range

Set rngDoc1 = ActiveDocument.Range.Bookmarks("bkHere1").Range

ActiveDocument.AttachedTemplate.AutoTextEntries("test1").Insert _
Where:=rngDoc1, RichText:=True

End Sub
 
S

Sammy

Sorry about that, I forgot to give that info.

Your solution worked perfectly. Thanks for your time.
 
G

Gordon Bentley-Mix

Sammy,

I do this sort of things regularly in several templates, so I've written a
"generic" procedure that accepts arguments for the bookmark and AutoText
entry names, which I call whenever I need to insert an AutoText entry at a
particular bookmark location. It looks like this:

Public Sub InsertATEntry(BkmkName As String, ATEntryName As String, Optional
bRestoreBkmk As Boolean = False)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) = True Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
On Error GoTo ATEError
.AttachedTemplate.AutoTextEntries(ATEntryName).Insert myRange,
True
If bRestoreBkmk = True Then .Bookmarks.Add BkmkName, myRange
Else: Msgbox "Cannot find the bookmark " & BkmkName & "."
End If
End With
Exit Sub

ATEError:
MsgBox "The required AutoText entry " & ATEntryName & " could not be
found." , vbCritical, "AutoText Error"
End Sub

Calling it is simply a matter of doing something like this:

InsertATEntry "Bookmark1", "AutoText1", True

This eliminates the need to create several lines of duplicate code each time
you want to insert an AutoText entry. Also, because of the structure of the
procedure, IntelliSense will prompt you for the arguments so you don't miss
anything out.

Note that the code also contains functionality to (optionally) "restore" the
bookmark after the AutoText entry is inserted. It also has a bit of error
handling to make sure that the bookmark actually exists before trying to
insert the AutoText entry, and to warn the user if the bookmark or AutoText
entry cannot be found - but if I've done my job correctly, these only come
into play during debugging.

BTW, if you decide to use this code, please watch the line breaks introduced
by the newsgroup editor.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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