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

clock1
clock1 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
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

Author

Commented:
Ryan -

On click, I receive "Compile Error. Sub or Function not defined"
Debugger highlights line 10 on OpenWordDataFile()
Ryan ChongSoftware Team Lead

Commented:
>>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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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")
Ryan ChongSoftware Team Lead

Commented:
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?
Ryan ChongSoftware Team Lead

Commented:
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

Author

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

Author

Commented:
I added Microsoft Office 15.0 Object Library to Reference and your code worked.
Software Team Lead
Commented:
>>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.

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial