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.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>Hi this is my first post to this forum and hope someone can help!!
>(Will be using this on Word97)
[quoted text clipped - 46 lines]
>Many thanks
>Jon
news.btinternet.com - 30 Mar 2005 13:09 GMT
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
> Hi Jon,
>
[quoted text clipped - 88 lines]
> >Many thanks
> >Jon
news.btinternet.com - 30 Mar 2005 16:09 GMT
my error...forgot to add the space...doh!!!...Thanks v much works a treat.
Jon
> Hi Jon,
>
[quoted text clipped - 88 lines]
> >Many thanks
> >Jon