J. Steinblock
2005-10-05 16:28:43 UTC
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.
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.