防止文件现在可用弹出Excel

时间:2011-09-30 04:02:27

标签: excel vba

我有一个问题,我已经工作了好几个小时。

我正在导入Excel文件,我使用此代码执行此操作:

   Dim objExcel As Excel.Application
        Dim objWorkBook As Excel.Workbook
        Dim totalWorkSheets As Excel.Worksheet
        Dim ExcelSheetName As String = ""

        objExcel = CreateObject("Excel.Application")
        objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False)
        objExcel.DisplayAlerts = False
        Dim exConS As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & excelfile & ";Extended Properties=Excel 8.0;"


        For Each totalWorkSheets In objWorkBook.Worksheets
            ExcelSheetName += totalWorkSheets.Name
            Dim exCon As New OleDbConnection(exConS)
            Dim dsExcel As New DataSet()
            exCon.Open()
            Dim sExcel As String = "SELECT * FROM [" & totalWorkSheets.Name & "$]"
            Dim daExcel As New OleDbDataAdapter(sExcel, exCon)
            daExcel.Fill(dsExcel)
            exCon.Close()
         Next totalWorkSheets

 objWorkBook.Close()
objExcel.Quit()
Dim ggProcess As New Process
ggProcess = Process.Start("EXCEL.EXE")
ggProcess.Kill()

问题是我运行代码时会出现以下对话框:

pop up

如何阻止此弹出式对话框出现?

5 个答案:

答案 0 :(得分:0)

正如poweruser建议的那样可以简单地改变

objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False)

objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False, Readonly:=True)

好的,因为那不起作用我已经对代码进行了更详细的研究,并认为你的问题是因为你正在打开excel文件,然后创建一个外部连接。该文件不必打开,以便创建与它的OLEDB连接。

您似乎事先不知道标签名称,因此需要打开文件才能获取标签名称?

如果是这样,我会创建一个方法来打开文件获取选项卡名称并返回一个选项卡名称数组,然后您可以在上面的代码中迭代,然后关闭文件。

答案 1 :(得分:0)

Use Set 
let TWB as workbook
Set TWB=workbooks.open(Filename:="\\......", Readonly:=false, notify:=False)

在Sube程序结束时将TWB视为无效。

答案 2 :(得分:0)

根据我的调查,如果你使用excel的创建并快速连续退出,它将不会立即关闭先前退出的excel对象。我注意到在xlApp.Quit之后,即使我已经完成所有清理工作(关闭工作表,将Excel COM对象设置为空),真正的EXCEL.exe进程消失也需要一段时间。

该解决方案对我有用,特别是在您快速连续执行Excel导入/导出功能的测试期间。

请注意,我的代码来自以下link

Function Excel_Conversation()
   On Error GoTo Proc_Err

   Dim xlApp As Excel.Application, _
      booLeaveOpen  As Boolean

   'if Excel is already open, use that instance
   booLeaveOpen = True

   'attempting to use something that is not available
   'will generate an error
   On Error Resume Next
   Set xlApp = GetObject(, "Excel.Application")
   On Error GoTo Proc_Err

   'If xlApp is defined, then we
   'already have a conversation
   If TypeName(xlApp) = "Nothing" Then
      booLeaveOpen = False
      'Excel was not open -- create a new instance
      Set xlApp = CreateObject("Excel.Application")
   End If

   'Do whatever you want
Proc_Exit:
   On Error Resume Next

   If TypeName(xlApp) <> "Nothing" Then
      xlApp.ActiveWorkbook.Close False
      If Not booLeaveOpen Then xlApp.Quit
      Set xlApp = Nothing
   End If

   Exit Function

Proc_Err:
   MsgBox Err.Description _
     , , "ERROR " & Err.Number & "   Excel_Conversation"
   'comment next line after debugged
   Stop:   Resume

   Resume Proc_Exit
End Function

答案 3 :(得分:0)

虽然其他人可以理解地指出OP方法中的问题,但我一直在寻找解决方案,因为最近的Office更新开始在Excel 2010中导致同样的问题, &#34;应用&#34;对此我负责 - 虚假地生成对话框,文件 可供阅读。解决方案 - 是:


  Application.DisplayAlerts = False
  Set oBook = Application.Workbooks.Open(Filename:=strFilename, ReadOnly:=False, Notify:=False)
  Application.DisplayAlerts = True

重申一下,只要它实际上是假的,这就成功地阻止了这个后续加载的虚假文件现在可用对话框。如果它不是虚假的,那么你的例程可能会出错。

答案 4 :(得分:0)

您可以设置为Excel.Application可见并将警报显示为false:

Dim objExcel As Excel.Application
objExcel.Visible = False
objExcel.DisplayAlerts = False
...

然后,当您关闭书本时,不会显示文件无可用警报