Saturday, March 6, 2010

VBScript - Formatting already written text in a word file

VBScript - Formatting already written text in a word file

'format already written text in a word file (divided in 3 parts as seen below)

formatting all content

Const Red = 255

Set word = CreateObject("Word.Application")
word.Visible = True

Set objectdocument = word.Documents.Open ("C:\test.docx")

word.selection.Wholestory

word.selection.Font.Name = "Times New Roman"
word.selection.Font.Size = 8
word.selection.Font.Bold = True
word.selection.Font.Color = Red

objectdocument.Save
objectdocument.Close
word.Quit


formatting specific paragraphs

Const Red = 255
Const Green = 155

Set word = CreateObject("Word.Application")
word.Visible = True

Set objectdocument = word.Documents.Open ("C:\test.docx")

count= objectdocument.paragraphs.count
msgbox count

Set objectrange = objectdocument.paragraphs(1).range

objectrange.Font.Name = "Times New Roman"
objectrange.Font.Size = 16
objectrange.Font.Bold = True
objectrange.Font.Color = Red

Set objectrange = objectdocument.paragraphs(2).range

objectrange.Font.Name = "Times New Roman"
objectrange.Font.Size = 16
objectrange.Font.Bold = True
objectrange.Font.Color = Green

objectdocument.Save
objectdocument.Close
word.Quit

You can use Range as a METHOD or as a PROPERTY see here.


finding a specific word in a specific paragraph and changing its formatting

Const Red = 255

strText = "This"

Set word = CreateObject("Word.Application")
word.Visible = True

Set objectdocument = word.Documents.Open ("C:\Documents and Settings\sachin\My Documents\abc.docx")

count= objectdocument.paragraphs.count
msgbox count

Set objectrange = objectdocument.paragraphs(2).range

With objectrange.Find

.Text = strText

Do
.Execute
If .Found Then
objectrange.Font.Name = "Times New Roman"
objectrange.Font.Size = 16
objectrange.Font.Bold = True
objectrange.Font.Color = Red
End If
Loop While .Found

End With

objectdocument.Save
objectdocument.Close
word.Quit

OR - Another Way

Const Red = 255
Const Green = 155
strText = "This"

Set word = CreateObject("Word.Application")
word.Visible = True

Set objectdocument = word.Documents.Open ("C:\test.docx")

count= objectdocument.paragraphs.count
msgbox count

Set objectrange = objectdocument.paragraphs(1).range

With objectrange.Find

.Text = strText

Do
.Execute
If .Found Then
objectrange.Font.Name = "Times New Roman"
objectrange.Font.Size = 16
objectrange.Font.Bold = True
objectrange.Font.Color = Red
End If
Loop While .Found

End With

Set objectrange = objectdocument.paragraphs(2).range

With objectrange.Find

.Text = strText

Do
.Execute
If .Found Then
objectrange.Font.Name = "Times New Roman"
objectrange.Font.Size = 16
objectrange.Font.Bold = True
objectrange.Font.Color = Green
End If
Loop While .Found

End With

objectdocument.Save
objectdocument.Close
word.Quit

Also See:

Opening a new word document
Opening an existing word document
Opening a new word document and writing text to it
Opening an existing word document and appending text to it at the end.
Opening an existing word document and appending text to it at the beginning.
Saving a new word file
Saving an existing word file
Writing formatted text to a word file
Inserting images/pictures to a word file
Creating table in a word document, adding text to cells, adding rows to table
Printing a word file