I have attached a very simple word doc and database for reference. I have seen a lot of posts in this forum regarding attempts to do what I would like to do, but none are user friendly at all.
It seems to me this should be a fairly easy thing to do. I want to click a button on my form and fill in a new record by retrieving a word document that has the info needed. If you look at my Test example, I have 3 fields: a date, a combobox choice, and a checkbox. I have code that is supposed to extract the info from the word doc and form a new record, but I get an error "User-defined type not defined" the line of code it hangs on is: "Dim appWord As Word.Application"
The code was taken from a tutorial on "2000". Does any one have a more up to date version that works with "2007"? Thank you for any help.
Private Sub CmdTest_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strDocName As String
Dim blnQuitWord As Boolean
On Error GoTo ErrorHandling
strDocName = "C:\Users\bbrazeau\Desktop\Test"
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\bbrazeau\Desktop\" & _
"Test.accdb;"
rst.Open "tblContracts", cnn, _
adOpenKeyset, adLockOptimistic
With rst
.AddNew
!TestDate = doc.FormFields("TestDate").Result
!TestText = doc.FormFields("TestText").Result
!TestChoice = doc.FormFields("Check1").Result
.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Contract Imported!"
Cleanup:
Set rst = Nothing
Set cnn = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
ErrorHandling:
Select Case Err
Case -2147022986, 429
Set appWord = CreateObject("Word.Application")
blnQuitWord = True
Resume Next
Case 5121, 5174
MsgBox "You must select a valid Word document. " _
& "No data imported.", vbOKOnly, _
"Document Not Found"
Case 5941
MsgBox "The document you selected does not " _
& "contain the required form fields. " _
& "No data imported.", vbOKOnly, _
"Fields Not Found"
Case Else
MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
End Sub