Automaticaly change field in Word

S

s3cardi

Hi,

I want to execute a VBA Macro each time a user print a
Word document using Microsoft Word2000. This macro must
replace all the field "PRINTDATE" by the
field "CREATEDATE" before the document was printed.

Is it possible?
If you have other idea (certainly best) to replace all
field "PRINTDATE" by the field "CREATEDATE" each time a
user access a Word document, I'm interested too.

Thank in advance for your help.

Patrick.
 
D

Dave Lett

Hi Patrick,

You will want to intercept Word's print commands (see "Intercepting events
like Save and Print"at
http://www.mvps.org/word/FAQs/MacrosVBA/InterceptSavePrint.htm) and insert
something like the following before printing the document:

Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldPrintDate Then
oFld.Code.text = Replace(oFld.Code.text, "PRINTDATE", "CREATEDATE")
End If
Next oFld

HTH
 
M

Martin Sägesser

Hi Patrick

when you show the field-codes (ALT+F9) you can use search and replace
for your problem. I recorded a macro and modified it a little bit:

'---
Sub Print2Create ()
ActiveWindow.View.ShowFieldCodes = True
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = "PRINTDATE"
.Replacement.Text = "CREATEDATE"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
.Find.Execute Replace:=wdReplaceAll
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

then you have to modify the Print commands:

Sub FilePrintDefault()
Print2Create
Dialogs(wdDialogFilePrint).Execute
End Sub

Sub FilePrint()
Print2Create
Dialogs(wdDialogFilePrint).Show
End Sub


hth, Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top