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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Data from a Word Form into an Access table 1

Status
Not open for further replies.

pra

Instructor
Jun 12, 2001
6
US
I have found the following code to use as an Access Module to move the data from a Word Form into an Access Table. I am getting a compile error: User defined type not defined at Dim doc As Word.Document. Any suggestions?

ADO procedure to import Word data
Sub GetWordData()
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:\Contracts\" & _
InputBox("Enter the name of the Word contract " & _
"you want to import:", "Import Contract")

Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\My Documents\" & _
"Test Contracts.mdb;"
rst.Open "tblContracts", cnn, _
adOpenKeyset, adLockOptimistic

With rst
.AddNew
!FirstName = doc.FormFields("fldFirstName").Result
!LastName = doc.FormFields("fldLastName").Result
!Company = doc.FormFields("fldCompany").Result
!Address = doc.FormFields("fldAddress").Result
!City = doc.FormFields("fldCity").Result
!State = doc.FormFields("fldState").Result
!ZIP = doc.FormFields("fldZIP1").Result & _
"-" & doc.FormFields("fldZIP2").Result
!Phone = doc.FormFields("fldPhone").Result
!SocialSecurity = doc.FormFields("fldSocialSecurity").Result
!Gender = doc.FormFields("fldGender").Result
!BirthDate = doc.FormFields("fldBirthDate").Result
!AdditionalCoverage = doc.FormFields("fldAdditional").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



 
You're getting this message because Word.Document isn't defined anywhere. The definition is stored in a "type library" that comes with Office, but you have to add it to your Access project before Access will know about it.

To add the Word type library to your project:
1. Open the VBA code window.
2. Choose Tools>References from the menu.
3. In the dialog, scroll down the list to Microsoft Word 9.0 Object Library.
4. Check the check box next to this.
5. Click OK.

Your project should compile after that. Rick Sprague
 
I have the following References selected:
VBA
Access 9.0 Ob Lib
Word 9.0 Ob Lib
OLE Automation
VBA Extensibility 5.3

I am now getting the same error "user-defined type not defined" at:
cnn As New ADODB.Connection
 
You also need a reference to Microsoft ActiveX Data Objects Version so-and-so (I don't have Access 2000 where I am right now). It's odd that this one isn't already in your list. It's added to the references by default, with every new Access project. Rick Sprague
 
Now that i have debugged this (added the resources, and updated my field names.)

I am getting the dialog box that asks for my document name to import then I get the following error, after I type in my 9999.doc OK

"-2147417851 Automation Error The Service threw an exception"

Any ideas? I really appreciate your help!

Pam
 
This is really an entirely different problem, probably related to OLE DB services, about which I know nothing. Since this thread has several responses now, it's less likely to have new people who know the answer looking at it. Also, this problem isn't described by the subject line. So I'd recommend posting this as a new question. Rick Sprague
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top