Andrew
Andrew

Reputation: 11

Set style in Word using automation

I have the following VB script that is supposed to print: 123456789 in the following way (first 3 digits - normal style, next 3 - strong, last 3 - normal):

Set word = CreateObject("word.application")
word.visible = true

Set docs = word.documents
Set doc = docs.add()

Set sel = word.selection
Set oldStyle = sel.style
Set newStyle = doc.Styles("Strong")

sel.typeText("123")

sel.Style = newStyle
sel.typeText("456")

sel.style = oldStyle
sel.typeText("789")

However, the result is: first 3 digits normal and the rest, bold. What am I doing wrong?

Upvotes: 1

Views: 1435

Answers (1)

AutomatedChaos
AutomatedChaos

Reputation: 7500

At this part: Set oldStyle = sel.style you create a Reference (pointer) to the style. Later on, you change style to the new style. Referencing oldstyle will return the pointer, so also the new style.

Upvotes: 1

Related Questions