I have taken this code and tried to modify for a small project. I am not a VBA wizard and have come unstuck. The following code produces a compile error "type mismatch" - I don't know what that means, could someone please enlighten me and suggest a solution.
This seems to be the offending line:
strDocName = "C:\Contracts\" & _
TIA
Option Compare Database
Option Explicit
Sub GetWordData()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As DAO.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)
Set rst = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\My Documents\" & _
"SoftwareAcademyContacts.mdb;"
rst.Open "tblQuestionnaire", cnn, _
adOpenKeyset, adLockOptimistic
With rst
.AddNew
!Name = doc.FormFields("Name".Result
!Location = doc.FormFields("Location".Result
!University = doc.FormFields("University".Result
!Qualification = doc.FormFields("Qualification".Result
!GraduationDate = doc.FormFields("GraduationDate".Result
!Looking = doc.FormFields("Looking".Result
!CurrentState = doc.FormFields("CurrentState".Result
!CurrentStateExpand = doc.FormFields("CurrentStateExpand".Result
!HearAbout = doc.FormFields("HearAbout".Result
!HearAboutExpand = doc.FormFields("HearAboutExpand".Result
!SAPurpose = doc.FormFields("SAPurpose".Result
!ScottishOnly = doc.FormFields("ScottishOnly".Result
!Connection = doc.FormFields("Connection".Result
!ConnectionExpand = doc.FormFields("fldAdditional".Result
!SADoneforme = doc.FormFields("SADoneforme".Result
!RateService = doc.FormFields("RateService".Result
!Methods = doc.FormFields("Methods".Result
!MethodsExpand = doc.FormFields("MethodsExpand".Result
!SuccesfulMethods = doc.FormFields("SuccesfulMethods".Result
!SuccesfulMethodsExpand = doc.FormFields("SuccesfulMethodsExpand".Result
!SubmittedCV = doc.FormFields("SubmittedCV".Result
!Impression = doc.FormFields("Impression".Result
!ImpressionExpand = doc.FormFields("ImpressionExpand".Result
!Improvements = doc.FormFields("Improvements".Result
.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
MsgBox "Questionnaire Imported!"
Cleanup:
Set rst = 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
This seems to be the offending line:
strDocName = "C:\Contracts\" & _
TIA
Option Compare Database
Option Explicit
Sub GetWordData()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As DAO.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)
Set rst = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\My Documents\" & _
"SoftwareAcademyContacts.mdb;"
rst.Open "tblQuestionnaire", cnn, _
adOpenKeyset, adLockOptimistic
With rst
.AddNew
!Name = doc.FormFields("Name".Result
!Location = doc.FormFields("Location".Result
!University = doc.FormFields("University".Result
!Qualification = doc.FormFields("Qualification".Result
!GraduationDate = doc.FormFields("GraduationDate".Result
!Looking = doc.FormFields("Looking".Result
!CurrentState = doc.FormFields("CurrentState".Result
!CurrentStateExpand = doc.FormFields("CurrentStateExpand".Result
!HearAbout = doc.FormFields("HearAbout".Result
!HearAboutExpand = doc.FormFields("HearAboutExpand".Result
!SAPurpose = doc.FormFields("SAPurpose".Result
!ScottishOnly = doc.FormFields("ScottishOnly".Result
!Connection = doc.FormFields("Connection".Result
!ConnectionExpand = doc.FormFields("fldAdditional".Result
!SADoneforme = doc.FormFields("SADoneforme".Result
!RateService = doc.FormFields("RateService".Result
!Methods = doc.FormFields("Methods".Result
!MethodsExpand = doc.FormFields("MethodsExpand".Result
!SuccesfulMethods = doc.FormFields("SuccesfulMethods".Result
!SuccesfulMethodsExpand = doc.FormFields("SuccesfulMethodsExpand".Result
!SubmittedCV = doc.FormFields("SubmittedCV".Result
!Impression = doc.FormFields("Impression".Result
!ImpressionExpand = doc.FormFields("ImpressionExpand".Result
!Improvements = doc.FormFields("Improvements".Result
.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
MsgBox "Questionnaire Imported!"
Cleanup:
Set rst = 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