How to initialize ComObject for Microsoft Access in PowerShell

37 views Asked by At

I am trying to programmatically access my Microsoft Access 2016 database (.accdb, but I can use .accde as well) using PowerShell (for automation, command-line use, etc). However, when I check the value of the object after initialization, I receive an error.

Following is the code I'm having trouble with.

$accessApp = New-Object -ComObject Access.Application

After initialization,

$accessApp

The result is:

format-default : Error HRESULT E_FAIL has been returned from a call to a COM component.
    + CategoryInfo          : NotSpecified: (:) [format-default], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.FormatDefaultCommand

However, I can enter:

$accessApp.GetType()

To get:

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     False    ApplicationClass                         System.__ComObject  

When I enter:

$accessApp.CurrentProject | Select *

I get:

AllForms                   : 
AllReports                 : 
AllMacros                  : 
AllModules                 : 
AllDataAccessPages         : 
ProjectType                : 
BaseConnectionString       : 
IsConnected                : True
Name                       : 
Path                       : 
FullName                   : 
Connection                 : 
Properties                 : 
Application                : Microsoft.Office.Interop.Access.ApplicationClass
Parent                     : Microsoft.Office.Interop.Access.ApplicationClass
RemovePersonalInformation  : False
FileFormat                 : 
AccessConnection           : 
ImportExportSpecifications : 
IsTrusted                  : 
WebSite                    : 
IsWeb                      : 
Resources                  : 
IsSQLBackend               : 

Most of the values are null. I'd like to be able to get things like All Reports from this variable, but I cannot.

To me it seems like there is an issue when initializing the ComObject. Is this expected behavior and something I just need to work around, or does there appear to be something wrong?

Let me know if more information is needed. This is my first post.

UPDATE $accessApp.OpenCurrentDatabase($DatabasePath) must be added to connect to the database before accessing all reports. $accessApp still displays an error, but not when used with Select, so perhaps there is just not a default view.

0

There are 0 answers