我做了一个数据输出到excel文件的程序,只在没打开excel的情况下,运行正常,如果打开excel,运行程序,程序就会出错。......
我想进行出错判断:程序运行时,如果此时正开着excel,我希望能报错!!!

解决方案 »

  1.   

    你可以加一句
        If Err <> 0 Then   .......
      
        endif
    当有错误的时候就能提示你了
      

  2.   

    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
    Dim Handle As Long    ' 窗口句柄
    Handle = FindWindow("XLMAIN", vbNullString)   ' 获得窗口句柄
    If Handle = 0 Then MsgBox "Excel没启动" Else MsgBox "Excel已经启动!"
    End Sub
      

  3.   

    你的程序写的有问题,你可以使用已经打开的实例,在vb 中应该可以这样set exlapp=getobject("EXCEL.SHEET","你要使用的EXCEL文件名")
    你想报错的话,只要判断exleapp is nothing 就可以了
      

  4.   

    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
    Dim Handle As Long    ' 窗口句柄
    Handle = FindWindow("XLMAIN", vbNullString)   ' 获得窗口句柄
    If Handle = 0 Then MsgBox "Excel没启动" Else MsgBox "Excel已经启动!"
    End Sub
    没错的 !!!!