将格式化的 Word 文本复制到 Access [关闭]
Posted
技术标签:
【中文标题】将格式化的 Word 文本复制到 Access [关闭]【英文标题】:Copying formatted Word text into Access [closed] 【发布时间】:2017-07-06 11:01:23 【问题描述】:我正在尝试从此页面 copy formatted text into access using vba 调整 VBA,以便它遍历所有选定的文件或特定文件夹中的所有文件。这可能吗?谢谢。
【问题讨论】:
有可能,是的。你的问题到底是什么?使用文件选择器选择多个文件?调整代码以每个文件执行一次?选择扩展名为 .doc 或 .docx 的文件夹中的所有文件? 我试图为多个文件重复它,而不是为每个文件重新运行它。我修改了 AllowMultiSelect = True 但想为所有选择的文件或特定文件夹中的所有文件运行它,以最简单的为准。对不起,我对这一切都很陌生。谢谢。 【参考方案1】:您可以做以下两件事之一:不要拆分文件选择器函数和导入函数,或者从文件选择器函数返回集合或数组。
出于教育价值我会选择后者:
选择文件
Public Function FilesToOpen() As Collection
' This function will essentially allow you to browse to MS-Word document
' and then store the path of that file for use in the GetWordContent function
Dim fDialog As Object 'Office.FileDialog
Dim varFile As Variant
Set fDialog = Application.FileDialog(3)
Set FilesToOpen = New Collection
With fDialog
.AllowMultiSelect = True
.Title = "Select Word document to import"
.Filters.Clear
.Filters.Add "Word files", "*.doc?"
If _
.Show = True _
Then
For Each varFile In .SelectedItems
FilesToOpen.Add varFile
Next
End If
End With
End Function
然后打开它们
Private Sub cmdGetWordData_Click()
' This subroutine runs on your command button; it will call both the FileToOpen function and GetWordContent subroutine
' to retrieve the text contents of your chosen MS-Word Document.
' It will then store both the path the text contents of of your chosen MS-Word Document in 2 fields in a table in Access.
' NOTE: this code assumes that your Access database has:
' - a table called tblWordDump
' - a memo text field in this table called WordDocDataSrc to store the path of MS-Word file imported
' - a memo text field in this table called WordDocData with the TextFormat property set to "Rich Text",
' which will store the text and text formating of the MS-Word file imported
Dim collFiles As Collection
Dim strWordContent As Variant
' Select files via File Dialogue
Set collFiles = FilesToOpen
Dim oneFile As Variant
' Conditionals when a file was or wasn't selected
If _
collFiles.Count <> 0 _
Then
For Each oneFile In collFiles
DoCmd.GoToRecord , , acNewRec
GetWordContent CStr(oneFile)
Next oneFile
MsgBox "Import Successful", vbInformation Or vbOKOnly
Else
MsgBox "No File Selected", vbExclamation Or vbOKOnly
End If
End Sub
请注意,我尝试尽可能少地进行更改,并且没有对 GetWordContent
函数做任何事情。
【讨论】:
谢谢。我在 GetWordContent oneFile 上收到“编译错误:ByRef 参数类型不匹配”。 哦,那只是 Access 很挑剔。GetWordContent CStr(oneFile)
应该可以。编辑答案以反映这一点。
谢谢。我现在在 Set collFiles = FilesToOpen 上遇到运行时错误“424”。
呃……你不应该。我已经测试了代码,它是有效的。有时 Access 更加挑剔,需要像这样的括号:Set collFiles = FilesToOpen()
但这里没有(Access 2016)以上是关于将格式化的 Word 文本复制到 Access [关闭]的主要内容,如果未能解决你的问题,请参考以下文章