How to get Content Controls in Word Form to populate Access Table

I am interested in Access code that will import data directly from the Word form (Input1.doc) into the Access table (Input) using the Access form (Action) in the Survey.accdb file.

Tried to get the content controls in the Word form into Access using the content control Titles.

I've uploaded what I have so far in my event procedure in the Access form (Action).
Input1.docx
Survey.accdb
clock1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try this instead...

Private Sub Command2_Click()
 Dim appWord As Word.Application
    Dim doc As Word.Document
    Dim MyDB As Database
    Dim rst As Recordset
    Dim strDocName As String
    Dim blnQuitWord As Boolean
    
      
    strDocName = OpenWordDataFile()
    On Error Resume Next
    Set appWord = GetObject(, "Word.Application")
    If appWord Is Nothing Then
        Set appWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    Set doc = appWord.Documents.Open(strDocName)
                  
       Set MyDB = CurrentDb
       Set rst = MyDB.OpenRecordset("Input", dbOpenDynaset)
       With rst
        .AddNew
        ![OrgID] = doc.ContentControls.Item(1).Range.Text
        ![OrgNm] = doc.ContentControls.Item(2).Range.Text
        ![OrgAddr] = doc.ContentControls.Item(3).Range.Text
        ![Pmaint] = doc.ContentControls.Item(4).Checked
        ![Prep] = doc.ContentControls.Item(5).Checked
        ![Pres] = doc.ContentControls.Item(6).Checked
        ![Omaint] = doc.ContentControls.Item(7).Checked
        ![Orep] = doc.ContentControls.Item(8).Checked
        ![Ores] = doc.ContentControls.Item(9).Checked
        .Update
        .Close
       End With
       MyDB.Close
    
    doc.Close
    If blnQuitWord Then appWord.Quit
    MsgBox "Organization Imported!"
    
    Set rst = Nothing
    Set doc = Nothing
    Set appWord = Nothing

End Sub

Open in new window

0
clock1Author Commented:
Ryan -

On click, I receive "Compile Error. Sub or Function not defined"
Debugger highlights line 10 on OpenWordDataFile()
0
Ryan ChongCommented:
>>On click, I receive "Compile Error. Sub or Function not defined"
>>Debugger highlights line 10 on OpenWordDataFile()
I thought that was from your original code... you can replace that with a word document file path instead.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

clock1Author Commented:
You're correct. That was from my original code and might be a reason why my code is not working.

I tried this, but still get same compile error:

 strDocName = OpenWordDataFile("c:\input1.doc")
0
Ryan ChongCommented:
no, ... you need to include the function OpenWordDataFile in to your module.

it depends on that function to return a string value...

I guess it will call out a common dialog box, so that you can select a word document from your local directory?
0
Ryan ChongCommented:
try something like this instead....

Private Sub Command2_Click()
 Dim appWord As Word.Application
    Dim doc As Word.Document
    Dim MyDB As Database
    Dim rst As Recordset
    Dim strDocName As String
    Dim blnQuitWord As Boolean
    
      
    strDocName = OpenWordDataFile()
    If strDocName = "" Then Exit Sub
    
    On Error Resume Next
    Set appWord = GetObject(, "Word.Application")
    If appWord Is Nothing Then
        Set appWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    Set doc = appWord.Documents.Open(strDocName)
                  
       Set MyDB = CurrentDb
       Set rst = MyDB.OpenRecordset("Input", dbOpenDynaset)
       With rst
        .AddNew
        ![OrgID] = doc.ContentControls.Item(1).Range.Text
        ![OrgNm] = doc.ContentControls.Item(2).Range.Text
        ![OrgAddr] = doc.ContentControls.Item(3).Range.Text
        ![Pmaint] = doc.ContentControls.Item(4).Checked
        ![Prep] = doc.ContentControls.Item(5).Checked
        ![Pres] = doc.ContentControls.Item(6).Checked
        ![Omaint] = doc.ContentControls.Item(7).Checked
        ![Orep] = doc.ContentControls.Item(8).Checked
        ![Ores] = doc.ContentControls.Item(9).Checked
        .Update
        .Close
       End With
       MyDB.Close
    
    doc.Close
    If blnQuitWord Then appWord.Quit
    MsgBox "Organization Imported!"
    
    Set rst = Nothing
    Set doc = Nothing
    Set appWord = Nothing

End Sub

Function OpenWordDataFile() As String
    With Application.FileDialog(msoFileDialogFilePicker)
        ' Allow the user to make multiple selections in the dialog box.
        .AllowMultiSelect = False
        
        ' Set the title of the dialog box.
        .Title = "Select word document"
        
        ' Clear out the current filters, and then add your own.
        .Filters.Clear
        .Filters.Add "Word", "*.docx"
        .Filters.Add "All Files", "*.*"
        
        ' Show the dialog box. If the .Show method returns True, the
        ' user picked at least one file. If the .Show method returns
        ' False, the user clicked Cancel.
        If .Show = True Then
        ' Loop through each file that is selected and then add it to the list box.
            OpenWordDataFile = .SelectedItems(1)
        Else
            OpenWordDataFile = ""
        End If
    End With
End Function

Open in new window

0
clock1Author Commented:
Sorry no cigar, but did get further this time.  Copied and pasted your code and got following run-time error:

Method File Dialog of object _Application failed.

Debugger stopped on line 51 of function:

With Application.FileDialog(msoFileDialogFilePicker)

I checked to make sure that Microsoft Word 15.0 Object Library is an available Reference
0
clock1Author Commented:
I added Microsoft Office 15.0 Object Library to Reference and your code worked.
0
Ryan ChongCommented:
>>I added Microsoft Office 15.0 Object Library to Reference and your code worked.
yup, so sorry forgot to mention that. We can also do that via Windows API which no need to add Microsoft Office 15.0 Object Library to Reference but for a quick solution I prefer the former.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clock1Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.