简体   繁体   English

从Powershell启动时,VBA中的“对象'工作簿'的方法'打开'失败”,但直接从Excel调用时有效

[英]“Method 'Open' of object 'Workbooks' failed” in VBA when launched from a Powershell, but work when directly call from Excel

I'm stuck on this problem. 我被困在这个问题上。 An user will go to holiday and want to automate some actions on his Excel. 用户将去度假,并希望在其Excel上自动执行某些操作。 I've created a very basic Powershell for the Windows Task Scheduler that open a first Excel file, launch a macro and close: 我为Windows Task Scheduler创建了一个非常基本的Powershell,它可以打开第一个Excel文件,启动宏并关闭:

$target = "G:\ES\GI\xxxx\xxxxMailer.xlsm" #Where is my file
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $true
$workbook = $objExcel.Workbooks.Open($target) #Open my file
$objExcel.Run("Callxxxx") #Launch a macro in the target file
$workbook.close($false)
$objExcel.Quit()
[Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)

In the macro I have this very simple line (so in VBA this time) for opening an other workbook: 在宏中,我有这条非常简单的行(因此这次在VBA中)用于打开其他工作簿:

Workbooks.Open Filename:="G:\ES\GI\xxxxx\destinations xxxxxx 2018.xlsm", _
    UpdateLinks:=3, _
    ReadOnly:= False

My problem is the following: when I launch the macro directly from the first workbook, the second one open without any problem; 我的问题如下:当我直接从第一个工作簿启动宏时,第二个打开没有问题。 but when I try to launch the process from Powershell, I got the error "Method 'Open' of object 'Workbooks' failed". 但是当我尝试从Powershell启动该过程时,出现错误“对象“工作簿”的方法“打开”失败”。

I tried to change all properties from Workbooks.Open, disable macro at StartUp from the destination file, same process work with others files and not this one. 我试图更改Workbooks.Open的所有属性,从目标文件禁用StartUp的宏,相同的过程可以处理其他文件,而不是此文件。 I'm short on ideas. 我缺乏主意。

The problem came from specific addin in the targetted workbook. 问题来自目标工作簿中的特定插件。 The $ObjExcel don't run with them loaded correctly. $ ObjExcel不能在正确加载的情况下运行。 I finally assign the $ObjModel to the full Excel path containing all addins, and not to a "-New-Object" who's running on separate instance. 最后,我将$ ObjModel分配给包含所有插件的完整Excel路径,而不是分配给在单独实例上运行的“ -New-Object”。 Everything working fine after that. 之后一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel打开只读工作簿时,对象“工作簿”的方法“打开”失败 - excel Method 'open' of object 'workbooks' failed error when opening a read only workbook 对象“工作簿”的方法“打开”失败 - Method 'Open' of object 'Workbooks' failed Excel错误代码1004 - 对象工作簿的方法打开失败 - Excel error code 1004 - method open of object workbooks failed 无法从 Access VBA 后期绑定 excel - Workbooks.Open 不起作用 - Unable to late bind excel from Access VBA - Workbooks.Open doesn't work 打开两个Excel工作簿(每个都有VBA代码)时,如何在工作簿之间切换代码视图? - When two Excel workbooks are open, each with VBA code, how do I switch the code view between workbooks? 搜索lastrow VBA Excel时,对象_worksheet的方法“范围”失败 - Method “Range” of object _worksheet failed when searching for lastrow VBA Excel 对象工作簿的方法打开失败错误 - Method open of object workbooks failed error 方法“添加”对象“工作簿”失败 - 使用vba Access 2007导入excel工作簿 - Method 'Add' of object “workbooks” failed - Importing excel workbook with vba Access 2007 从WCF服务调用时,如何解决Excel.Application.Workbooks.Open失败? - How to resolve Excel.Application.Workbooks.Open failing when called from WCF Service? 从PHP网站执行VBScript Excel Workbooks.Open无法正常工作 - VBScript Excel Workbooks.Open Not Working When Executed from PHP Website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM