Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Calgary, Alberta, Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    merging into a form with text fields (97/SR1)

    We have a form with text fields, and need to perform a mail merge with it. Of course, we have had to unprotect the form in order to perform the merge, which then works fine. However, once merged, the text fields have disappeared. Is there a solution to this? In other words to keep the text fields AND also perform a merge???

  2. #2
    Star Lounger
    Join Date
    Mar 2001
    Location
    St Georges du Bois, Pays de la Loire
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    You could try building the whole thing with a macro. We have a letter that uses a mailmerge from an Access database and writes the letter text as it goes. If you were to record a macro adding the fields you could add this in at the end of the merge.

    I have attached our letter macro to this post. It's very specialised but it does 2 merges and inserts mailmerge fields so you might be able to make use of bits of it. Very clumsy scripting I'm afraid but I'm still on the learning curve.

    David
    Attached Files Attached Files

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    I'm not sure I can help, as I'm a little unclear as to what you're doing. When you say you need to perform a mail merge with your form, are you using the form as a main document or as a datasource? If the latter, could you be more specific as to how it's laid out. Is there a table in it with variable data? Perhaps you could attach a sample to your post.
    Cheers,

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    If you want to keep the values in the FormFields after you unprotect the form, you will need to run a macro to reprotect the Form. It only needs to be 1 line:

    ActiveDocument.Protect Type, NoReset, Password

    There are 3 parameters to the Protect Method.
    Type - is required and indicates the type of Protection (wdAllowOnlyFormFields to Protect for Forms)

    NoReset - is optional and indicates if the FormFields should be reset to default values or retain their current value (True = Retain Current Value False = Reset to default)

    Password - is optional and is the protection password

    So assuming that you do not have a password on you form, the actual line would look like:

    ActiveDocument.Protect wdAllowOnlyFormFields,True
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  5. #5
    Lounger
    Join Date
    Jan 2001
    Location
    Calgary, Alberta, Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    Phil - The form is the main document and has text fields in it. Once merged (with thousands of data records), the text fields remaining are to be filled in on an individual basis. Hope this clarifies.

  6. #6
    Lounger
    Join Date
    Jan 2001
    Location
    Calgary, Alberta, Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    Thanks - I will try this.

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: merging into a form with text fields (97/SR1)

    You can't merge form fields. That is, when you perform the merge, the form fields themselves are not merged, so merely trying to reprotect the form without resettting form fields shouldn't make a difference--there are no form fields left in the merged document. However, I copied the following tip from somewhere (I thought it was MS KB, but I'm not sure). Hope this helps.

    MailMerge: Text Form Fields Are Not Retained
    Word unlinks the text form fields during the mail merge. Word does not unlink display form fields, such as the drop-down and the check box form fields.

    You can work around the problem by using the following sample macros to:

    1. Replace text form fields in the main mail merge document with placeholders while preserving the contents of the form fields.
    2. Merge to a new document.
    3. Replace the placeholders with text form fields and restore the contents of the form fields.
    4. Restore the main mail merge document to its original content prior to running the macro.

    NOTE: These two macros work in conjunction with one another, and both macros must be created in the same Visual Basic for Applications project. The first macro will call the second macro.

    Macro 1:

    <pre>Sub PreserveMailMergeFormFields()
    Dim fFieldText() As String
    Dim iCount As Integer
    Dim fField As FormField
    Dim sWindowMain, sWindowMerge As String
    On Error GoTo ErrHandler

    ' Store Main merge document window name.
    sWindowMain = ActiveWindow.Caption

    ' Because the document contains form fields,
    ' it should be protected so, unprotect document.
    If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect
    End If

    ' Loop through all text form fields
    ' in the main mail merge document.
    For Each aField In ActiveDocument.FormFields

    ' If the form field is a text form field...
    If aField.Type = wdFieldFormTextInput Then

    ' Redim array to hold contents of text field.
    ReDim Preserve fFieldText(1, iCount + 1)

    ' Place content and name of field into array.
    fFieldText(0, iCount) = aField.Result
    fFieldText(1, iCount) = aField.Name

    ' Select the form field.
    aField.Select

    ' Replace it with placeholder text.
    Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

    ' Increment icount
    iCount = iCount + 1

    End If

    Next aField

    ' Perform mail merge to new document.
    ActiveDocument.MailMerge.Destination = wdSendToNewDocument
    ActiveDocument.MailMerge.Execute

    ' Find and Replace placeholders with form fields.
    doFindReplace iCount, fField, fFieldText()

    ' Protect the merged document.
    ActiveDocument.Protect Password:="", NoReset:=True, Type:= _
    WdAllowOnlyFormFields

    ' Get name of final merged document.
    sWindowMerge = ActiveWindow.Caption

    ' Reactivate the main merge document.
    Windows(sWindowMain).Activate

    ' Find and replace placeholders with form fields.
    doFindReplace iCount, fField, fFieldText()

    ' Reprotect the main mail merge document.
    ActiveDocument.Protect Password:="", NoReset:=True, Type:= _
    WdAllowOnlyFormFields

    ' Switch back to the merged document.
    Windows(sWindowMerge).Activate

    ErrHandler:
    End Sub</pre>


    Macro 2:

    <pre>Sub doFindReplace(iCount As Integer, fField As FormField, _
    fFieldText() As String)

    ' Go to top of document.
    Selection.HomeKey Unit:=wdStory

    ' Initialize Find.
    Selection.Find.ClearFormatting
    With Selection.Find
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False

    ' Loop form fields count.
    For i = 0 To iCount

    ' Execute the find.
    Do While .Execute _
    (FindText:="<" & fFieldText(1, i) _
    & "PlaceHolder>") = True

    ' Replace the placeholder with the form field.
    Set fField = Selection.FormFields.Add _
    (Range:=Selection.Range, Type:=wdFieldFormTextInput)

    ' Restore form field contents and bookmark name.
    fField.Result = fFieldText(0, i)
    fField.Name = fFieldText(1, i)
    Loop

    ' Go to top of document for next find.
    Selection.HomeKey Unit:=wdStory

    Next
    End With
    End Sub</pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •