inkserious
Technical User
- Jul 26, 2006
- 67
I'm new to VBA in Word. I've used it some in Excel. I am using {REF h_date \dddd, MMMM d, yyyy} in the header of a word document to get the date from a FormField Bookmarked "h_date" in the body of the document. Once the user enters the date in the formfield, it fires off the code on exit. What I want to happen is for the formfield to clear after the date is set in the header. Obviously, since the header contains a REF to the formfield, printing or print preview, or any other method of updating the fields updates the date in the header. I tried making the font white in the formfield but that also carries over to the header. Ultimately, I don't want the date to show in the body of the document - only in the header.
Any ideas on how to solve this problem, or is there an easier way than what I am doing? I've posted the code below.
Thanks for any help you can offer.
Any ideas on how to solve this problem, or is there an easier way than what I am doing? I've posted the code below.
Thanks for any help you can offer.
Code:
Sub Header_Date()
Application.ScreenUpdating = False
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"
Else
End If
[COLOR=green]'open header, select FormField and refresh with date entered in h_date FormField[/color]
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
[COLOR=green]'Clear Date from h_date FormField[/color]
ActiveDocument.FormFields("h_date").Result = ""
[COLOR=green]'Reprotect Document without resetting any other form fields in the document[/color]
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True ', Password:="aPassword"
Application.ScreenUpdating = True
End Sub