vba - 将 Access 数据库转换为后期绑定以使用 Access 运行时
问题描述
我在 Access 数据库 2016 中编写了包含函数和宏的代码,然后我移动了这个文件以与一些没有 Access 应用程序的用户共享,所以我安装了 Access 运行时它运行良好,但我在参考库方面遇到了很多问题。我决定使用后期绑定(通过在 VBA 编辑器的数据库属性中写入 EarlyBinding = 0)并取消选中除两个之外的所有引用(我无法取消选中)和隐蔽
Set appWord = New Word.Application
至
Set appWord = CreateObject(Word.Application)
注 1:我有一个从 Access 表单打开 Word 文档的功能。注意2:我在后期绑定中进行更改后将数据库扩展名从.accdb转换为.accdr,并转换上一个语句没有出现消息错误(供参考)但打开Word的功能不起作用。是否有类似 Word 的 Access 运行时的工具?所以我不能因为这个原因打开?在这个函数的代码下面:
Function fillWordForm()
Dim appWord As Object
Dim doc As Object
Dim path As String
Dim myID As String
On Error Resume Next
Error.Clear
'Set appWord = CreateObject("word.application")
Set appWord = CreateObject(Word.Application)
If Err.Number <> 0 Then
'Set appWord = New Word.Application
'Set appWord = CreateObject(Word.Application)
appWord.Visible = True
End If
'path = Application.CurrentProject.path & "\H_F.docx"
'path = "\\ubcdatacenter\Public\UBCIEDatabase\DOC\H_F.docx"
path = "C:\Users\LENOVO\Desktop\UBC Database\H_F.docx"
If FileExists(path) = False Then
MsgBox "Template File Not Found", vbExclamation, "File Not Found"
Else
Set doc = appWord.Documents.Add(path, , True)
myID = DLookup("ID", "Exports_imports_Table", "[ID] = " & Me.ID)
With doc
.FormFields("BookID").Result = [ID]
.FormFields("Book_BC_date").Result = Me.date_BC
.FormFields("Book_AH_date").Result = Me.date_AH
.FormFields("Book_AH_date").Result = Me.date_AH
.FormFields("BookTopic").Result = Me.topic
.FormFields("BookProjectName").Result = Me.projectName
.FormFields("BookCompanyName").Result = Me.companyName
.FormFields("BookContent").Range.Text = Me.content
'Result = Me.content
appWord.Visible = True
appWord.Active
End With
Set doc = Nothing
Set appWord = Nothing
End If
End Function
这段代码确保文件是兴奋的
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
'Purpose: Return True if the file exists, even if it is hidden.
'Arguments: strFile: File name to look for. Current directory searched if no path included.
' bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
'Note: Does not look inside subdirectories for the file.
'Author: Allen Browne. http://allenbrowne.com June, 2006.
Dim lngAttributes As Long
'Include read-only files, hidden files, system files.
lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
Else
'Strip any trailing slash, so Dir does not look inside the folder.
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
'If Dir() returns something, the file exists.
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
还应该转换什么以使所有代码后期绑定? Mathieu修改后的编辑
函数fillWordForm()
Dim appWord As Object
Dim doc As Object
Dim path As String
Dim myID As String
On Error Resume Next
Error.Clear
Set appWord = GetWordApp
If appWord Is Nothing Then
'can't get ahold of Word.Application... now what?
MsgBox "No thing :(((((((((((((("
'Exit Sub
End If
appWord.Visible = True '<~ unconditional
'Set appWord = CreateObject("word.application")
'Set appWord = CreateObject(Word.Application)
'If Err.Number <> 0 Then
'Set appWord = New Word.Application
'Set appWord = CreateObject(Word.Application)
'appWord.Visible = True
'End If
'path = Application.CurrentProject.path & "\H_F.docx"
path = "\\ubcdatacenter\Public\UBCIEDatabase\DOC\H_F.docx"
'path = "C:\Users\LENOVO\Desktop\UBC Database\H_F.docx"
If FileExists(path) = False Then
MsgBox "Template File Not Found", vbExclamation, "File Not Found"
Else
Set doc = appWord.Documents.Add(path, , True)
myID = DLookup("ID", "Exports_imports_Table", "[ID] = " & Me.ID)
With doc
.FormFields("BookID").Result = [ID]
.FormFields("Book_BC_date").Result = Me.date_BC
.FormFields("Book_AH_date").Result = Me.date_AH
.FormFields("Book_AH_date").Result = Me.date_AH
.FormFields("BookTopic").Result = Me.topic
.FormFields("BookProjectName").Result = Me.projectName
.FormFields("BookCompanyName").Result = Me.companyName
.FormFields("BookContent").Range.Text = Me.content
'Result = Me.content
appWord.Visible = True
appWord.Active
End With
Set doc = Nothing
Set appWord = Nothing
End If
End Function
Private Function GetWordApp() As Object
On Error Resume Next
Set GetWordApp = CreateObject("Word.Application")
End Function
解决方案
'Set appWord = CreateObject("word.application") Set appWord = CreateObject(Word.Application) If Err.Number <> 0 Then 'Set appWord = New Word.Application 'Set appWord = CreateObject(Word.Application) appWord.Visible = True End If
这段代码有几个问题。CreateObject
想要一个 ProgID 字符串,而您正在给它Word.Application
,它甚至不应该编译(期望成员调用上的“需要对象”错误,.Application
以及“未声明变量” 1Word
)。如果它可以编译,则您已引用 Word 对象库并需要将其删除。注释掉的语句格式正确。
现在If Err.Number <> 0
, then appWord
was not Set
, 它的参考是Nothing
. 这意味着如果CreateObject
失败,代码将进入错误状态并在过程的其余部分保持错误状态,因为错误永远不会清除,错误处理也永远不会恢复。
将错误内容纳入其自身的有限范围:
Private Function GetWordApp() As Object
On Error Resume Next
Set GetWordApp = CreateObject("Word.Application")
End Function
现在您的过程只需要检查函数是否返回了有效的对象引用:
Set appWord = GetWordApp
If appWord Is Nothing Then
'can't get ahold of Word.Application... now what?
Exit Sub
End if
appWord.Visible = True '<~ unconditional
1假设Option Explicit
是在模块的顶部,因为它应该是。
推荐阅读
- ignite - 运行两个 ignite 服务器时出现 classNotFoundException
- groovy - 如何使用 for 循环从 TestSuite 仅调用 1 个测试步骤或如何仅在 3 个测试套件中的 2 个测试套件中添加断言
- python - 如何在python脚本函数内部的sql查询中传递多个命令行参数或系统参数?
- or-tools - 使用 ortools for python 选择列表的最小值或最大值
- two-factor-authentication - 如何确保 TOTP 的用户密钥有效?
- c# - 如何使对象仅旋转一次 360 度?
- java - 从 Map 中的 Lists 中删除重复的元素
- javascript - b-table 在“取消移动”新行时重新加载嵌套组件(但不是在“推送”时)
- asp.net-core-2.0 - 在 .net core 2.1.6 中将 Microsoft.aspnetcore.all 更改为 Microsoft.NETCore.App 后,UseLibuv 选项消失了
- openshift - oc new-app 的附加步骤。创建自动缩放策略