解决方案 »

  1.   

    我的想法是需要用个if条件语句,若excel已打开,则msgbox “EXCEL已打开,请关闭“;否则程序继续运行,直接生成新的EXCEL。
      

  2.   

    Dim fileName As StringfileName = App.Path & "/结果输出.xls"On Error Resume Next
    Kill fileName
    If Err.Number <> 0 Then '删除不了应该是正打开中'
        MsgBox "EXCEL已打开,请关闭"
        Exit Sub
    End If
    On Error GoTo 0
      

  3.   

    非常感谢 Tiger_Zhao 该问题完美解决了 谢谢啊!
      

  4.   

    在VB源程序内 Tiger_Zhao的方法可以完美解决该问题 但是生成exe执行文件后 就不行了?点击“excel”按钮后 不断弹出“EXCEL已打开,请关闭”的提示框。
      

  5.   

    MsgBox 中显示一下 fileName 、Err.Description、Err.Number
      

  6.   

    Private Sub Command1_Click()
       
        Dim fileName As String
     
        fileName = App.Path & "/结果输出.xls"
     
        On Error Resume Next
             Kill fileName
        If Err.Number <> 0 Then '删除不了应该是正打开中'
            MsgBox "EXCEL已打开,请关闭"
            Exit Sub
        End If
        On Error GoTo 0
      
       
       Set xlApp = CreateObject("Excel.Application")
       Set xlBook = xlApp.Workbooks.Add  '新建EXCEL工件簿文件
       xlApp.Visible = True
       Set xlsheet = xlBook.Worksheets(1) '设置活动工作表''
       
       xlsheet.Cells(1, 2) = "初期"
       xlsheet.Cells(1, 3) = "近期"
       xlsheet.Cells(1, 4) = "远期"
       
       xlsheet.SaveAs App.Path & "/结果输出.xls"           '按指定文件名存盘
       Set xlApp = Nothing '释放xlApp对象
       怎么显示“ fileName 、Err.Description、Err.Number”呢?谢谢Tiger_Zhao!
      

  7.   

    MsgBox "EXCEL已打开,请关闭" & vbCrLf & _
           Err.Number & " " & Err.Description
           
      

  8.   

    Tiger_Zhao:
    按照您的方法添加这行代码后:
    1、在工程原文件中,不关闭生成的excel文件,再次单击“EXCEL”按钮,msgbox出现的对话框是“EXCEL已打开,请关闭70拒绝的权限”
    2、将该工程生成执行文件后,点击“EXCEL”按键,直接出现 msgbox对话框“EXCEL已打开,请关闭 53 File not found ”
      

  9.   

    先判断下文件是否存在再删
    If LenB(Dir(fileName)) <> 0 Then
        On Error Resume Next
        Kill fileName
        If Err.Number <> 0 Then '删除不了应该是正打开中'
            MsgBox "EXCEL已打开,请关闭"
            Exit Sub
        End If
        On Error GoTo 0
    End If
      

  10.   

    这次彻底是搞定了 非常谢谢Tiger_Zhao!
    麻烦Tiger_Zhao很久 谢谢啊