【问题标题】:MS Access VBA ReferenceMS Access VBA 参考
【发布时间】:2017-12-21 00:05:15
【问题描述】:

我有这个数据库,它运行良好,但今天它开始讨厌我。我开始收到编译错误:找不到项目或库。我环顾四周,发现在另一台计算机上使用前端时,在 VBA 和引用下,它缺少 Microsoft Word 15.0 对象库的引用。

我的电脑我检查了 Microsoft Word 16.0 对象库。我怎样才能让它在其他具有 15.0 和 16.0 的计算机上工作?

这里是代码

Private Sub cmd_LocateFile_Click()
On Error GoTo Error_Handler
Dim sFile As String
Dim sFolder As String
Dim ID As Long
Dim sTarget As String



sFile = FSBrowse("", msoFileDialogFilePicker, "All Files (*.*),*.*")
If sFile <> "" Then
    sFolder = ("\\aiowima23fp1\Ecological Sciences and Engineering\Cultural Resources\New - Cultural Resources Request Database") & "\" & sAttachmentFolderName & "\"
    If FolderExist(sFolder) = False Then MkDir (sFolder)
    ID = RequestID_FK  ' Set current record id.
sTarget = sFolder & CStr(ID) & "-" & GetFileName(sFile)
If CopyFile(sFile, sFolder & GetFileName(sTarget)) = True Then
        Me!FullFileName.Value = sTarget
    Else
    End If
End If

Error_Handler_Exit:
On Error Resume Next
Exit Sub

Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
       "Error Number: " & Err.Number & vbCrLf & _
       "Error Source: " & sModName & "\cmd_LocateFile_Click" & vbCrLf & _
       "Error Description: " & Err.Description & _
       Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
       , vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub

【问题讨论】:

  • 使用后期绑定。如果你想要比这更具体的东西,你需要分享你的代码。
  • 我编辑了问题以包含代码
  • 请包含依赖于 Microsoft Word 对象库参考的代码,而不仅仅是随机代码。

标签: ms-access vba


【解决方案1】:

在开发过程中添加对 Word 的引用的好处是能够使用强类型的单词对象,从而能够利用 IntelliSense。但是,正如您所经历的那样,这些参考对版本控制很敏感。因此,我建议您在开发完成后删除对 Word 的引用。您还必须将所有与 Word 相关的类型替换为 Object,即使用后期绑定。这使得应用程序在版本控制方面更加健壮。

您可以获取一个已经打开的现有 Word 实例,也可以使用此代码 sn-p 打开一个新实例,而无需对 Word DLL 进行任何引用。

Public Function GetWordApplication() As Object
    'Gets an active Word application or opens a new Word instance.
    'Raises Error No. 8 if word cannot be opened.

    On Error Resume Next
    'Find existing instance of Word
    Set GetWordApplication = GetObject(, "Word.Application")
    If Err.Number <> 0 Then 'Not found, create new instance.
        Set GetWordApplication = CreateObject("Word.Application")
    End If

    'Following code is optional. You can instead test for Nothing at the call site
    On Error Goto 0
    If GetWordApplication Is Nothing Then
        Err.Raise 8, "YourApp.GetWordApplication", "Word could not be opened."
    End If
End Function

【讨论】:

  • 非常感谢!你是一个救生员!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多