Use Microsoft.Office.Interop.Word enum constants in VBScript

Given this code:

Set Word = CreateObject("Word.Application")
Set Edit = Word.Selection
Edit.style = "Heading 1"

I want to make this code language-independent, and there is an enumeration provided called WdBuiltinStyle:

However AFAIK VBScript has no enums, so this fails:

Set Word = CreateObject("Word.Application")
Set Edit = Word.Selection
Edit.style = WdBuiltinStyle.wdStyleHeading1

I can look up on the first link that the value of wdStyleHeading1 is -2, so this works:

Set Word = CreateObject("Word.Application")
Set Edit = Word.Selection
Edit.style = -2

So the question is:

  • Can I access the enum constants by name from VBScript?
  • Is hard-coding the numeric value safe? (Are they subject to change in other Office releases for example?)

Answers


If the enum value is documented you should be safe using it. Microsoft is famous for going to great lengths to maintain backwards compatibility.

VBScript doesn't support enum constants, but you can define the constants that you want to use in your script. This is a normal practice when using VBScript (check the first example on this page). i.e.

Const wdStyleHeading1 = -2
Const wdStyleNormal = -1

Set Word = CreateObject("Word.Application")
Set Edit = Word.Selection
Edit.Style = wdStyleHeading1

Need Your Help

How to find all the hard and symbolic links to a file/folder (Windows and UNIX)?

c++ winapi cross-platform symlink hardlink

How to find all the hard and symbolic links to a file/folder in Windows (using standard OS functions)?

How to Unit Test Methods with Sharepoint Objects as parameters with Microsoft Fakes

c# unit-testing sharepoint microsoft-fakes

Im trying to write a unit test for methods that take sp objects as parameters like SPWeb or SPEventReceiverCollection etc. with Microsoft Fakes but have no idea how to mock these.