Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Insert Autotext Header macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Summer - 30 Jun 2007 02:11 GMT
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
====================================
Russ - 30 Jun 2007 03:15 GMT
Summer,
Does it still happen if you use:
rngTmp.Delete
In place of:
rngTmp.Text = ""
???
Or if you comment out that line?

> 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
[quoted text clipped - 70 lines]
> End Sub
> ====================================

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Summer - 01 Jul 2007 08:02 GMT
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.

> Summer,
> Does it still happen if you use:
[quoted text clipped - 85 lines]
>> End Sub
>> ====================================
Russ - 01 Jul 2007 09:41 GMT
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.

> Hi Russ,
>
[quoted text clipped - 112 lines]
>>> End Sub
>>> ====================================

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.