开发者

Prevent File Now Available Popup Excel

开发者 https://www.devze.com 2023-04-09 05:43 出处:网络
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:

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:

   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 = Pr开发者_C百科ocess.Start("EXCEL.EXE")
ggProcess.Kill()

The problem is that the following dialog box keeps appearing when I run the code:

Prevent File Now Available Popup Excel

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. The file does not have to be open in order create an OLEDB connection to it.

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.


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. 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).

The solution worked for me especially during testing where you execute the Excel import / export function in fast succession.

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. 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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消