Looking up the value for MS Office Interop constants rather than hard coding them Looking up the value for MS Office Interop constants rather than hard coding them powershell powershell

Looking up the value for MS Office Interop constants rather than hard coding them


Use the primary interop assembly for Excel. If you have Office installed these should be in the GAC. Use like so:

Add-Type -AssemblyName Microsoft.Office.Interop.Excel[int][Microsoft.Office.Interop.Excel.Constants]::xlDoubleQuote


Keith already gave you the answer, here's another option. You can use tab completion on the $xlConstants object to get the constants:

$xl = New-Object -ComObject Excel.Application$constants = $xl.gettype().assembly.getexportedtypes() | where-object {$_.IsEnum -and $_.name -eq 'constants'}$pso = new-object psobject[enum]::getNames($constants) | foreach { $pso | Add-Member -MemberType NoteProperty $_ ($constants::$_) }$xlConstants = $pso


Keith and Shay gave perfect answers, however, note this:

When using Excel 2003 or Excel 2007, the Office Primary Interop Assemblies (PIAs) should be installed on the machine. There are redistributable versions available from Microsoft. See this stackoverflow.com posting here for more info:

Different Interop references on two different computers doesn't work