MS Access VBA to Get Data from Microsoft Word Document and Paste it into Form (MEMO) (1 Viewer)

Mohsin Malik

Registered User.
Local time
Today, 13:21
Joined
Mar 25, 2012
Messages
175
Hello,

I would need guidance in copying all the contents/text from word document and pasted that in the Memo Field, I have a Recruitment database where I have 02 fields on is CV Path (Text Field) that stores the CV Path (Word Document Path) and another text box content (Memo Field) where I would like to copy all the data from the word document to the Content text box (Memo) field.

I have put a Command button on the form and can anyone please advise about the code to "Copy the content/text" from [CV Path] word document and add it to the "Memo" field

CV's are mostly contains the text and I want to copy the content over to the memo field, any idea?

Thank you
Mohsin
 

ino_mart

Registered User.
Local time
Today, 02:21
Joined
Oct 7, 2009
Messages
78
Dear

I was able to do this with next code.

First of all, in the Access VBA Editor go to Tools - References. Enable next references
Microsoft Word 14.0 Object Library
Microsoft Office 14.0 Object Library
note: depending your Office version, you might see something else than 14.0

Add next function to your project. This will open a file dialog where you can double click the word file you want to import

Code:
Private Function FileToOpen() As String
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
    .AllowMultiSelect = False
    .Title = "Select Word document to import"
    .Filters.Clear
    .Filters.Add "Word files", "*.doc?"
 
    If .Show = True Then
        For Each varFile In .SelectedItems
            FileToOpen = varFile
        Next
    Else
        FileToOpen = ""
    End If
End With
End Function

Add function below. This will pick out all the text from the Word-document. Note that pictures and special objects will be ignored.
Code:
Private Function GetWordContent(strFile As String) As Variant
Dim objDoc As Word.Document
Set objDoc = GetObject(strFile)
GetWordContent = CVar(objDoc.Range.Text)
objDoc.Close
End Function

Finally, add a button on your form to select the file, read the content and save it in your table
Code:
Private Sub cmdPickWord_Click()
Dim strFile As String
Dim strWordContent As Variant
Dim strSQL As String
strFile = FileToOpen 'Open FileDialog and select file
If Len(strFile) > 0 Then 'Check if user did not press cancel
    strWordContent = GetWordContent(strFile)  'get content of file
'On line below you must replace 'table' with the real name of the table/view. You also need to replace [cv path] and [memo field] with the real fieldnames,  
    strSQL = "insert into [table]([cv path],[memo field]) values('" & strFile & "','" & strWordContent & "')"
    DoCmd.RunSQL strSQL 'save data into table
    MsgBox("Imported")
Else
    MsgBox ("No file selected")
End If
 
End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom