How do I prevent macro from replacing entire footer?

M

Myoshia

I currently have a macro that pulls in external information into the
footer(s) of my document and thus far, the information comes over just fine.
However, I am not familiar enough with VBA to:

1. make sure that the info appears in EVERY footer (whether the section is
set to Different First Page or not)
2. make sure that if there is existing text in my footer (most commonly a
page number), that it doesn't get wiped out (I'm assuming by the .range).

Again, I'm not familiar enough with VBA to tell it to only remove that which
is beyond the first line (which is quite often where the page number
appears). Here's the code I am currently using:

Dim objRange As Word.Range
Dim objFont As Word.Font

Set objRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
Set objFont = objRange.Font

objRange.Style = wdStyleFooter
objRange.Text = strTextString

With objFont
.Size = 8
End With

If ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Exists =
True Then

ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text _
= strTextString
End If

Set objRange = Nothing
Set objFont = Nothing
End Sub

I hope this is not confusing. Please advise and thanks in advance.
 
D

Doug Robbins

I would have placed a { Docvariable "varname" } field into each of the
footers and the used code to set the value of the document variable and
then, the quick and dirty way to update the display of the information is to
use

ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview

The alternative code to do the updating is

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count
.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields.Update
.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields.Update
Next i
End With



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg

You might need to refine this a bit but it might suit your needs.

Sub Test1()
Dim myRng As Range
For Each myRng In ActiveDocument.StoryRanges
If myRng.StoryType = 8 Or myRng.StoryType = 9 Or myRng.StoryType = 11
Then
myRng.Collapse Direction:=wdCollapseEnd
myRng.InsertAfter vbCr & "Your text here"
While Not (myRng.NextStoryRange Is Nothing)
Set myRng = myRng.NextStoryRange
myRng.Collapse Direction:=wdCollapseEnd
myRng.InsertAfter vbCr & "Your text here"
Wend
End If
Next
End Sub
 
G

Greg Maxey

Or this version:

Sub InsertTextAfterCurrentTextInFooters()
Dim myRng As Range
For Each myRng In ActiveDocument.StoryRanges
Select Case myRng.StoryType
Case Is = 8, 9, 11
Do
myRng.Collapse Direction:=wdCollapseEnd
myRng.InsertAfter vbCr & "Your text here"
Set myRng = myRng.NextStoryRange
Loop Until myRng Is Nothing
Case Else
'Do nothing
End Select
Next
End Sub
 
M

Myoshia

I was able to piece together a litlle from each response and it worked like a
charm. I thank each of you (Doug and Greg) for all of your help. This
newsgroup is so very helpful.
 

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