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

↘锁芯ラ 提交于 2019-11-30 19:41:22

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

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
knb

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

Using Visio 2016 32-bit, I found that attempting to use Keith Hill's approach resulted in error message: "Add-Type : Cannot add type. The assembly 'Microsoft.Office.Interop.Visio' could not be found." Shay Levy's approach required a bit more modification to work with Visio. Here is what I was able to get working:

$visioApp = New-Object -ComObject Visio.Application 
$visioConstants = @{}

$visioEnums = $visioApp.gettype().assembly.getexportedtypes() | where-object {$_.IsEnum } #-and $_.name -eq 'constants'}

The answers of Keith Hill and Shay Levy are best, however, I found that Visio 
$visioEnums |%{
    $enumname = $_
    [enum]::getNames($enumname) | foreach {
        $val = invoke-expression "[$($enumname)]::$($_).value__"
        $visConstants.$_ = $val
    }
}
$visioApp.Quit()

echo $visConstants.visTopEdge 

Disappointingly, it takes around 12 seconds to execute when I tested.

To combine the technique from Keith Hill's helpful answer with the tab-completion idea from Shay Levy's answer:

# Instantiate Excel, which implicitly loads the interop
# assembly that contains the [enum] type of interest.
# Assign to $null, if you're not planning on using the object further.
$xl = New-Object -ComObject Excel.Application

# Store the [enum] type of interest in a variable for easier access.
$xlConstants = [Microsoft.Office.Interop.Excel.Constants]

Note: To find the full name of the Constants type, you can use tab completion: after having run New-Object -ComObject Excel.Application, type [constants<tab> (don't type the closing ]), which should expand to [Microsoft.Office.Interop.Excel.Constants; if a type from a namespace other than Microsoft.Office.Interop.Excel shows up, press the tab key until the desired type is found.

Now you can:

  • Access the individual enumeration values as static members of the type stored in $xlConstants, via the :: operator, which also works with tab completion; e.g.:

    $xlConstants::xl3d<tab>  # -> expands to $xlConstants::xl3DBar
    
  • Get a specific value's underlying number by either casting to [int] or accessing the .Value__ property:

    [int] $xlConstants::xl3DBar    # -> -4099
    
    $xlConstants::xl3DBar.Value__  # ditto
    
  • Enumerate all symbolic names:

    [enum]::GetNames($xlConstants)
    
  • Also show the numbers underlying the symbolic names:

    PS> [enum]::GetNames($xlConstants) |
        Select @{ n='Name'; e={$_} }, @{ n='Number'; e={ $xlConstants::$_.Value__ } }
    
    Name            Number
    ----            ------
    xlAbove              0
    xlFirst              0
    xlDirect             1
    # ...
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!