Creating a Specially Formatted Word Document Through Visual Basic

E

Eager_Beever

I am a developer using Visual Basic 6. I have written a program where the
user enters some values to text boxes. Depending upon the values, a Word
document is created which displays the values entered along with descriptive
labels. None of the
fields/text boxes are compulsory and the output Word document contains only
those non-zero values. (That means I can not use predefined bookmarks to mark
locations. The resultant document will not contain a fixed no. of lines each
time). The user wants to apply bold formatting only to the values entered by
him and not to the descriptive labels. Both the labels and values are to be
printed on the same line (in plain and in bold format respectively). Also the
user wants that the values
should be printed in a separate font than the descriptive lables.

I have done everything else. I am able to create a Word document (.doc)
using Visual Basic with all the required contents. I am also able to apply
Bold and Underline formatting to an entire line. I am able to use different
fonts and font sizes to an entire line. But I am unable to apply bold
formatting only to a selected word from a line.

I need the document in the following format:

Today's Price : Rs. [125.00]
Available Qty : [10.250]

In the above output, the values enclosed within square braces should be in
Bold and in a different font, while everything else should be normal.

I will appreciate any type of help or suggestion on this. Thank You.

Eager_Beever


My code sample is as follows :

Dim objWord As Word.Application
Dim thisDoc As Word.Document
Dim thisRange As Word.Range

Set objWord = CreateObject("Word.Application")
Set thisDoc = objWord.Documents.Add


thisDoc.Range.InsertAfter "Today's Rates" & vbCrLf & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 2
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 11
thisRange.Font.Bold = True
thisRange.Font.Underline = True


thisDoc.Range.InsertAfter " Today's Price : " & txtPrice.Text & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 1
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 9

thisDoc.Range.InsertAfter " Available Qty : " & txtQty.Text & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 1
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 9
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .

thisDoc.SaveAs ("D:\Reports\Repo.doc")
objWord.Visible = True
 
C

Cindy M.

Hi Beaver,

It's one of those things that's simple - once you know how :)

You're already working with ranges, that's good. You just need to break things
down a bit, so that you can work with "smaller bits and pieces". There are many
ways to approach this, but generally, I tend to do it along these lines:

'Make sure the range is like a blinking cursor, no content
thisRange.Collapse wdCollapseEnd
thisRange.Text = " Today's Price : "
thisRange.Collapse wdCollapseEnd
thisRange.Text = txtPrice.Text
'Format the txtPrice.Text info bold - nothing else should be affected
thisRange.Font.Bold = true
'Format the entire paragraph
thisRange.Paragraphs(1).ParagraphFormat.Alignment = wdAlignParagraphLeft
'Create the new line, shouldn't use LF with Word
thisRange.InsertAfter vbCr
'Start in the new line
thisRange.Collapse wdCollapseEnd
'And so on...

If this is a fairly repetitive process, then I'd probably break it down into two
or three functions: one to insert the static text, one to insert and format the
bold text, one to format the paragraph and end it. That would make the code
shorter and easier to read.
I am a developer using Visual Basic 6. I have written a program where the
user enters some values to text boxes. Depending upon the values, a Word
document is created which displays the values entered along with descriptive
labels. None of the
fields/text boxes are compulsory and the output Word document contains only
those non-zero values. (That means I can not use predefined bookmarks to mark
locations. The resultant document will not contain a fixed no. of lines each
time). The user wants to apply bold formatting only to the values entered by
him and not to the descriptive labels. Both the labels and values are to be
printed on the same line (in plain and in bold format respectively). Also the
user wants that the values
should be printed in a separate font than the descriptive lables.

I have done everything else. I am able to create a Word document (.doc)
using Visual Basic with all the required contents. I am also able to apply
Bold and Underline formatting to an entire line. I am able to use different
fonts and font sizes to an entire line. But I am unable to apply bold
formatting only to a selected word from a line.

I need the document in the following format:

Today's Price : Rs. [125.00]
Available Qty : [10.250]

In the above output, the values enclosed within square braces should be in
Bold and in a different font, while everything else should be normal.

I will appreciate any type of help or suggestion on this. Thank You.

Eager_Beever


My code sample is as follows :

Dim objWord As Word.Application
Dim thisDoc As Word.Document
Dim thisRange As Word.Range

Set objWord = CreateObject("Word.Application")
Set thisDoc = objWord.Documents.Add


thisDoc.Range.InsertAfter "Today's Rates" & vbCrLf & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 2
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 11
thisRange.Font.Bold = True
thisRange.Font.Underline = True


thisDoc.Range.InsertAfter " Today's Price : " & txtPrice.Text & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 1
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 9

thisDoc.Range.InsertAfter " Available Qty : " & txtQty.Text & vbCrLf
varLCount = thisDoc.Paragraphs.Count - 1
Set thisRange = thisDoc.Paragraphs(varLCount).Range
thisRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisRange.Font.Name = "Verdana"
thisRange.Font.Size = 9
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .

thisDoc.SaveAs ("D:\Reports\Repo.doc")
objWord.Visible = True

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top