Search notes:

Powershell module: COM

get-activeObject get-activeObject tries to simulate the VBA function getObject.
get-COMPropertyValue Returns the value of a collection that is indexed by name or number, returns $null if it does not exist

get-activeObject

getActiveObject tries to simulate the VBA function getObject.
$xls = get-activeObject excel.application
$xls.Workbooks | select-object name 
$xls.run('vba_macro')

$acc = get-activeObject access.application
$acc.currentProject.path + '\' + $acc.currentProject.name
See also the function get-msOfficeComObject in the MS-Office PowerShell module.

Source code

COM.psm1

#
# V0.2
#
set-strictMode -version latest

function init {

   if ($psVersionTable.PsEdition -eq 'Core') {

      $src = get-content -raw "$psScriptRoot\COM.cs"
      add-type -typeDef $src

   }
}

function get-activeObject (
      [string] $progId
   ) {


  if ($psVersionTable.PsEdition -eq 'Core') {
     return [TQ84.COM]::getActiveObject($progId)
  }

  try {
     return [System.Runtime.InteropServices.Marshal]::GetActiveObject($progId)
  }
  catch [System.Runtime.InteropServices.COMException] {

     if ($_.exception.hResult -eq  -2147221021 <# -2146233087 #>) {
     #  write-warning "get-activeObject: progId $progId not found"
        return $null
     }
     throw
  }
}

function get-COMPropertyValue {
 #
 # Returns the value of a collection that is indexed
 # by name or index.
 # Returns null if property does not exist.
 #
   param (
      [__ComObject] $obj,
      [string]      $property
   )

   try {
      return $obj.item($property)
   }
   catch [System.Runtime.InteropServices.COMException]  {

      if ($_.Exception.HResult -eq -2146825023) {
         return $null
      }

      write-host 'get-COMPropertyValue: System.Runtime.InteropServices.COMException'
      $_.exception | select *
   }
}

init
Github repository ps-modules-COM, path: /COM.psm1

COM.psd1

@{
   RootModule        = 'COM.psm1'
   ModuleVersion     = '0.2'
   FunctionsToExport = @(
     'get-activeObject'    ,
     'get-COMPropertyValue'
    )
}
Github repository ps-modules-COM, path: /COM.psd1

COM.cs

using System;
using System.Runtime.InteropServices;

namespace TQ84 {

   public class COM {

     [DllImport("oleaut32.dll", PreserveSig=false)]
      static extern void GetActiveObject(
                                            ref Guid   rclsid,
                                                IntPtr pvReserved,
        [MarshalAs(UnmanagedType.IUnknown)] out Object ppunk
      );

     [DllImport("ole32.dll")]
      static extern int CLSIDFromProgID(
         [MarshalAs(UnmanagedType.LPWStr)]      string lpszProgID,
                                            out Guid   pclsid
      );

      public static object getActiveObject(string progId) {
         Guid clsid;
         CLSIDFromProgID(progId, out clsid);

         object obj;
         GetActiveObject(ref clsid, IntPtr.Zero, out obj);

         return obj;
      }
   }
}
Github repository ps-modules-COM, path: /COM.cs

History

V0.1 Init
V0.2 Add explicit System.Runtime.InteropServicves.COMException type in exception hander of get-activeObject.

TODO

Evaluate and/or interpret HRESULT values in thrown errors.

See also

René's simple PowerShell modules

Index