通过CreateObject(“应用程序”)获取Access 2007中的全名..?

时间:2018-01-04 17:39:22

标签: vba excel-vba access-vba word-vba excel

我正在尝试通过CreateObject()方法获取Access 2007中当前db文件的全名,但它不起作用,它只返回一个空字符串。我正在使用CreateObject(),因为代码将在多个Microsoft Office产品中使用,所以...我相信正确的术语是对象是后期绑定的。?

具体而言,这有效......

Public Sub ThisWorks()
    Dim obj1 As Object
    Dim obj2 As Object
    Set obj1 = Access.Application
    Set obj2 = obj1.CurrentProject
    Debug.Print obj2.FullName
End Sub

......但这不起作用......

Public Sub ThisDoesNotWork()
    Dim obj1 As Object
    Dim obj2 As Object
    Set obj1 = CreateObject("Access.Application") 'This is the only change.
    Set obj2 = obj1.CurrentProject
    Debug.Print obj2.FullName
End Sub

为什么这不起作用..?是否可以这样做..?

为了回答不可避免的问题“你是如何使用它的?”,上面的例子是以下函数的缩写...

Public Function CurrentFilename() As String

    'This function tries to return the full name & path of the currently open file. It's
    'based on the idea that all (or most?) implementations of VBA have the foundation
    'object called "Application", and a child object with a property that has the
    'filename. The design of this function should allow it to run in any software
    'product that supports VBA. It might even run in VBScript too, but that's not been
    'tested. If any error occurs, or the product cannot return the filename, or the
    'function does not recognize the product, an empty string is returned. As time goes
    'on, more software product names can be added to the Select Case, but for now it's
    'just Microsoft Office products.

    Dim productName As String
    Dim obj1 As Object
    Dim obj2 As Object
    Dim filename As String
    Dim msgStyle As VbMsgBoxStyle

    On Error GoTo ErrorHandler

    msgStyle = vbApplicationModal + vbExclamation + vbOKOnly

    productName = Application.Name

    Select Case productName
        Case "Microsoft Access"
            Set obj1 = CreateObject("Access.Application")
            Set obj2 = obj1.CurrentProject
            filename = obj2.FullName
        Case "Microsoft Excel"
            Set obj1 = CreateObject("Excel.Application")
            Set obj2 = obj1.ActiveWorkbook
            filename = obj2.FullName
        Case "Outlook"
            'Returns the path\name of the current default *.OST file.
            Set obj1 = CreateObject("Outlook.Application")
            Set obj2 = obj1.Session.DefaultStore
            filename = obj2.FilePath
        Case "Microsoft PowerPoint"
            Set obj1 = CreateObject("PowerPoint.Application")
            Set obj2 = obj1.ActivePresentation
            filename = obj2.FullName
        Case "Microsoft Publisher"
            Set obj1 = CreateObject("Publisher.Application")
            Set obj2 = obj1.ActiveDocument
            filename = obj2.FullName
        Case "Microsoft Word"
            Set obj1 = CreateObject("Application.ActivePresentation")
            Set obj2 = obj1.Session.DefaultStore
            filename = obj2.FullName
        Case Else
            'Optional error message to the user.
            MsgBox "The current VBA runtime environment is not recognized.", _
                    msgStyle, "Error getting file name."
    End Select

    CurrentFilename = filename

    Exit Function

ErrorHandler:

    Dim msg As String
    Dim dot As String

    'Be a grammar grouch. Ensure the description ends with a period/fullstop.
    If VBA.Right(err.Description, 1) <> "." Then
        dot = "."
    End If

    msg = ""
    msg = msg & "Error:" & vbTab & VBA.CStr(err.Number)
    msg = msg & vbCrLf & vbCrLf
    msg = msg & "Desc:" & vbTab & err.Description & dot
    msg = msg & vbCrLf & vbCrLf
    msg = msg & "Source:" & vbTab & err.Source

    MsgBox msg, msgStyle, "Runtime Error"

    'Do not return from the error. Just exit and return an empty string.
    'Resume Next

End Function

1 个答案:

答案 0 :(得分:2)

后期绑定版本无效的基本问题是因为您要求VBA引擎向您提供新的Application而不是当前正在运行的版本。早期绑定代码CreateObject("Access.Application")相当于new Access.Application。 (字符串“Access.Application”描述了您想要到达的类型。)这不是您想要的。

另一方面,当您指定Access.Application时,您将全局变量Access.Application分配给对象,该对象是当前正在运行的Access实例。

请注意,这是您的方法的一般问题。

由于您始终想要访问当前的Application对象,因此您可以简单地将此不合格的内容分配给obj1并从那里开始。这总是解析为引用对话框中具有最高优先级的引用,它将是主机应用程序之一。