【发布时间】: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 按钮太快了...无论如何,是的,感谢您的及时回复,这是我一直在玩的代码: