Avatar of clock1
clock1
 asked on

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
Microsoft Access

Avatar of undefined
Last Comment
clock1

8/22/2022 - Mon
Ryan Chong

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

clock1

ASKER
Ryan -

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

>>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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
clock1

ASKER
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 Chong

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 Chong

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
clock1

ASKER
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
clock1

ASKER
I added Microsoft Office 15.0 Object Library to Reference and your code worked.
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
clock1

ASKER
Thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23