[英]VBA run code in another instance of excel and detach
I currently have the below code which opens a workbook in a new instance of excel (The workbook that is opened has an on open macro). 我目前有以下代码在excel的新实例中打开工作簿(打开的工作簿有一个打开的宏)。
However when the code is running in the new instance, this also takes up the existing instance of excel and only frees it once the workbook open macro has run.Is there a way to open excel in a new instance and detach/stop the code from the existing workbook immediately? 但是当代码在新实例中运行时,这也会占用excel的现有实例,并且只有在工作簿开放宏运行后才释放它。有没有办法在新实例中打开excel并从中分离/停止代码现有的工作簿立即?
Please see code below. 请参阅下面的代码。
Sub OpenWBInNewInstance()
Dim xApp As Excel.Application
Set xApp = New Excel.Application
xApp.Visible = True
Dim wb As Excel.Workbook
Set wb = xApp.Workbooks.Open("Z:\MI\Not Purchased Detail\Not Purchased Detail Disposal Marketing Template.xlsb")
End Sub
Code execution runs on a single thread in VBA, so you can't do what you're asking because the execution won't move to the next line until the entire Workbooks.Open
line has been processed - which includes the Workbook_Open
event in the other workbook. 代码执行在VBA中的单个线程上运行,因此您无法执行所要求的操作,因为在处理完整个
Workbooks.Open
行之前执行不会移动到下一行 - 其中包括Workbook_Open
事件其他工作簿。
A possible workaround it to use VBScript instead: 它可能会改为使用VBScript:
Sub OpenWBInNewInstance()
Open Environ$("USERPROFILE") & "\Desktop\temp.vbs" For Output As #1
Print #1, "Set xl = CreateObject(""Excel.Application"")"
Print #1, "xl.Workbooks.Open ""Z:\MI\Not Purchased Detail\Not Purchased Detail Disposal Marketing Template.xlsb"""
Close #1
Shell Environ$("USERPROFILE") & "\Desktop\temp.vbs"
'// You will need some kind of hook here to set the workbook in other instance...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.