PowerShell Exception Calling "Unable to cast COM object" - Error loading type library/DLL

0

I have the following script in PowerShell which has been executing fine for months. I did not change anything, but I am receiving the error below. I've tried on other machines, but I keep receiving the same error. Any assistance or guidance would be greatly appreciated. I would have to get our IT involved for any registry or permission changes, and I would have to walk them through the process as well.

Script

# start Excel
$excel = New-Object -comobject Excel.Application
$ms_access = New-Object -comobject Access.Application

function Test-IsFileAvailable {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [string]$Path
    )

    if (!(Test-Path -Path $Path -PathType Leaf)) {
        # the file is not found
        Write-Verbose "File '$Path' not found."
        return $false
    }

    try {
        $file   = New-Object System.IO.FileInfo $Path
        $stream = $file.Open([System.IO.FileMode]::Open, 
                             [System.IO.FileAccess]::ReadWrite, 
                             [System.IO.FileShare]::None)

        if ($stream) { $stream.Close() }
        Write-Verbose "File '$Path' is not locked."
        return $true
    } 
    catch {
        # file is locked by a process.
        Write-Verbose "File '$Path' is locked by another process."
        return $false
    }
}

#set path files
$FilePath_raw = 'N:\DATA\StatusReport_Raw.xlsx'

$FilePath_report = 'N:DATA\StatusReport.xlsx'

$FilePath_date = 'N:\DATA\ReportDate.xlsx'

$FilePath_access = "N:\DATA\Access\Processing.accdb"

if (!(Test-IsFileAvailable $FilePath_raw -Verbose)) { exit }
if (!(Test-IsFileAvailable $FilePath_report -Verbose)) { exit }
if (!(Test-IsFileAvailable $FilePath_date -Verbose)) { exit }
if (!(Test-IsFileAvailable $FilePath_access -Verbose)) { exit }


#make it visible ($true) or invisible ($false)
$excel.Visible = $false

$wb_eval1 = $excel.Workbooks.Open($FilePath_raw)
$ws_eval1 = $wb_eval1.sheets.Item(1)

$wb_eval2 = $excel.Workbooks.Open($FilePath_report)
$ws_eval2 = $wb_eval2.sheets.Item(1)

$wb_date = $excel.Workbooks.Open($FilePath_date)
$ws_date = $wb_date.sheets.Item(1)

$ws_eval2.Cells.Clear()
$lrow2 = $ws_eval1.usedRange.Rows.Count
$range2=$ws_eval1.Range("A3:AA$lrow2")
$range2.copy()
$cpy_range_eval = $ws_eval2.Range("A1")
$ws_eval2.Paste($cpy_range_eval)

$date_range = $ws_eval1.Range("B1")
$date_range.copy()

$cpy_range_date = $ws_date.Range("A2")
$ws_date.Paste($cpy_range_date)

$wb_date.Save()
$wb_date.Close()

$wb_eval2.Save()
$wb_eval2.Close()
$wb_eval1.Close()

$ms_access.OpenCurrentDatabase($Filepath_access)
$ms_access.Run("ExportExcel")

$excel.Quit() 
$ms_access.Quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb_eval1)  | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb_eval2)  | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb_date)   | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)     | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ms_access) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
$excel = $ms_access = $null

$wshell = New-Object -ComObject Wscript.Shell
$output = $wshell.Popup("The task has finished")


Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info 
$balloon.BalloonTipText = 'The reports have been processed.'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME" 
$balloon.Visible = $true 
$balloon.ShowBalloonTip(20000)

Error Message

Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope.  Due to the override, your
shell will retain its current effective execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more information please see "Get-Help
Set-ExecutionPolicy".
At line:1 char:46
+ ...  -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'N ...
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
VERBOSE: File 'N:\DATA\StatusReport_Raw.xlsx' is not locked.
VERBOSE: File 'N:\DATA\StatusReport.xlsx' is not locked.
VERBOSE: File 'N:\DATA\ReportDate.xlsx' is not locked.
VERBOSE: File 'N:\DATA\Access\Processing.accdb' is not locked.
True
True
True
Exception calling "OpenCurrentDatabase" with "1" argument(s): "Unable to cast COM object of type
'Microsoft.Office.Interop.Access.ApplicationClass' to interface type 'Microsoft.Office.Interop.Access._Application'.
This operation failed because the QueryInterface call on the COM component for the interface with IID
'{68CCE6C0-6129-101B-AF4E-00AA003F0F07}' failed due to the following error: Error loading type library/DLL. (Exception
from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))."
At N:\RUN\RUN.ps1:82 char:1
+ $ms_access.OpenCurrentDatabase($Filepath_access)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidCastException

Exception calling "Run" with "1" argument(s): "Unable to cast COM object of type
'Microsoft.Office.Interop.Access.ApplicationClass' to interface type 'Microsoft.Office.Interop.Access._Application'.
This operation failed because the QueryInterface call on the COM component for the interface with IID
'{68CCE6C0-6129-101B-AF4E-00AA003F0F07}' failed due to the following error: Error loading type library/DLL. (Exception
from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))."
At N:\RUN\RUN.ps1:83 char:1
+ $ms_access.Run("ExportExcel")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidCastException

Exception calling "Quit" with "0" argument(s): "Unable to cast COM object of type
'Microsoft.Office.Interop.Access.ApplicationClass' to interface type 'Microsoft.Office.Interop.Access._Application'.
This operation failed because the QueryInterface call on the COM component for the interface with IID
'{68CCE6C0-6129-101B-AF4E-00AA003F0F07}' failed due to the following error: Error loading type library/DLL. (Exception
from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))."
At N:\RUN\RUN.ps1:86 char:1
+ $ms_access.Quit()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidCastException
vba
powershell
asked on Stack Overflow Nov 27, 2019 by MonkeyMonkey

1 Answer

1

So if I am understanding the problem the solution should be quite simple. Before you run the Powershell script right click on it and hit run as administrator. The reason the error is occurring is that you need escalated privileges to set the execution policy. As for the other issue it may be that those files are now protected and therefore unable to load so by using the run as administration option it should allow it to access those files.

I have had several issue in the past where just running with escalated permissions has fixed problems. Now I would not do this as a permanent fix but if this works it would at least get it working again.This is why you shouldn't

answered on Stack Overflow Nov 27, 2019 by Darrell Agee

User contributions licensed under CC BY-SA 3.0