[英]“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.