|
|
|
date: Wed, 05 Oct 2005 18:28:43 +0200,
group: microsoft.public.word.vba.addins
back
Code Example: Create a custom footer for all employees in a corperate Network
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.
date: Wed, 05 Oct 2005 18:28:43 +0200
author: J. Steinblock
Re: Code Example: Create a custom footer for all employees in a corperate Network
How about creating your footer with fields as an autotext entry and simply
having your macro insert the AutoText entry?
--
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.
"J. Steinblock" wrote in message
news:opsx6ld50byb3ui4@pc10006a.kesseboehmer.local...
> 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.
date: Wed, 5 Oct 2005 08:59:20 -0500
author: Charles Kenyon
Re: Code Example: Create a custom footer for all employees in a corperate Network
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.
--
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.
"J. Steinblock" wrote in message
news:opsx6l5ta5yb3ui4@pc10006a.kesseboehmer.local...
> 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) isnt
> 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...
>
>
>
>
> On Wed, 5 Oct 2005 08:59:20 -0500, Charles Kenyon
> wrote:
>
>> How about creating your footer with fields as an autotext entry and
>> simply
>> having your macro insert the AutoText entry?
>
date: Wed, 5 Oct 2005 12:14:26 -0500
author: Charles Kenyon
|
|