用户完成后关闭Excel [英] Close Excel when user is finished

查看:204
本文介绍了用户完成后关闭Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务很简单:


  1. 我想从VB.net GUI打开Excel文档(.xls)

  2. 用户将处理Excel文件

  3. 完成后关闭Excel文件

  4. 我想要发布Excel对象的VB.net代码

  1. I want to open an Excel document (.xls) from VB.net GUI
  2. The user will work on the Excel file
  3. The user closes the Excel file when it is finished
  4. I want the VB.net code to release the Excel object

问题是当用户关闭文件时,Excel对象仍然可以在任务管理器的进程选项卡上看到。如果我打开文件后立即写入 XlApp.quit(),用户就不会有机会做任何事情。如何判断用户何时关闭Excel文件,以便我可以运行代码来释放Excel对象?

The problem is that when the user closes the file, the Excel object still can be seen on the process tab of the task manager. If I were to write XlApp.quit() immediately after opening the file, the user won't get a chance to do anything. How can I tell when the user closes the Excel file so I could run the code to release the excel object?

到目前为止我已经有:

Dim xlapp as new Excel.Application
Dim xlwb as excel.workbook = xlapp.workbooks.open("file path")
xlapp.visible = true

'The user do work here'
'What should I put in between here to detect when the user exits the excel file???"

xlwb.close()
xlapp.quit()
releaseObject(xlwb)
releaseObject(xlApp)


推荐答案

不久之前,我已经遇到了这个问题,我发现我的解决方案适用于你的问题,请注意,我已经在Excel中进行了VBA,所以也许需要进行一些调整,我认为这个解决方案应该适合你但是:

I have had this problem not so long ago. I found my solution and adapted it to your question. Note that I have done this in VBA in Excel, so perhaps you need to make some adjustments. I think that the solution should work for you though:

以下是让用户编辑excel文件的代码:

Here is the code that lets the user edit the excel file:

Dim xlapp as new Excel.Application
Dim xlwb as excel.workbook = xlapp.workbooks.open("T:\tmp\Book1.xlsx")
xlapp.visible = true

'Loop until the Excel file is closed.
'I though I had to check if the user closed Excel and not just the excel-file,
'but it seems that Excel does not close even if the user does "Exit Excel".
Do 
    Sleep 1000  
    'Keep on looping as long as the Excel-file is part of the open workbooks.
Loop While objExist(xlApp.Workbooks,"Book1.xlsx")

'xlwb.Close    You can't run this command, as the workbook is already closed
xlapp.Quit
releaseObject (xlwb)
releaseObject (xlapp)

在VBA中使用Sleep,我需要在模块的开头声明它:

To be able to use Sleep in VBA, I need to declare it in the beginning of the module:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

这是我的objExist代码:

This is my code for objExist:

Public Function ObjExist(objCollection As Variant, iname As String) As Boolean
'------------------------------------------------------------
'Purpose    Tell if a named element is part of a collection
'Expects    obCollection    A collection of objects (i.e Worksheets, Tables)
'           iname           The name of the object that we want to look for
'Returns    True if the named object exists in the collection, otherwise False.
'           Note that it returns FALSE also if any other error occurs, like
'           the objCollection not being a collection.
'------------------------------------------------------------
Dim a As Object
    On Error GoTo DoesNotExist
    Set a = objCollection(iname)
    ObjExist = True
    Set a = Nothing
    Exit Function

DoesNotExist:
    ObjExist = False

End Function

祝你好运!

这篇关于用户完成后关闭Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆