简体   繁体   English

防止文件现在可用弹出 Excel

[英]Prevent File Now Available Popup Excel

I have a problem here that I've been working on for hours.我在这里有一个问题,我已经工作了几个小时。

I'm importing an Excel file, and I use this code to do it:我正在导入一个 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()

The problem is that the following dialog box keeps appearing when I run the code:问题是当我运行代码时不断出现以下对话框:

弹出

How can I prevent this pop-up dialog box from appearing?如何防止出现此弹出对话框?

As poweruser has suggested can simply change正如高级用户所建议的那样可以简单地改变

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

to

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

Ok since that didn't work I've taken a more detailed look at the code and think your issue is because you are opening the excel file, then creating an external connection to it.好的,因为那不起作用,我更详细地查看了代码,并认为您的问题是因为您打开了 excel 文件,然后创建了到它的外部连接。 The file does not have to be open in order create an OLEDB connection to it.不必打开该文件即可创建到它的 OLEDB 连接。

It appears that you don't know the tab names in advance so you need to open the file to get the tab names?似乎您事先不知道选项卡名称,因此您需要打开文件以获取选项卡名称?

If so I would create a method to open the file get the tab names and return an array of the tab names which you can then iterate through in your code above, then close the file.如果是这样,我将创建一个方法来打开文件,获取选项卡名称并返回一个选项卡名称数组,然后您可以在上面的代码中对其进行迭代,然后关闭文件。

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

make TWB as nothing at the end of the Sube procedure.在 Sube 程序结束时将 TWB 设为空。

From my investigation if you use the creation of excel and quitting in fast succession, it will not immediately close the previously quit excel object.根据我的调查,如果您使用创建excel并快速连续退出,它不会立即关闭之前退出的excel对象。 I noticed that after the xlApp.Quit, it takes a while before the real EXCEL.exe process is gone even if I have done all the cleanup (closing the worksheet, setting Excel COM object to nothing).我注意到在 xlApp.Quit 之后,即使我已经完成了所有清理工作(关闭工作表,将 Excel COM 对象设置为空),真正的 EXCEL.exe 进程也需要一段时间才能消失。

The solution worked for me especially during testing where you execute the Excel import / export function in fast succession.该解决方案对我有用,尤其是在快速连续执行 Excel 导入/导出功能的测试期间。

Note that my code was derrived from the following 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

Whilst others have, understandably, pointed out issues in the OP's methodology, I've been looking for a solution to the issue since a recent Office update started causing the same problem in Excel 2010, for an "application" for which I'm responsible - where the dialog is being generated spuriously and the file is available for reading.虽然其他人可以理解地指出了 OP 方法中的问题,但自从最近的 Office 更新开始在 Excel 2010 中导致相同问题以来,我一直在寻找该问题的解决方案,这是我负责的“应用程序” -在被虚假所产生的对话框和文件可以阅读。 The solution - is:解决方案 - 是:


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

To reiterate, this successfully stops the spurious File Now Available dialog for both this and subsequent loads, as long as it is actually spurious.重申一下,这成功地停止了这次后续加载的虚假文件现在可用对话框,只要它实际上是虚假的。 If it's not spurious, your routine will likely error.如果它不是虚假的,您的例程可能会出错。

You can set to the Excel.Application visible and display alerts to false:您可以将 Excel.Application 设置为可见并将警报显示为 false:

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

Then when you close the books the file no available alert will not appear然后,当您关闭书籍时,不会出现文件无可用警报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM