尝试从Word宏运行Powershell脚本时出现“权限被拒绝”错误

时间:2018-09-03 15:02:16

标签: vba powershell ms-word

我需要使用word宏中的参数运行Powershell脚本。目前,即使只是简单地从VBA运行脚本的尝试也不成功。我执行以下操作:

Sub Powershell_Run()

sCmd = "powershell -file ""C:\Users\i351063\Desktop\Scripts\NewAttempt.ps1"""
Set xShell = CreateObject("Wscript.Shell")
Set xShellExec = xShell.Run(sCmd)
rReturn = xShellExec.Run("C:\Users\i351063\Desktop\Scripts\NewAttempt.ps1")

End Sub

此代码的执行将在Set xShellExec = xShell.Run(sCmd)行上返回错误:“运行时错误'70':权限被拒绝”。我做错了什么?如何解决错误? 提前非常感谢!

UPD:Powershell代码(我想将文件名作为参数从VBA传递到PS,现在它已经在代码中初始化了

Param([string]$filename)
$filename = "HT.docm"

Add-Type -AssemblyName "Microsoft.Office.Interop.Word" 
$word = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$wshell = New-Object -ComObject Wscript.Shell

$doc = $word.Documents | Where-Object {$_.Name -eq "$filename"}
    If (!$doc) 
         {
            $form.Close()
            [void] $wshell.Popup("Failed to find an opened report",0,"Report not found",0x1)
            $word.ScreenUpdating = $true
            Break
        }
    Else {...}

0 个答案:

没有答案