将 MS Word 表单域导入 MS Access

Posted

技术标签:

【中文标题】将 MS Word 表单域导入 MS Access【英文标题】:Import MS Word form fields into MS Access 【发布时间】:2010-10-19 16:26:06 【问题描述】:

我已经使用 MS Word 和一大堆表单字段创建了一个申请表,并且我有一个 Access db,可以从这个 Word 文档中导入我需要的所有数据,这要归功于:

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

现在一切正常(我什至设法将它导入到多个表中!),但上面的问题是我必须一次手动输入每个文件的名称......这是如果只是在申请表进来时导入申请表,那很好......但我有很多需要输入到数据库的文件夹中。

然后我发现了这个:

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

我已经尝试调整和合并这两者以使其工作...但您可以猜到,无济于事...(当我是 Access 新手时,它没有帮助!)

我想要做的是能够通过使用打开/选择文件对话框将一堆 Word 文档/表单字段导入 MS Access ......我有什么工作,但我想要让工作更轻松!

谢谢大家 杰克

##### 我一直在使用的代码
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
#

我试图弄乱 me.tables 和 me!forms 和 .add 等 - 显然我在这里完全是新手!!!

我想要的是能够将 Word Doc 中的表单字段中的数据导入到 MS Access 表中(我已经设法使用上面原始帖子中的第一个 URL);通过从打开/选择对话框中选择 Word 文档,而不是手动输入每个 Word 文档的名称。

如果这听起来很明显或简单,我深表歉意 - 无论如何,访问都不是我的强项!

【问题讨论】:

你能告诉我们你在做什么的一步一步吗?另外,您对 VB 和 VBA 感觉如何?我问是因为解决方案可能需要适量的。 所以你的问题是关于对话框设置的吧?你想在那里有什么不同? 感谢您的及时回复! 哎呀...按 Enter 按钮太快了...无论如何,是的,感谢您的及时回复,这是我一直在玩的代码: 【参考方案1】:

在我开始之前,我不明白为什么您的代码示例中有这么多未注释的行(行 beginnig mit ' )。我认为这些行中的大多数通常不会被取消注释并且是工作代码的一部分。还是有 Stack Overflow Editor 的神器?

我看到了一些问题,可能会指导您找到解决方案。

1) 使用时

With fDialog

你让这个'打开'直到代码结束(甚至在两者之间使用第二个 With )。我建议在您不再需要它之后立即为您设置相应的“结束于”。记住(或注意):

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

实际上只是

的简写
fDialog.Title

(即“裸”。意味着它必须附加到 With 中的对象),因此您可以完全取消“With”。在你的例子中,我会在

之前设置“结束于”
If .Show = True Then

然后使用

If fDialog.Show = True Then

2) 我会设置

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

你的 For Each 循环之外(不要忘记在循环之外使用 Set appWord = Nothing)。请记住,使用 GetObject 您需要一个运行的 Word 实例,否则您可能需要使用

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 用于后续步骤(以防您再遇到此问题) 安德烈亚斯

【讨论】:

以上是关于将 MS Word 表单域导入 MS Access的主要内容,如果未能解决你的问题,请参考以下文章

MS Access 2013:通过 VBA 使用 MS Word 的语法检查

使用VB将数据从Excel导入Ms Access

从 ms-access 获取数据到 ms-word

有啥方法可以将 ms access 数据库与 ms word 连接起来吗?

从 MS Word 文档粘贴到 Web 表单

MS ACCESS, VBA 将外部 MS Access 表导入 SQL server 表