Looking up the value for MS Office Interop constan

2020-03-02 03:50发布

Using PowerShell, it is easy enough to create, say, an instance of the Excel Application class and start manipulating it:

$app = New-Object -ComObject "Excel.Application"

However, if I need to use the constants like xlDoubleQuote or xlDelimited - it seems like I am forced to hard code them. I would really like to be able to do something like:

$constants = New-Object -ComObject "Excel.Constants"
$constants.xlDoubleQuote

And see that it would return the value of 1. Unfortunately I can't create an instance of an enumeration, and there doesn't seem to be a way to reference it like you would a normal .NET class library:

[Excel.Constants]::xlDoubleQuote

Is there some way to dynamically import that enumeration into PowerShell? Maybe through the managed libraries rather than COM?

5条回答
Fickle 薄情
2楼-- · 2020-03-02 04:04

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
查看更多
啃猪蹄的小仙女
3楼-- · 2020-03-02 04:10

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.

查看更多
甜甜的少女心
4楼-- · 2020-03-02 04:17

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
    # ...
    
查看更多
做个烂人
5楼-- · 2020-03-02 04:19

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
查看更多
再贱就再见
6楼-- · 2020-03-02 04:19

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

查看更多
登录 后发表回答