【发布时间】:2018-06-14 12:06:43
【问题描述】:
我正在尝试通过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
【问题讨论】:
标签: excel ms-access ms-word vba