"最新及QUOT; Excel.Application与Excel.Application [英] "New" Excel.Application vs Excel.Application

查看:163
本文介绍了"最新及QUOT; Excel.Application与Excel.Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求对新对象和脚本的影响的澄清。

I am seeking clarification on the impact of "New" on the objects and the script.

我的理解是,如果我需要对excel文档执行操作并且应用程序是关闭的,那么我应该使用新的Excel.Application。

My understanding is that if I need to perform actions on an excel document and the application is closed then I should use New Excel.Application.

如果我保持此应用程序处于活动状态(例如通过一个对象,例如Workbook),然后在脚本我决定打开另一个工作簿,我还应该使用New Excel.Application还是使用Excel.Application呢?

If I keep this application active (through an object such as a Workbook for example) and later in the script I decide to open another workbook, should I still use New Excel.Application or would it be better to use Excel.Application then?

我的关注在于我将编写一个长脚本,将至少执行5个工作簿执行操作。我必须从一个工作簿切换到另一个工作簿,然后回到前一个工作簿。

My concern lies in the fact that I am going to write a long script that will perform actions on at least 5 Workbooks. I will have to a switch from one Workbook to another and then come back to the former...

如果每次脚本创建一个新的Excel.Application,我可以最终有很多的运行,我担心这个混乱会产生问题。

If each time the script creates a New Excel.Application, I may end up having quite a lot of them running and I am fearing that this mess would generate issues.

更适合写这样的东西:

Dim NxlApp as New Excel.Application
Dim xlApp as Excel.Application

  NxlApp.Workbooks.Open "C:\Users\...\WorkbookA.xlsx"
  NxlApp.Visible = True

'Perform actions on WorkbookA (keep it open)


  Set ExcelApp = GetObject("", "Excel.Application.14")
  xlApp.Workbooks.Open "C:\Users\...\WorkbookB.xlsx"
  xlApp.Visible = True

'Perform actions on WorkbookB (keep it open)


'Go back to WorkbookA (using the xlApp variable this time)

  xlApp.Workbook("A.xlsx")...


推荐答案

Excel应用程序可以有多个工作簿打开一次,所以你不必实例化一个新的。如果你从excel里面你不需要引用应用程序来打开工作簿。您可能希望创建几个工作簿实例,例如

The excel application can have several workbooks open at once so you don't need to instancing a new one. If you from inside excel you should not need to reference the application to open workbooks. You might wish to create several workbook instances e.g.

Dim wbWorkbookA As Workbook
Dim wbWorkbookB As Workbook

Set wbWorkbookA = Workbooks.Open("C:\Users\...\WorkbookA.xlsx")
Set wbWorkbookB = Workbooks.Open("C:\Users\...\WorkbookB.xlsx")

这将打开工作簿并给出参考,以便您可以操作他们使用您的代码。

This will open the workbooks and give a reference so that you can manipulate them using your code.

这篇关于"最新及QUOT; Excel.Application与Excel.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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