VBS启动一个excel的实例,然后从它脱离 [英] VBS Start an instance of excel then detach from it
问题描述
所以我基本上有一个VBS脚本,应该是将数据以异步方式发布到Excel表。我现在通过在工作簿的路径上使用GetObject来执行此操作:
So I basically have a VBS script that's supposed to post data to an Excel sheet asynchronously. I currently do this by using GetObject on the workbook's path like so:
Set xlBook = GetObject(strPath & "\Runner.xlsm")
这似乎工作正常,除了工作簿将在脚本,如果它以前没有打开(不需要,我有一个宏,将关闭并保存书需要)。
This seems to work fine, except that the workbook will close at the end of the script if it was not open previously (not desired, I have a macro that will close and save the book when necessary).
这是类似于问题7708039 ,除了我想故意保持excel实例打开,不强制它关闭(与他的问题相反)
This is similar to Question 7708039, EXCEPT I want to intentionally keep the excel instance OPEN, not force it to close (the reverse of his problem).
我认为这是关闭,因为引用对象的变量在脚本结束时被破坏,但是我无法弄清楚释放这些句柄而不破坏它们(即设置为Nothing)。
I think it's closing because the variables referencing the object get destroyed at the end of the script, but I can't figure out how to release those handles without destroying them (i.e. set to Nothing).
推荐答案
而不是获得对特定工作簿的引用,你试过获得Excel的参考,然后打开工作簿?
Instead of getting a reference to a specific workbook, have you tried getting a reference to Excel and then opening the workbook?
' 1a. Get an existing Excel instance...
Set Excel = GetObject(, "Excel.Application")
' 1b. Or, create one. Make it visible for testing.
Set Excel = CreateObject("Excel.Application")
Excel.Visible = True
' Load the workbook...
Set Workbook = Excel.Workbooks.Open(strPath & "\Runner.xlsm")
' Do stuff and save, if desired.
' Close workbook...
Workbook.Close
' Excel stays open. If you want to close Excel, use:
Excel.Quit
这篇关于VBS启动一个excel的实例,然后从它脱离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!