none
Method to List Word VBA Constants and Values RRS feed

  • Question

  • I am working on a Microsoft.Office.Interop.Word app and need certain Word VBA constants and values.  I was wondering if I could use VBA to obtain the list or is there a better way.
    Friday, February 3, 2012 6:01 PM

Answers

  • Use Reflection to enumerate them from VB.NET, C#, or PowerShell. Here's a PowerShell example:

    $wordAsm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word")
    [Type[]]$WordEnums = ($wordAsm.GetTypes() | where { $_.IsEnum })
    foreach($WordEnum in $WordEnums) {
        Write-Output "$($WordEnum.Name) {"
        foreach($ItemName in $WordEnum.GetEnumNames()) {
            Write-Output $("    {0,-40} = {1}" -f $ItemName, [int][Enum]::Parse($WordEnum, $ItemName))
        }
        Write-Output "}`r`n"
    }
    

    Or in VB.NET (console app):

    Imports System.Reflection
    Imports Word = Microsoft.Office.Interop.Word
    
    Module Module1
        Sub Main()
            Dim wordAsm As Assembly = GetType(Word.Document).Assembly
            Dim wordEnums = From T As Type In wordAsm.GetTypes() Where T.IsEnum Select T
    
            For Each T As Type In wordEnums
                Console.WriteLine(T.Name & "{")
                For Each ItemName As String In [Enum].GetNames(T)
                    Console.WriteLine("    {0,-40} = {1}", ItemName, [Enum].Parse(T, ItemName))
                Next
                Console.WriteLine("}" & vbCrLf)
            Next
        End Sub
    End Module
    

     


    jmh
    • Marked as answer by mogulman52 Monday, February 6, 2012 1:06 PM
    Friday, February 3, 2012 7:06 PM
  • I would open Word VBA and look in the Object Broweser. Alternatively I recommend always getting Add-in code working in VBA first, then copy and paste into VS and translate teh code. This provides teh constants you will need. This is obviously easier to do in VB. If you do use C# I strongly recommend C# 2010 as it finally allows optional and named parameters which make a big difference in readability of your code.

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Sunday, February 5, 2012 2:51 AM

All replies

  • Use Reflection to enumerate them from VB.NET, C#, or PowerShell. Here's a PowerShell example:

    $wordAsm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word")
    [Type[]]$WordEnums = ($wordAsm.GetTypes() | where { $_.IsEnum })
    foreach($WordEnum in $WordEnums) {
        Write-Output "$($WordEnum.Name) {"
        foreach($ItemName in $WordEnum.GetEnumNames()) {
            Write-Output $("    {0,-40} = {1}" -f $ItemName, [int][Enum]::Parse($WordEnum, $ItemName))
        }
        Write-Output "}`r`n"
    }
    

    Or in VB.NET (console app):

    Imports System.Reflection
    Imports Word = Microsoft.Office.Interop.Word
    
    Module Module1
        Sub Main()
            Dim wordAsm As Assembly = GetType(Word.Document).Assembly
            Dim wordEnums = From T As Type In wordAsm.GetTypes() Where T.IsEnum Select T
    
            For Each T As Type In wordEnums
                Console.WriteLine(T.Name & "{")
                For Each ItemName As String In [Enum].GetNames(T)
                    Console.WriteLine("    {0,-40} = {1}", ItemName, [Enum].Parse(T, ItemName))
                Next
                Console.WriteLine("}" & vbCrLf)
            Next
        End Sub
    End Module
    

     


    jmh
    • Marked as answer by mogulman52 Monday, February 6, 2012 1:06 PM
    Friday, February 3, 2012 7:06 PM
  • I would open Word VBA and look in the Object Broweser. Alternatively I recommend always getting Add-in code working in VBA first, then copy and paste into VS and translate teh code. This provides teh constants you will need. This is obviously easier to do in VB. If you do use C# I strongly recommend C# 2010 as it finally allows optional and named parameters which make a big difference in readability of your code.

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Sunday, February 5, 2012 2:51 AM
  • I want to thank both jhm and Rod.  I am doing what you say Rod in that I take VBA proven code and translate to C#.  It is fairly straight forward.  I did figure out the constants are in the Word interop but it takes a bit to find them. For example, rather than wdCaptionPositionBelow in VBA you need to use Word.WdCaptionPosition.wdCaptionPositionBelow.  Doing a search in the Visual Studio Object Browser finds them.  With Named and Optional parameters now in C#, C# and VB are very similar in complexity.  It should be a fun experiment to see how this works vs. VBA.
    Monday, February 6, 2012 1:06 PM