Insert Autotext Header macro

S

Summer

I have a macro for my letter template that lets people update the header.
Part of this is to insert an auto text entry into the first page header. (I
put my auto text entries for all my templates in one global template to make
them easy for the client to find and modify.)

I have noticed that the FIRST TIME I run the code to insert the auto text
entry it DOES NOT INSERT IT. No errors, but no auto text either. But if I
run it a second time -- voila! The auto text appears. After futzing around
with this for hours my current solution is to simply tell it twice to insert
it. And that seems to work.

Does anyone have any idea why it won't insert on the first try?

This is my current code -- I am passing it the autotext entry name,
document, and whether or not I want it done for all sections of the
document. :
================================================
Sub InsFPHeader(sTxt As String, oDoc As Document, Optional bAll As Boolean)
Dim rngTmp As Range, i As Integer, iSec As Integer, iSecs As Integer
If bAll = True Then
iSecs = oDoc.Sections.Count
Else
iSecs = 1
End If
For iSec = 1 To iSecs
'make sure different first page is on.
If oDoc.Sections(iSec).Headers(wdHeaderFooterFirstPage).Exists Then
'this runs a function that returns the index number of my startup template
i = SDLib.RtnTmpltIndex(csStartup)
Set rngTmp =
oDoc.Sections(iSec).Headers(wdHeaderFooterFirstPage).Range
rngTmp.Text = ""
Templates(i).AutoTextEntries(sTxt).Insert _
Where:=rngTmp, RichText:=True
'here I tell it to insert it again and this time it will work.
Templates(i).AutoTextEntries(sTxt).Insert _
Where:=rngTmp, RichText:=True
End If
Next iSec
End Sub

========================================
I also tried writing the code that tells it to get the autotext from the
startup template this way
but the same thing happens:

Sub InsFPHeader(sTxt As String, oDoc As Document, Optional bAll As Boolean)
Dim rngTmp As RangeiSec, iSecs As Integer
Dim oTmplt As Template, sTmpltPath As String

sTmpltPath = Options.DefaultFilePath(wdStartupPath)
If Right(sTmpltPath, 1) <> Application.PathSeparator Then _
sTmpltPath = sTmpltPath & Application.PathSeparator
sTmpltPath = sTmpltPath & csStartup

Set oTmplt = Templates(sTmpltPath)

If bAll = True Then
iSecs = oDoc.Sections.Count
Else
iSecs = 1
End If

For iSec = 1 To iSecs
'make sure different first page is on.
If oDoc.Sections(iSec).Headers(wdHeaderFooterFirstPage).Exists Then
Set rngTmp =
oDoc.Sections(iSec).Headers(wdHeaderFooterFirstPage).Range
rngTmp.Text = ""
oTmplt.AutoTextEntries(sTxt).Insert Where:=rngTmp, RichText:=True
End If
Next iSec
End Sub
====================================
 
R

Russ

Summer,
Does it still happen if you use:
rngTmp.Delete
In place of:
rngTmp.Text = ""
???
Or if you comment out that line?
 
S

Summer

Hi Russ,

Thanks.

This is really strange...
rngTmp.Delete didn't work, but commenting out the whole rngtmp.Text ="" line
did. Sorta.

It works fine each time I create a new letter (it inserts the logo and
return address in the letterhead). But if I run it to UPDATE an existing
letter -- KABOOM.

I think my logo image may be the problem. If I run it to insert the return
address without the logo, no problem. But with the logo, it does not like
that. (Maybe it has trouble replacing a range with auto text that holds an
image?)

I did get a corrupt image file once where someone's logo crashed Word 2003
(but not 2002) every time it was inserted. I got a different logo and it
was fine.

In this case though, I can insert the logo the first time.

Any ideas why that might be?

Many thanks for the assist.
 
R

Russ

Summer,
Did you try the combination of rngTmp.Delete while rngtmp.Text ="" was
commented out? Did you step through the code and see if rngTmp.Delete got
rid of everything in the header that already had something in it?
Is the logo a floating picture that is anchored or inline picture that acts
like a character and moves with new text? Another way to anchor pictures is
to put both picture(s) and text(s) in different cells of an invisible table
grid.

The kaboom probably was trying to insert the autotext while there was still
something there.
 

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