我可以将 Excel 应用程序对象设置为指向已打开的 Excel 实例吗? [英] Can I set a Excel Application Object to point to an already open instance of Excel?

查看:18
本文介绍了我可以将 Excel 应用程序对象设置为指向已打开的 Excel 实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个导出工具来将数据推送到 Excel.然而,在第一次推送后,Excel 保持打开状态,有时用户可能希望在将更多数据推送到同一个 Excel 工作表中的另一个页面时让 Excel 保持打开状态.我通常只在导出完成后释放 COM 对象.我的方法现在的工作方式涉及每次导出时打开一个新的 excel 实例...

I am currently trying to create an export tool to push data into Excel. However after the first push, Excel is left open, and sometimes a user may want to leave Excel open while they push more data to another page in the same excel sheet. I usually just release the COM objects once the export is done. The way my methods work right now involves opening a new instance of excel every time I export...

创建应用程序的全局实例以从我开始导出到 Excel 应用程序关闭时连续使用是否有益?

would it be beneficial to create a global instance of the application to use continuiously from the point I begin exporting until the point of the Excel application being closed?

我是否应该使用事件处理程序来检测 Excel 何时关闭,以便知道何时释放 COM 对象?

Should I be using event handlers to detect when Excel has been closed so I know when to release the COM objects?

如果我确实尝试创建 Excel 应用程序的全局实例,我可以实例化它并等待释放它,这样我就不需要再次尝试找到 excel 的打开实例..但如果我需要找到并分配给一个变量,一个打开的 Excel 实例(以前存在于代码中但释放了 COM 对象),我该怎么做?

If I do try to make a global instance of an Excel Application, I can instantiate it and wait to release it, that way I don't need to try and find open instances of excel again.. but if I needed to find, and assign to a variable, an open instance of Excel (that has previously existed in code but had the COM objects released), how would I do this?

推荐答案

我过去用过这个.tPath 是我应该打开/重用的 excel 文件的路径.

I have used this in the past. tPath is the path to my excel file that should be opened/reused.

Dim exApp As New Excel.Application

'finds the workbook.  If it is already open, then it uses that.  Otherwise, it opens in new instance
Dim wb As Excel.Workbook
wb = System.Runtime.InteropServices.Marshal.BindToMoniker(tPath)
exApp = wb.Parent

这篇关于我可以将 Excel 应用程序对象设置为指向已打开的 Excel 实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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