我在程序实现一个将记录汇出成Excel文件的功能,在VB环境下运行没有问题,但是在打包后安装运行就会出现这个错误提示:Object variable or with block variable not set,请教各位如何解决?
程序如下
Sub ExportExcel(JetSQL As String, SaveLocation As String) Dim adoConn As ADODB.Connection
Dim exportSQL As String
Dim fso As New FileSystemObject
Set adoConn = CurrentProject.Connection
If Len(SaveLocation) > 0 Then
exportSQL = "select * into [Excel 5.0;HDR=YES;DATABASE=" & SaveLocation & "].[sheet1] from " & JetSQL
If fso.FileExists(SaveLocation) Then
MsgBox "此文件已存在﹗"
Set fso = Nothing
If adoConn.State = 1 Then adoConn.Close
Set adoConn = Nothing
Exit Sub
Else
adoConn.Execute exportSQL
If fso.FileExists(SaveLocation) Then
MsgBox "操作成功!", vbExclamation, "^_^"
If adoConn.State = 1 Then adoConn.Close
Set adoConn = Nothing
Set fso = Nothing
Exit Sub
End If
End If
End If
End Sub
程序如下
Sub ExportExcel(JetSQL As String, SaveLocation As String) Dim adoConn As ADODB.Connection
Dim exportSQL As String
Dim fso As New FileSystemObject
Set adoConn = CurrentProject.Connection
If Len(SaveLocation) > 0 Then
exportSQL = "select * into [Excel 5.0;HDR=YES;DATABASE=" & SaveLocation & "].[sheet1] from " & JetSQL
If fso.FileExists(SaveLocation) Then
MsgBox "此文件已存在﹗"
Set fso = Nothing
If adoConn.State = 1 Then adoConn.Close
Set adoConn = Nothing
Exit Sub
Else
adoConn.Execute exportSQL
If fso.FileExists(SaveLocation) Then
MsgBox "操作成功!", vbExclamation, "^_^"
If adoConn.State = 1 Then adoConn.Close
Set adoConn = Nothing
Set fso = Nothing
Exit Sub
End If
End If
End If
End Sub
Dim exportSQL As String
Dim fso As New FileSystemObject
Set adoConn = CurrentProject.Connection '这个Connection也需要用new生成实例
看看是否Excel版本和你的开发环境不一致,
你的代码是用这个,
“Excel 5.0”^_^
Dim fso As New FileSystemObject改为 dim fso as object set fso=createobject("scripting.filesystenobject")
scripting.filesystenobject 这个记不清了,你自己查一下吧
如果还不行,请更新方计算机的 mdac_typ.exe