Skip to content

Importing Data from a Word Document into a Memo Field

How to import the contents of a specific Word document into a Memo field on an Access form.

Q: John Piper asks if there is a way to import the contents of a specific Word document, in a specific folder, into a Memo field on an Access form.

A: The following Click event procedure will do the job, for a hard-coded document name and path.  If you want to pick up the document name or path from a text box or InputBox (for greater flexibility), see the faxing code in Access Archon #112.  This code requires a reference to the Word object model (see the References section for more details).

Private Sub cmdImportNotes_Click()

 

On Error GoTo ErrorHandler

  

   Dim strFolder As String

   Dim strDoc As String

   Dim appWord As Word.Application

   Dim doc As Word.Document

   Dim varMemo As Variant

  

   Set appWord = GetObject(, “Word.Application”)

   strFolder = “D:DocumentsMiscellaneous”

   strDoc = strFolder & “Memo Text.doc”

   Set doc = appWord.Documents.Open(strDoc)

   appWord.Selection.WholeStory

   varMemo = appWord.Selection

   Me![Notes] = varMemo

   Me![txtNotes].Requery

   doc.Close (wdDoNotSaveChanges)

  

ErrorHandlerExit:

   Set appWord = Nothing

   Exit Sub

 

ErrorHandler:

   ‘Word is not running; open Word with CreateObject

   If Err.Number = 429 Then

      Set appWord = CreateObject(“Word.Application”)

      Resume Next

   Else

      MsgBox “Error No: ” & Err.Number & “; Description: “

      Resume ErrorHandlerExit

   End If

 

End Sub

About this author