1

我一直在尝试自动化 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 文件时,我收到以下错误:

使用“1”参数调用“打开”的异常:“Microsoft Excel 无法访问文件 'D:\ABC\XYZ\workspace\library.xlsm'。可能的原因有多种:文件名或路径不存在。该文件正被另一个程序使用。您尝试保存的工作簿与当前打开的工作簿同名。" 在 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. 插件安装成功

有什么我想念的吗?

提前致谢!:)

4

1 回答 1

0

好吧,事实证明我必须在以下每个路径中添加文件夹 - “桌面”。解决方案很奇怪,但它现在对我有用:

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

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

于 2016-08-20T07:00:12.450 回答