我正在尝试在vba中打开excel时执行以下代码,但是它给了我错误对象或者没有设置块变量。我编写了以下函数来执行我的功能。
Private Sub Workbook_Open()
Dim FilePath As String
Dim AccessPath As String
FilePath = Application.ThisWorkbook.Path
AccessPath = FilePath & "\ABC Data.accdb"
With ActiveWorkbook.Connections("ABC DATA_V1").OLEDBConnection
.BackgroundQuery = True
.CommandText = Array("ABCDataTest")
.CommandType = xlCmdTable
.Connection = Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=" & AccessPath & ";" _
, _
"Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";" _
, _
"Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet" _
, _
" OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Da" _
, _
"tabase=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet" _
, _
" OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass" _
, " ChoiceField Validation=False")
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("ABC DATA_V1")
.Name = "ABC DATA_V1"
.Description = ""
End With
ActiveWorkbook.Connections("ABC DATA_V1").Refresh
End Sub
当我尝试调试时,如果逐行执行它正常工作但在文件打开时它会在第一个带块(使用ActiveWorkbook.Connections ...)时给出错误。