How do I put this into a Footer

O

Office_Novice

I would like to insert the UserName FormField into footer programmatically.

this code works but can i get it into a footer?

Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldUserName)
 
J

Jean-Guy Marcil

Office_Novice said:
I would like to insert the UserName FormField into footer programmatically.

this code works but can i get it into a footer?

Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldUserName)

It depends on the structure of your document.
There are three types of footers, and your document may have more than one
section.

Let's assume you want that in the main footer of the first section:

Option Explicit

Sub InsertFieldFooter()

Dim myField As Field
Dim rgeFooter As Range

Set rgeFooter =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range

With rgeFooter
.Collapse wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=rgeFooter, _
Type:=wdFieldUserName)
End With

End Sub
 
F

fumei via OfficeKB.com

1. "Username FormField" - your code inserts a Field, not a FormField. A
formfield is used for user input.

2. "can i get it into a footer" Yes. You do not state WHAT footer, so I
will use the Primary footer of Section 1.

ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _
.Range.Fields.Add _
Range:=ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary) _
.Range, _
Type:=wdFieldUserName

This could certainly be written shorter, by using a range object (and numeric
constants), like:

Dim r As Range
Set r = ActiveDocument.Sections(1).Footers(1).Range
r.Fields.Add Range:=r, Type:=60

Both code snippets do exactly the same thing. They REPLACE the footer
content.with the UserName field. If you need to put the field within
existing content of the footer, that is another - not too difficult - issue.

Do you need the myField as an object?
 
O

Office_Novice

No i dont need the MyField as object. Both of these were very useful thanks
guys.
 

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