Form data to MS Word & protecting the form template

mixup

Registered User.
Local time
Today, 14:35
Joined
Jul 29, 2005
Messages
34
Thanks to some of the threads here, I have managed to automate inputting form data from a record into bookmarks in a word document template (a letter). What I am trying to do is to lock the letter allowing modification only to the form fields in that template. If the original template is locked (allowing entry only in the fields), the data does not flow through. So I was trying to accomplish this through the "ProtectedForForms" property. However, it does not work. The letter gets locked before the information flows through. Here's the code I am trying to use (borrowed from one of the users here):

Private Sub cmd_letWarn_Click()

' Check for empty fields and unsaved record.
If IsNull(occupant) Then
MsgBox "Occupant Name cannot be empty"
Me.occupant.SetFocus
Exit Sub
End If

If IsNull(propad_no) Then
MsgBox "Building Number cannot be empty"
Me.propad_no.SetFocus
Exit Sub
End If


If IsNull(prop_ZIP) Then
MsgBox "ZIP Code cannot be empty"
Me.prop_ZIP.SetFocus
Exit Sub
End If

If Me.Dirty Then
If MsgBox("Record has not been saved. " & Chr(13) & _
"Do you want to save it?", vbInformation + vbOKCancel) = vbOK Then
DoCmd.RunCommand acCmdSaveRecord
Else
Exit Sub
End If
End If

' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String

' Specify location of template
strTemplateLocation = "T:\Planning\Planning\EnforcementLog\suppfiles\tempwarn.dot"


On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler


WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False

' Replace each bookmark with field contents.
With WordApp.Selection


.Goto what:=wdGoToBookmark, Name:="ownername"
.TypeText [occupant]

.Goto what:=wdGoToBookmark, Name:="bnum"
.TypeText [propad_no]

.Goto what:=wdGoToBookmark, Name:="stname"
.TypeText [propad_street]

.Goto what:=wdGoToBookmark, Name:="zipcode"
.TypeText [prop_ZIP]

.Goto what:=wdGoToBookmark, Name:="pbnum"
.TypeText [propad_no]

.Goto what:=wdGoToBookmark, Name:="pstname"
.TypeText [propad_street]

.Goto what:=wdGoToBookmark, Name:="ppn"
.TypeText [parcel_no]

.Goto what:=wdGoToBookmark, Name:="ordinance"
.TypeText [code_sections]

.Goto what:=wdGoToBookmark, Name:="orddesc"
.TypeText [complaint_typ]

.Goto what:=wdGoToBookmark, Name:="ownername2"
.TypeText [occupant]

.Goto what:=wdGoToBookmark, Name:="officer"
.TypeText [officer_name]


End With

DoEvents
WordApp.Activate
WordApp.ActiveDocument.ProtectedForForms = True


Set WordApp = Nothing
Exit Sub

ErrHandler:
Set WordApp = Nothing

End Sub


Thanks in adavance for any help.
 
You don't need to unprotect the template in order to have it enter data into the fields. Here's a sample of something I worked on:
Code:
    Dim objWord As Word.Application
    Dim wrdDoc As Word.Document
    Dim strPath As String

    On Error GoTo err_handler

    strPath = DLookup("cifloc", "tbl-CentralIntakeFormLocation")
    Set objWord = New Word.Application
    With objWord
        .Visible = True
        .WindowState = wdWindowStateMaximize
    End With
    Set wrdDoc = objWord.Documents.Open(strPath, , False, False, , , , , , , , True)

    wrdDoc.FormFields("txtPatientFname").Result = Forms![FrmInput].txtPatientFname
    wrdDoc.FormFields("txtPatientLname").Result = Forms![FrmInput].txtPatientLname
    wrdDoc.FormFields("txtPatientMI").Result = Nz(Forms![FrmInput].txtPatientMI)
    wrdDoc.FormFields("txtPatientDOB").Result = Forms![FrmInput].txtPatientDOB
 
Bob: Thanks for the post. Can you explain it a little bit more how it would apply to my case? My Access and VB/A knowledge is pretty limited. Thanks again.
 
Instead of the code you posted, you would use this (you can add the additional fields - I am putting only a couple here so you can see how it will work. You don't need to unprotect the template for this to work.

Code:
    Dim objWord As Word.Application
    Dim wrdDoc As Word.Document
    Dim strPath As String


    strPath = "C:\PutYourPathToTheWordTemplateHere"
    Set objWord = New Word.Application
    With objWord
        .Visible = True
        .WindowState = wdWindowStateMaximize
    End With
    Set wrdDoc = objWord.Documents.Open(strPath, , False, False, , , , , , , , True)

    wrdDoc.FormFields("ownername").Result = Forms!YourFormNameHere. [occupant]
    wrdDoc.FormFields("bnum").Result = FormsYourFormNameHere. [propad_no]

wrdDoc.Save
wrdDoc.Close
Set objWord=Nothing

So, what this is doing is creating a Word object and a document object, opening the template wherever you set the path (in my original sample I had it set via a DLookup to a table where I keep the path so I can change it easily without hard-coding it in to the code. Then, it opens the template, and then places the appropriate info into the named form fields.

I hope that helps.
 

Users who are viewing this thread

Back
Top Bottom