Insert file from form field

Home Up Search This Site What's New? Audio On CDR Favourites Downloadable files Photo Gallery 2002 Photo Gallery 2003 Photo Gallery 2004/5 Photo Gallery 2006/7 Photo Gallery 2008 UK Photo Gallery Ireland Photo Gallery Cats Photo Gallery 

 

 

Google
 

 

There is no charge for using any of the material (for personal use) on this web site, but if you wish to make a contribution to the ever growing running costs, any donation would be much appreciated. Click the adjacent button to access PayPal
Insert text based on choice from a dropdown form field
 

When preparing forms in Word, it would often be desirable to insert a block of text or a complete document based upon the choice offered in a drop-down form field. In the example below, the form field has three choices called Job1, 2 & 3. The actual names of the choices doesn't matter a great deal as long as the appropriate changes are made in the conditional fields.

 

The Word 2007 dialog is almost identical:

 

In this first example, note the exit macro UpdateAllFields. This calls macro code below to force an update of the conditional fields that are used to insert the appropriate document.

 

Sub UpdateAllFields()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
    oStory.Fields.Update
    If oStory.StoryType <> wdMainTextStory Then
        While Not (oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            oStory.Fields.Update
        Wend
    End If
Next
oStory
Set oStory = Nothing
End Sub

 

The conditional fields are all inserted on the same line, allowed to wrap to the margins, with no spaces between them.

The documents to be inserted in this case are named to match the field choices. Note that the path must be included and you must use double slashes '\\' between the elements of the path. The document is placed at the location of the conditional field, thus:

 

A second possibility is to insert the required document using a macro on exit from the field. Here the macro is called DropSelection and the code is shown below:

 

Sub DropSelection()
Dim myrange As Range
ActiveDocument.Unprotect
Set myrange = ActiveDocument.Range
myrange.Collapse wdCollapseEnd
myrange.InsertFile "D:\My Documents\Test\" & _
ActiveDocument.FormFields("DropDown1").DropDown.Value & ".doc"
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
End Sub

 

This macro inserts the required document at the end of the form. The result is much the same. However on this occasion the documents are called 1.doc 2.doc 3.doc and these numbers reflect the order in which the choices are listed in the drop-down list. Thus the First entry will insert 1.doc

Note:

Similar techniques can be used to insert autotext entries if preferred. The use of autotext tends to make things simpler when distributing forms between users because of the issue of everyone having access to the same documents in the same path.