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
====================================
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
====================================