Link to home
Start Free TrialLog in
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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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

Avatar of clock1
clock1

ASKER

Ryan -

On click, I receive "Compile Error. Sub or Function not defined"
Debugger highlights line 10 on OpenWordDataFile()
>>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.
Avatar of 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")
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?
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

Avatar of 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
Avatar of clock1

ASKER

I added Microsoft Office 15.0 Object Library to Reference and your code worked.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clock1

ASKER

Thanks