Adding text to specifc section of a footer

J

Jon Rowland

Hi this is my first post to this forum and hope someone can help!!
(Will be using this on Word97)

What I am trying to do is as follows:

Upon Print/Print Preview I wish the network User ID into my document
footer. (This I have managed)
More specfically I wish to add to exsisting footer text. I have a 3x3
table and wish to add this data into cell C3 (ie bottom right) (This I
can't manage) and I would be gratful if anyone can assit.

Below is the code I have managed to pull together (Just the
PrintPreview bit to save space).


Sub FilePrintPreview()
'
' FilePrintPreview Macro
' Displays full pages as they will be printed
'
ActiveDocument.PrintPreview

strUser = LCase(Environ("username"))

' Open Header/Footer view

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
End If

' Add Footer Text
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection

Many thanks
Jon
 
J

Jay Freedman

Hi Jon,

In Word, working with the Selection (which simultaneously moves the
insertion point in the document) is the worst possible choice,
especially when working in headers and footers. The fact that this
garbage is what the macro recorder supplies is nothing short of
criminal.

Use the power of the Word object model to make life simpler! This is
all the code you need:

Sub FilePrintPreview()
Dim oRg As Range
Dim strUser As String

strUser = LCase(Environ("username"))

Set oRg = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary) _
.Range.Tables(1).Cell(1, 3).Range

oRg.Text = strUser

ActiveDocument.PrintPreview
End Sub

The Set statement works like this: Choose the first (probably the
only) section in the active document. Choose the primary footer of
that section (as opposed to the first-page footer or the even-page
footer, if they exist). Within the range of that footer, choose the
first (presumably the only) table. Within that table, choose the cell
in row 1 / column 3. Assign the range of that cell to the Range
variable oRg.

Then it remains only to assign the strUser variable to the .Text
property of the chosen Range.
 
N

news.btinternet.com

Thanks Jay,

Tried this but bit

Set oRg = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary) _
.Range.Tables(1).Cell(1, 3).Range

causes a compile error=syntax error, I think it is down to the _ but don't
know how why it causes the error, so can't fix it

Thanks

Jon
 
N

news.btinternet.com

my error...forgot to add the space...doh!!!...Thanks v much works a treat.

Jon
 

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