Import MS Word form fields in MS Access

I created an application form using MS Word and a whole bunch of form fields, and I have an access db that can import all the data I need from this Word document, thanks to this:

http://msdn.microsoft.com/en-us/library/aa155434%28office.10%29.aspx

Now everything works fine (I even managed to get it for import into several tables!), But the problem with the above is that I need to manually enter the name of each file one at a time ... which is fine if this is just a case import the application form because it is included ... but I am pretty much sitting in the folder that I want to enter into the database.

Then I found this:

How to show "Open File" Dialog in Access 2007 VBA?

I tried tweaking and combining the two to make it work ... but, as you might guess, to no avail ... (this does not help when I am very new to Access!)

What I want to do is import a bunch of fields of Word documents and forms into MS Access using the Open / Select file dialog box ... I have work, but I would like to simplify the work!

Thanks everyone Jake

##### Codes I Used
Option Compare Database

Option Explicit

Private Sub cmdFileDialog_Click()

' This requires a reference to the Microsoft Office 11.0 Object Library.

Dim fDialog As Office.FileDialog
Dim varFile As Variant

Dim appWord As Word.Application
Dim doc As Word.Document
' Dim cnn As New ADODB.Connection
' Dim rst As New ADODB.Recordset
Dim strDocName As String
Dim blnQuitWord As Boolean

' Clear the list box contents.
' Me.FileList.RowSource = ""

' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow the user to make multiple selections in the dialog box.
.AllowMultiSelect = True

' Set the title of the dialog box.
.Title = "Select One or More Files"

' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "Microsoft Word", "*.DOC"
.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.
For Each varFile In .SelectedItems
' Me.FileList.AddItem varFile

Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(varFile)

' cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
'     "Data Source=M:\Medical\GPAppraisal\Contacts & Databases\" & _
'     "AppForm.mdb;"
' rst.Open "tbl_Applicants", cnn, _
'     adOpenKeyset, adLockOptimistic

' With rst
.addnew
!Title = doc.FormFields("wTitle").Result
!FirstName = doc.FormFields("wFirstName").Result
!LastName = doc.FormFields("wLastName").Result
!Address1 = doc.FormFields("wAddress1").Result
!Address2 = doc.FormFields("wAddress2").Result
!Address3 = doc.FormFields("wAddress3").Result
!City = doc.FormFields("wCity").Result
!PostCode = doc.FormFields("wPostCode").Result
!Email = doc.FormFields("wEmail").Result
!Phone1 = doc.FormFields("wPhone1").Result
!Phone2 = doc.FormFields("wPhone2").Result
!LM = doc.FormFields("wLM").Result
!LMAddress1 = doc.FormFields("wLMAddress1").Result
!LMAddress2 = doc.FormFields("wLMAddress2").Result
!LMAddress3 = doc.FormFields("wLMAddress3").Result
!LMCity = doc.FormFields("wLMCity").Result
!LMPostCode = doc.FormFields("wLMPostCode").Result
!LMEmail = doc.FormFields("wLMEmail").Result
!LMPhone = doc.FormFields("wLMPhone").Result
!LMOK = doc.FormFields("wLMOK").Result
!Probity = doc.FormFields("wProbity").Result
!Practising = doc.FormFields("wPractising").Result
!Signature = doc.FormFields("wSignature").Result
!AppDate = doc.FormFields("wAppDate").Result
!e2011012028 = doc.FormFields("w2011012028").Result
!e2011021725 = doc.FormFields("w2011021725").Result
!e2011030311 = doc.FormFields("w2011030311").Result
!e2011031625 = doc.FormFields("w2011031625").Result
!e20110203 = doc.FormFields("w20110203").Result
!e20110211 = doc.FormFields("w20110211").Result
!e20110322 = doc.FormFields("w20110322").Result
!e20110330 = doc.FormFields("w20110330").Result
.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Application Imported!"

Cleanup:
' Set rst = Nothing
' Set cnn = Nothing
Set doc = Nothing
Set appWord = Nothing

Next
Else
   MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
#

I tried to ruin my forms and files, etc. etc. etc. - obviously I'm a complete newbie here!

I want to be able to import data from form fields in Word Doc into an MS Access table (which I managed to do with the first URL in my initial post above); by selecting a Word document from the Open / Select dialog box, instead of manually entering the names of each Word document.

, - !

+3
1

, , (lines beginnig mit '). , , , . ?

, .

1)

With fDialog

"" ( ). "End With" , . ( ):

With fDialog
   [... something]
   ' Set the title of the dialog box.
   .Title = "Select One or More Files"

fDialog.Title

(.. "" , ), "". "End With"

If .Show = True Then

If fDialog.Show = True Then

2)

Set appWord = GetObject(, "Word.Application")

For Each ( Set appWord = Nothing ). , GetObject runnig Word-instance,

Set appWord = CreateObject("Word.Application")

, Word, (.. Err.Number = 429), .

On Error Resume Next
Set appWord = GetObject(, "Word.Application")

If Err.Number = 429 Then
    Set appWord = CreateObject("Word.Application")
End If
On Error GoTo 0

3) , ,

objword.Visible = True

Word.

HTH ( ) Andreas

+1

Source: https://habr.com/ru/post/1770342/


All Articles