Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

Code Example: Create a custom footer for all employees in a corperate Network

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J. Steinblock - 04 Oct 2005 17:31 GMT
Ok,

because I had spend a couple of houres with this topic and I think I am  
not the first or last person who is interested in this, I will post a  
little Howto:

The task was to create an identical footer in every new created  
word-document in our company (1.600 employees) with the following  
Information:
- UserName
- Creation Date of the Document
- Printdate
- Document Name
- Pagecount

The main problem is that you never ever should replace the normal.dot,  
because custom macros and the user settings will be lost.
The Code how to handle global events comes from this page:
http://www.word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm
(thanks to Jonathan West for the link)

How it works:
Create a new document and save it as a *.dot file
Follow the instructions on the page linked above to handle the global  
events in word.

Add the following code to the PsuedoAutoNew Sub

Private Sub PsuedoAutoNew()
    If LCase(ActiveDocument.AttachedTemplate.Name) = "normal.dot" Then
        Application.ScreenUpdating = False
        WriteDocumentFooter
        Application.ScreenUpdating = True
    End If
On Error GoTo 0
End Sub

WriteDocumentFooter is a Sub that fills the footer with content. Disable  
ScreenUpdating seems not to work in Word 2000 (maybe later versions, I had  
no change to test it.) so you see the document move to the bottom of the  
page and then to the top again within a few milisecs.

Add this sub to your code:
Private Sub WriteDocumentFooter()
    ' Go to PageFooter
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

    ' Draw a table
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,  
NumColumns:= _
        3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed

    ' Add Createdate
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "CREATEDATE \@ dd.MM.yyyy", PreserveFormatting:=True
    Selection.MoveRight Unit:=wdCell

    ' Add document name
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "FILENAME ", PreserveFormatting:=True
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.MoveRight Unit:=wdCell

    ' Add Page Count (Page X of Y)
    Selection.TypeText Text:="Seite "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "PAGE ", PreserveFormatting:=True
    Selection.TypeText Text:=" von "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "NUMPAGES ", PreserveFormatting:=True
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
    Selection.MoveRight Unit:=wdCell

    ' Add Username
    ' If you do not maintaine the word user and company names (like us :-)  
you could write a little function to get the name from AD for example.
    ' In this case you can use the code: Selection.TypeText  
Text:=GetUserNameFromAD
    ' (GetUserNameFromAD must be a Function that returns the username
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "USERNAME ", PreserveFormatting:=True
    Selection.MoveRight Unit:=wdCell

    ' Empty Row
    Selection.MoveRight Unit:=wdCell

    ' This field will be empty until you print the first time, than the  
text will be: "printed: 01.01.2111
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
        PreserveFormatting:=False
    Selection.TypeText Text:="IF "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
        PreserveFormatting:=False
    Selection.TypeText Text:="PRINTDATE \@ ddMMyyyy"
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeText Text:="=""00000000"" """" "
    Selection.TypeText Text:=""""
    Selection.TypeText Text:="printed: "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
        PreserveFormatting:=False
    Selection.TypeText Text:="PRINTDATE \@ dd.MM.yyyy"
    Selection.TypeText Text:=""""
    Selection.Fields.Update
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

    ' Go back to the top of the main-document
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.MoveUp Unit:=wdScreen, Count:=1
    Selection.MoveUp Unit:=wdScreen, Count:=1
End Sub

This should produce a table at the bottom of the page that should look  
like this:
------------------------------------------------------------------
| 21.09.2005         |    DocName.doc     | Page 3 of 5          |
------------------------------------------------------------------
| Boll Gotes         |                    | printed: 04.10.2005  |
------------------------------------------------------------------

If you prefer a different formatting for the date search the string  
"CREATEDATE \@ dd.MM.yyyy" and change the "dd.MM.yyyy" to whatever you  
like (search the vba-help for the format function. Should be exactly the  
same syntax.
J. Steinblock - 04 Oct 2005 17:50 GMT
A little addition:

to make sure every user gets this template I used a network share and  
configured our logon-script to add following registry-value:
HKCU\Software\Microsoft\Office\9.0\Word\Options\STARTUP-PATH=x:\path\to\add-in\folder

If you run a different version of word (we use Office 2000) you have to  
use 10.0 (XP) 11.0 (2003) or something different for minor versions.

If you want this to work for notebook-users you have to make this folder  
offline available.

I made something similar in excel (It is a little bit easier, because you  
have more events in excel). If someone is interested in the code, please  
request here. (do not reply to my e-mail, this is only a spam-account.
Charles Kenyon - 05 Oct 2005 14:59 GMT
How about creating your footer with fields as an autotext entry and simply
having your macro insert the AutoText entry?
Signature

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

> Ok,
>
[quoted text clipped - 125 lines]
> like (search the vba-help for the format function. Should be exactly the
> same syntax.
J. Steinblock - 05 Oct 2005 17:45 GMT
That was exactly what I did. I recorded a macro and got the resulting vba  
source code.
Look here:
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,  
Text:= _
        "CREATEDATE \@ dd.MM.yyyy", PreserveFormatting:=True
    Selection.MoveRight Unit:=wdCell

That is what word returns if I add the fild CREATEDATE.

The Autotext with the Printdate (Invisible if not printed yet) isn´t  
recorded correctly so I had have to patch the code a little.
Fell free to record your own macro for the footer and replace it with my  
code for yourself...

> How about creating your footer with fields as an autotext entry and  
> simply
> having your macro insert the AutoText entry?
Charles Kenyon - 05 Oct 2005 18:14 GMT
You misunderstand me. How about creating one AutoText entry that has all of
your fields. Then use a macro to insert that single entry. You can keep the
AutoText entry in a global template. See
http://addbalance.com/word/movetotemplate.htm for step-by-step instructions
on moving / sharing / copying / backing-up customizations including
AutoText, AutoCorrect, keyboard assignments, toolbars, macros, etc.
Signature


Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

> That was exactly what I did. I recorded a macro and got the resulting vba
> source code.
[quoted text clipped - 14 lines]
>> simply
>> having your macro insert the AutoText entry?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.