[enum]::Getnames($xlConstants) fails

Welcome Forums General PowerShell Q&A [enum]::Getnames($xlConstants) fails

Viewing 3 reply threads
  • Author
    • #159375
      Topics: 86
      Replies: 138
      Points: 677
      Rank: Major Contributor

      I have this one line of code:


      the purpose being to enumerate all symbolic names (in EXCEL).

      I am getting the error message:

       Exception calling GetNames with "1" arguments. "Value cannot be null.  Parameter name: enumType"

      Why?  Would be grateful for any tips and advice.

    • #159377
      Topics: 12
      Replies: 527
      Points: 1,244
      Helping Hand
      Rank: Community Hero
      #Requires -Version 5
      enum xlConstants {

      You’re trying to pass an empty variable to the GetNames method of the enum class. That method accepts an enum type as in the example above.
      Enum values can have no spaces, special characters, or numbers.

    • #159380
      Topics: 2
      Replies: 1013
      Points: 2,093
      Helping Hand
      Rank: Community Hero

      This is not a PowerShell see, you just happen to be doing that here.
      This is a raw .Net namespace call and you are bound to what it expects and PowerShell can only abide by that.

      Enum.GetNames(Type) Method
      Namespace: System
      Assemblies: System.Runtime.dll, mscorlib.dll, netstandard.dll

      # C#
              Console.WriteLine("The members of the Styles enum are:");
              foreach(string s in Enum.GetNames(typeof(Styles)))

      So, like this articles shows, you need to be passing an array.
      Working With Enums In PowerShell

      How are you populating …


      See also this:
      #PSTip Working with Excel constants

      # create Excel object
      $xl = New-Object -ComObject Excel.Application
      # create new PowerShell object
      $xlEnum = New-Object -TypeName PSObject
      # get all Excel exported types of type Enum
      $xl.GetType().Assembly.GetExportedTypes() | Where-Object {$_.IsEnum} | ForEach-Object {
          # create properties from enum values
          $enum = $_
          $enum.GetEnumNames() | ForEach-Object {
              $xlEnum | Add-Member -MemberType NoteProperty -Name $_ -Value $enum::($_)

      And this…
      Using enums in Powershell

    • #159411
      Senior Moderator
      Topics: 9
      Replies: 1263
      Points: 4,571
      Helping Hand
      Rank: Community Hero

      Better, if you could post the whole function/script you have to avoid guessing.

Viewing 3 reply threads
  • The topic ‘[enum]::Getnames($xlConstants) fails’ is closed to new replies.