0

I made a function to upload documents and it worked great for a few months, then suddenly I was getting errors relating to failing to create the COM component. It sometimes will run 50x successfully, sometimes, just 5 or 6 times. I added a "catch" statement along with a forceful kill of all IE processes, but can't get around the error and it's driving me mad! Any suggestions? See Function Below.

Error:

Script Failed, please re-try, report error accordingly.

Creating an instance of the COM component with CLSID {0002DF01-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 800706bf The remote procedure call failed and did not execute. (Exception from HRESULT: 0x800706BF).

Press Enter to continue...:

Function:

function Do-UploadReport {
    param (
        [String]
        $FileName,
        [String]
        $FileType,
        [String]
        $WONum
    )
    $url = "http://URL/$WONum"
    sleep 2
    get-process iexplore 2> $null | stop-process 2> $null
    try
    {
        $ie = New-Object -com InternetExplorer.Application
    }
    catch
    {
        Write-Output "Script Failed, please re-try, report error accordingly."
        Write-Host $_
        Pause
        Exit
    }
    $ie.TheaterMode = $True
    $ie.visible = $true
    $wshell = New-Object -ComObject wscript.shell;
    $wshell.AppActivate($ie.Name)
    Start-Sleep -Milliseconds 500
    $ie.navigate($url)
    While ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
    $link = $ie.Document.getElementById("file")
    Do-WindowsFileDiagSelection
    $link.Click()
    sleep 1
    $wshell.SendKeys('^f')
    sleep 1
    $wshell.SendKeys('Upload a File')
    sleep 1 
    $wshell.SendKeys('{TAB}{TAB}{TAB}{TAB}{TAB}') #On Report Type Selector
    sleep 1
    If ($FileType -eq "WORKSHEET") {$wshell.SendKeys('{DOWN}')}
    If ($FileType -eq "CLIENT_FILE") {$wshell.SendKeys('{UP}')}
    sleep 1
    $wshell.SendKeys('{TAB}')
    sleep 1
    $wshell.SendKeys('{ENTER}')
    If ($FileType -eq "WORKSHEET") {sleep 15}
    If ($FileType -eq "CLIENT_FILE") {sleep 35}
    If ($FileType -eq "SUBMITTAL") {sleep 50}
    $ie.TheaterMode = $False
    }
3
  • You are creating the $ie com object, but never seem to remove it from memory with $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie). After some time you'll run out of resources Commented Jan 28, 2021 at 22:02
  • @Theo would you add that after the get|stop line? I thought by forcefully killing IE I would be resolving that issue. Commented Jan 28, 2021 at 22:22
  • 2
    I'm in favor of Theo's comment. You need to destroy the IE Com Object when you finish using it. I also find a similar thread and you can also refer to the accepted answer in it. I think you should put the clear memory code in the end when you finish using IE object. Commented Jan 29, 2021 at 6:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.