安装我的插件后 Excel 中使用“1”个参数调用 "Open" 的异常:...”

Exception calling "Open" with "1" argument(s): ..." in Excel after installing my addin

我一直在尝试自动化 excel 自动化,在 excel 中安装我们公司的插件,然后加载库文件(library.xlsm 包含库宏子例程和函数)。在每次测试之后,我加载相应的 test.xlsm 文件并执行宏。所有这一切,我正在使用 powershell (V3) 来做。我的脚本在一台机器上被调用并在另一台远程机器上执行。

这是我安装插件的方式:

kill -processname excel
$Release1RootDir = $workspace + "\Release1"
            $release1Path = Get-ChildItem -Force $release1RootDir
            if($release1Path -eq $Null) {
               echo "Error: No sub-folder found having MyAddin Installer inside "$release1RootDir
            }
            else {
               $release1 = $release1Path.name.replace('_', '.')
               $ExcelAddinInstaller = ($release1Path.FullName + "\MyAddin.msi")
               $ExcelAddinTargetDir = ($Release1Path.FullName)
       $msiexecPath = "msiexec.exe"
       if(Test-Path -Path $ExcelAddinInstaller){
                    echo "Version for MyAddin inside Release1: "$Release1
                    $proc = Start-Process $msiexecPath -ArgumentList /x, `"$ExcelAddinInstaller`", TARGETDIR=$ExcelAddinTargetDir, /quiet, /lvx, "D:\Temp\uninstall.log" -Wait 
            $proc = Start-Process $msiexecPath -ArgumentList /i, `"$ExcelAddinInstaller`", TARGETDIR=$ExcelAddinTargetDir, /quiet, /lvx, "D:\Temp\install.log" -Wait -ErrorAction Stop
                    echo "Installing addin"
                    Start-Process "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe" -ArgumentList /tlb, "C:\Users\Serviceadmin\Addin\MyAddin\Release\MyAddin.dll"
       }
               else{
                    echo $ExcelAddinInstaller
               }
            }  

PS:我正在添加 .dll 文件,因为我的宏中的辅助函数调用了我的 C# 代码。

但是,在打开任何这些 xlsm 文件时,出现以下错误:

Exception calling "Open" with "1" argument(s): "Microsoft Excel cannot access the file 'D:\ABC\XYZ\workspace\library.xlsm'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook." At C:\Users\Serviceadmin\AppData\Local\Temp\hudson.ps1:94 char:3 + $libraryBook = $excel.workbooks.open("$xlLibraryPath$xlLibraryFileName"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

尽管文件的路径是正确的,但仍会出现上述错误。这是我使用 powershell 打开文件的方式:

$excel = new-object -comobject excel.application;
    $excel.visible = $true;
    $libraryBook = $excel.workbooks.open("$xlLibraryPath$xlLibraryFileName");
    $testWorkbook = $excel.workbooks.open("$testFile")
    $excel.Run("$xlLibraryFileName!Initialize", "$testAct")
    $loginsuccess = $excel.Run("$xlLibraryFileName!Login", "$xlenvironment", "$xlUserName", "$xlPassword");
    if($loginsuccess)
    {
      $excel.Run("PerformTest");
      $excel.Run("$xlLibraryFileName!Logout");
    }
    $testWorkbook.close($false)
    $libraryBook.close($false)
    $excel.quit()

我已经验证了以下内容:
1. Excel 安装在所需的机器上- 是的,Excel 2013
2. xlsm 文件的路径 - 全部存在
3.插件安装成功

有什么我遗漏的吗?

提前致谢! :)

嗯,事实证明我必须在以下每个路径中添加文件夹- "Desktop"。该解决方案很奇怪,但它现在对我有用:

C:\Windows\System32\config\systemprofile\Desktop(32 位机器甚至 64 位机器都应该存在)

C:\Windows\SysWOW64\config\systemprofile\Desktop(仅适用于64位机器)