如何在不使用CreateObject的情况下打开Excel并获取工作簿 [英] How to open up excel and get workbook without using CreateObject
本文介绍了如何在不使用CreateObject的情况下打开Excel并获取工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
For a few reasons discussed here I can't open an excel worksheet like this:
Set excel = CreateObject("Excel.Application")
excel.Visible = False
Set excelWb = excel.Workbooks.Open(file_path.xls)
所以我正试图像这样打开它:
So I'm trying to open it like this:
CreateObject("WScript.Shell").Run "excel.exe"
Wscript.Sleep 5000 ' wait for it to load
Set excel = GetObject(,"Excel.Application")
excel.visible = False
Set excelWb = excel.Workbooks.Open(file_path.xls)
但是,这给了我这错误:
ActiveX component can't create object
除非我已经打开了excel,否则我将打开两个页面,如下所示:
Unless I already have excel opened up, or I open up two pages like so:
CreateObject("WScript.Shell").Run "excel.exe"
Wscript.Sleep 5000
CreateObject("WScript.Shell").Run "excel.exe"
Wscript.Sleep 5000
Set excel = GetObject(,"Excel.Application")
excel.visible = False
Set excelWb = excel.Workbooks.Open(file_path.xls)
但是,这给了我一张可见的Excel工作表.我需要打开两个才能使用 GetObject
,这有点奇怪.有什么想法吗?
But this leaves me with one visible excel sheet. And it's sort of weird that I need to open up two in order to use GetObject
. Any ideas around this?
推荐答案
如果您的目标是在后台打开工作簿,则可能是一个更好的选择:
If your goal is to open the workbook in background, this may be a better option:
On Error Resume Next
Dim excel, excelWB
CreateObject("WScript.Shell").Run "excel.exe /e ""C:\Test\blank.xlsx"""
Set excelWB = Nothing
Do
Set excel = GetObject(, "Excel.Application")
Set excelWB = excel.Workbooks("blank.xlsx")
Loop While excelWB Is Nothing
excel.Visible = False
这篇关于如何在不使用CreateObject的情况下打开Excel并获取工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文