How to open up excel and get workbook without usin

2019-08-06 01:21发布

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)

However, this gives me this error:

ActiveX component can't create object

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)

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?

1条回答
相关推荐>>
2楼-- · 2019-08-06 02:16

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
查看更多
登录 后发表回答