Question

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?)
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top