PDA

View Full Version : Using VBA to append text to all footers in Word 2007



Stoo85
11-06-2007, 07:56 AM
Hi there,
I have a problem I am hoping someone can help me with.
I am trying to find a method to add in a persons's domain username to the bottom of every Word document.

So far I have got the code to actually add the text in and I can use the "Auto???" macro names to get it to put the inforamtion when Word opens, a use opens a new document or opens an existing document the problem is that I can't stop it putting in if it already exists.

I had thought about just re-setting the footer every time but that won't work as I don't want to delete anything that people have put in the footer.

I would assume I would need something allong the lines of

[IF DOCUMENT FOOTER CONTAINS] environ("username")
[THEN] do nothing
[ELSE] WordBasic.ViewFooterOnly
Selection.TypeText Text:=Environ("username")
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Color = -603923969
Selection.Font.Size = 10
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
[END IF]

It's just the syntax of the bits in bold I can't work out. What's anoying is that I can do this easilly in Access VBA. Word just confuses me! :(
Any help would be appreciated.

TonyJollans
11-07-2007, 04:37 AM
One way would be ..

If InStr(ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range, Environ("UserName")) > 0 Then

Generally you would be better working with the Range all the time instead of selecting the footer in the UI.

Interesting colour, by the way - is it meant to be barely visible?

Stoo85
11-07-2007, 04:49 AM
Thanks for the advice I'l have a go and see how I get on.

Tho be honest I'm not very good at the Word VBA so had to create that using the "Record Macro" and then do a bit of editing.

Yep it's meant to be small and faded out. Working in a school I need a way to keep track of printing. My print monitoring software is good but has drawbacks and can take a while to track doen who has printed what. At the same time I need to make sure that it's not too obvious for when students are printing posters for display.

Being able to just look at a page then return it to the student in question might make them think about their printing.

fumei
11-07-2007, 03:39 PM
Selection.Font.Color = -603923969

Very interesting colour...as I get an error with that value. "Out of range."

TonyJollans
11-08-2007, 12:29 AM
It's a new colour for 2007 - see here (http://proofficedev.com/blog/2007/08/21/colours-in-word-2007-part-1) for my musings on the subject (also part 2 (http://proofficedev.com/blog/2007/08/21/colours-in-word-2007-part-2))

fumei
11-09-2007, 03:13 PM
Part 2 returns a 404 error.

Good article though in the first one.

Sheeeesssh. Sigh, I still am nowhere up to speed on 2007.

TonyJollans
11-09-2007, 04:24 PM
Sorry - I just changed the name in the url I copied but the date is different as well. Try this http://proofficedev.com/blog/2007/10/10/colours-in-word-2007-part-2