Retaining Bookmarks in Autotext Macros

M

Mickey

I created a macro (with the wonderful help of Stefan Blom) that calls an
autotext (Smokey) in the header of each section of the document. I basically
added bookmarks to a header and made it an autotext. Problem is that the
first header loses the bookmarks after the next header is populated. Any
suggestions on how to have all section headers retain the bookmarks that are
in the autotext??? Is there a better way that includes code that I could
have created the autotext???

Thanks,
Mickey
 
T

Tony Jollans

Bookmarks must have unique names. If you add a bookmark which has a
duplicate bookmark name, the previously existing one will be deleted. If you
have the bookmark in an Autotext entry then it will only be retianed in the
latest insertion. Basically you can't add bookmarks that way and must add
each individually after inserting the autotext.
 
M

Mickey

Thanks Tony. Do you know if it's possible to write another macro that can
update the headers (that lost the original bookmark) with a cross reference
to the bookmark?

Thanks,
Mickey
 
T

Tony Jollans

Why not just have two autotexts - one with the bookmark and one with a cross
reference?
 
M

Mickey

How would I code it to put one autotext in the first header and the other
autotext with the cross-reference in the subsequent headers. Do you know the
code for cross-references?

Thanks so much for your help and time.
 
T

Tony Jollans

How are you coding it now? How many (different) headers have you got - in
fact, thinking about it, why have you got lots of different headers at all
if you want the same content in each?

You don't need code for the cross reference - just create it (manually) and
save as an autotext in the same way you have (presumably) done for your
bookmark.
 
T

Tony Jollans

I've just realised you're running two threads with this problem - it must be
as confusing for you as it is for me.

If you have two different Autotexts - Smokey and Smokey2, say - one with a
bookmark and one with a cross-reference to it, you should be able to change
Stefan's code along these lines:

Sub AddToMainHeader2()
Dim r As Range
Dim s As Section

For Each s In ActiveDocument.Sections
Set r = s.Headers(wdHeaderFooterPrimary) _
.Range.Paragraphs(1).Range

With r
.ParagraphFormat.TabStops.ClearAll
.ParagraphFormat.TabStops(InchesToPoints(1.25)) _
.Alignment = wdAlignTabLeft
.ParagraphFormat.TabStops(InchesToPoints(6.5)) _
.Alignment = wdAlignTabRight

.Collapse wdCollapseStart
.InsertAfter vbTab
.InsertAfter vbTab
.Collapse wdCollapseEnd

If s.Index = 1 then
NormalTemplate.AutoTextEntries("Smokey").Insert _
Where:=r, RichText:=True
Else
NormalTemplate.AutoTextEntries("Smokey2").Insert _
Where:=r, RichText:=True
End If

End With
Next s

End Sub
 
M

Mickey

Works Great. Thank you so much. I can't begin to express how grateful I am.
Again, thank you.

Mickey
 

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