Here is my modified code. Works great. I do have 2 checkboxes that I need if they are checked or not.
The Male and Female fields are the checkboxes. The other forms that I need to import have around 60 or so checkboxes.
Thank you for all your help. This is my first time with VB and I have learned a lot from all your help.
Code:
Sub GetWordData()
Dim cc As ContentControl
Dim fc As Field
Dim ccInfo As String
Dim YourName As String
Dim YourAddress As String
Dim YourPhone As String
Dim Male As CheckBox
Dim Female As CheckBox
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:\SampleForm\" & _
InputBox("Enter the name of the Word document you want to import:", "Import document")
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set doc = appWord.Documents.Open(strDocName)
For Each cc In doc.ContentControls
ccInfo = "<> ID= " & cc.Id & " Title = " & cc.Title & " Text = " & cc.Range.Text & vbCrLf
Debug.Print ccInfo
Select Case cc.Title:
Case "YourName"
YourName = cc.Range.Text
Case "YourAddress"
YourAddress = cc.Range.Text
Case "YourPhone"
YourPhone = cc.Range.Text
Case "Male"
Male = cc.Range.Text
Case "Female"
Female = cc.Range.Text
End Select
Next
Set cnn = CurrentProject.Connection
rst.Open "FormData", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
rst.Fields("FullName") = YourName
rst.Fields("Address") = YourAddress
rst.Fields("Phone") = YourPhone
rst.Fields("Male") = Male
rst.Fields("Female") = Female
rst.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Request Imported"
Cleanup:
Set rst = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
ErrorHandling:
Select Case Err.Number
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 5491
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
Thank you for all your help. This is my first time with VB and I have learned a lot from all your help.
Comment