Macro to add document path and filename to footer on first page on

B

Beach Lover

A user has asked me to write a macro that will add the document path and
filename to the footer of the first page only in specific
documents....doesn't want it in all documents or I could make a template.

He would also like to have an icon to click to run the macro.

I have been able to get the macro to put the document path and file name in
the footer but can't figure out how to get it ONLY on the first page. I am
checking the option "different first page" but it's putting the footer on all
pages.

Does anyone know how to do this?
 
G

Graham Mayor

Without knowing what is already in the footer on page 1 and what to do with
the existing content, in relation to the additional information, it is hard
to be specific, but try the following.

Dim fFname As String
Dim sText As String
With ActiveDocument
If Len(.Path) = 0 Then
.Save
End If
fFname = .FullName
If .Sections(1).PageSetup.DifferentFirstPageHeaderFooter = 0 Then
sText = .Sections(1).Footers(wdHeaderFooterPrimary).Range.Text
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
Else
sText = .Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text
End If
.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text _
= sText & fFname
End With

If there is no footer content, then it can be simplified

Dim fFname As String
With ActiveDocument
If Len(.Path) = 0 Then
.Save
End If
fFname = .FullName
If .Sections(1).PageSetup.DifferentFirstPageHeaderFooter = 0 Then
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
End If
.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text _
= fFname
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Beach Lover

I used your second option (no footer content) and it worked! Thanks!

Do you know how a person would do this using the "record maco" option where
you click on things to make the macro? That's what I couldn't get to work.
For someone who doesn't know the information you provided, the "click" method
is what I need so I can "do it on my own."
 
G

Graham Mayor

The macro recorder is useful but limited. This sort of thing is a stretch
too far.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Beach Lover

Thanks. I certainly appreciate the info.

Graham Mayor said:
The macro recorder is useful but limited. This sort of thing is a stretch
too far.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Beach Lover

Now the person would like the footer info to be right justified. I've tried
to do this, but can't get it right. Can you advise how to do this?
 
G

Graham Mayor

I have added a number of formatting options - to address the OPs next
requirements ;)

Dim fFname As String
Dim oRng As Range
With ActiveDocument
If Len(.Path) = 0 Then
.Save
End If
fFname = .FullName
If .Sections(1).PageSetup.DifferentFirstPageHeaderFooter = 0 Then
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
End If
Set oRng = .Sections(1).Footers(wdHeaderFooterFirstPage).Range
With oRng
.Text = fFname
.ParagraphFormat.Alignment = wdAlignParagraphRight
'.Font.name = "Arial"
'.Font.Italic = True
'.Font.Size = 8
End With
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Beach Lover

This worked perfectly. Thank you so much!

Graham Mayor said:
I have added a number of formatting options - to address the OPs next
requirements ;)

Dim fFname As String
Dim oRng As Range
With ActiveDocument
If Len(.Path) = 0 Then
.Save
End If
fFname = .FullName
If .Sections(1).PageSetup.DifferentFirstPageHeaderFooter = 0 Then
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
End If
Set oRng = .Sections(1).Footers(wdHeaderFooterFirstPage).Range
With oRng
.Text = fFname
.ParagraphFormat.Alignment = wdAlignParagraphRight
'.Font.name = "Arial"
'.Font.Italic = True
'.Font.Size = 8
End With
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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