Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with Word VBA

Status
Not open for further replies.

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.
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
 
Think about it.

The field in the header is a pointer. It duplicates the attributes and contents of what it points to.

Thus... it looks like, and contains the values of what it is pointing to.

You are asking to take the attributes/values of the formfield, duplicate those elsewhere...and then change them. Well, you can certainly change them, but because the OTHER field justs point to the first one, if you change the first one...the other field changes as well. That is the whole point of REF fields.

My first question is why you are using the formfield in the document in the first place if you do not actually want the formfield to have content? There are many ways to take information and put it places (like into the header).

BTW: you do NOT need to open the header to perform action inside it.
Code:
Option Explicit

Public fldFF As Word.FormField

Sub FillBM(strBM As String, strText As String)
[COLOR=red]' prcoedure that dynamically changes the bookmark content
' value is IN the bookmark, not outside it[/color red]
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End Sub


Private Function CurrentFF() As Word.FormField
[COLOR=red]' gets the current formfield as an object[/color red]
With Selection
    If .FormFields.Count = 1 Then
        ' CheckBox or DropDown
        Set CurrentFF = .FormFields(1)
    ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
        Set CurrentFF = ActiveDocument.FormFields _
        (.Bookmarks(.Bookmarks.Count).Name)
    End If
End With
End Function

Sub myOnExit()
Set fldFF = CurrentFF
With fldFF
   If .Type = wdFieldFormTextInput Then
    [COLOR=red]  ' files the BOOKMARK in the header with
      ' the formfield value[/color red]
      Call FillBM("h_date", CurrentFF.Result)
   End If
[COLOR=red]   ' makes the value blank[/color red]
   .Result = ""
End With
End Sub
The formfield uses Sub myOnExit.

So, on exit, it fills the bookmark h_date (in the header) with the value of the formfield (whatever its name is), and then makes the formfield value blank( "" ).

Gerry
 
My scripting skills are not very good, to say the least. I understand the concept behind the REF field simply being a pointer, thus duplicating the values/attributes of the formfield. I just wasn't sure how to get around it.

Thanks for your help. It works great.

Edward
 
Normally, the REF field would be quite sufficient for what most people want to happen. However, in your case you want to do very different things.

You want another field to REF to the current field...but only temporarily. You want to take the original field value and REF it (so to speak), but then change the original field value. This is not possible, as a REF field does exactly that, reference another field. It is its whole being, so to speak. "Whatever that is, that is what I am."

But you do not want this. You want a location to get the current value of a formfield, THEN change that formfield value.

Thus a REF field is not the route. You are NOT making a reference to the formfield. You are taking its value, and putting it someplace else. It is, in fact, a form of ByVal (by VALUE), not ByRef (by REFERENCE).

A bookmark is a tagged location.

Thus, the logic is:

1. get the value of the formfield.
2. put that value into a location (a bookmark)
3. change the value of the formfield to ""

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top