我在程序实现一个将记录汇出成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

解决方案 »

  1.   

    Dim adoConn As new ADODB.Connection '用new生成实例
        Dim exportSQL As String
        Dim fso As New FileSystemObject
        Set adoConn = CurrentProject.Connection  '这个Connection也需要用new生成实例
      

  2.   

    樓上大哥:    用new 生成實例我都試過,還是提示這個錯誤信息呀。
      

  3.   

    在本机安装不出错吧,如果在别的机器上,
    看看是否Excel版本和你的开发环境不一致,
    你的代码是用这个,
    “Excel 5.0”^_^
      

  4.   

    VB6.0+Access2000 ,Excel是office2000的。
      

  5.   

    把这个:
    Dim fso As New FileSystemObject改为 dim fso as object set fso=createobject("scripting.filesystenobject")
     
     scripting.filesystenobject 这个记不清了,你自己查一下吧
     如果还不行,请更新方计算机的  mdac_typ.exe