execWB适用于Excel VB脚本,但相同的代码会在Powershell脚本中引发RPC异常

时间:2014-08-09 21:40:02

标签: excel vba excel-vba powershell

Powershell版本的错误.....

  

异常通话"退出"用" 0"参数:" RPC服务器不可用。 (HRESULT异常:0x800706BA)"   在C:\ Users \ robp \ Desktop \ nbrs.ps1:19 char:13   + $ ie.quit<<<< ()       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:ComMethodCOMException

     

异常调用" ExecWB"用" 2"参数:"远程过程调用失败。 (来自HRESULT的异常:0x800706BE)"   在C:\ Users \ robp \ Desktop \ nbrs.ps1:12 char:19   + $ ie.ExecWB<<<< (6,2)       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:ComMethodTargetInvocation

Powershell版本......

$reader = [System.IO.File]::OpenText("c:\users\robp\desktop\conveyance.txt")
try {
    $ie = New-Object -comobject InternetExplorer.Application 
    $ie.visible = $true 
    # $ie.silent = $true 
    for(;;) {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        $url = "http://xxx.xxx.xxx.xxx/specdata/specdata.asp?requiredmcpartno=" + $line
        $ie.Navigate( $url )
        while( $ie.busy){Start-Sleep 4} 
        $ie.ExecWB(6,2)
    }
    while( $ie.busy){Start-Sleep 4} 
    $ie.quit()
}
finally {
    while( $ie.busy){Start-Sleep 4} 
    $ie.quit()
    $reader.Close()
}

excel中的VBA后面的按钮....

Private Sub CommandButton1_Click()
    Dim urlData As String
    Dim part As String
    Dim myFile As String

    myFile = "N:\conv\conveyance.txt"


    Const OLECMDID_PRINT = 6
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2
    Const PRINT_WAITFORCOMPLETION = 2

    Set Browser = CreateObject("InternetExplorer.Application")

    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, part
        urlData = "http://xxx.xxx.xxx.xxx/specdata/specdata.asp?requiredmcpartno=" + part
        Browser.navigate (urlData)
        Browser.Visible = True

        Do While Browser.ReadyState <> 4
            Application.Wait (1000)
        Loop

        Browser.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    Loop
    Close #1
End Sub

0 个答案:

没有答案