Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bolding lines not working properly 1

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hello Everyone,
I am using Word Object Library 9.0. This code is executed from within VB6.
I have four lines of Word content. The following block of code is designed to "bold" the first line of the content while keeping the remaining three lines "non-bolded".
The problem is this. The first iteration yields the desired results i.e., the first line is bold and the remaining three normal. The Word doc is visible. The next thing I do is shutdown Word via the 'X' in the corner of the window, not saving the doc. I then re-execute the same code but this time ALL lines are bolded. I do set the Word objects to nothing between each iteration.
Please advise. Thx in advance.
Regards,
RPrinceton

Code snippet:
Set objRng = .ActiveDocument.Range(ActiveDocument.Sentences(1).Start, ActiveDocument.Sentences(1).End)
With objRng
.Bold = True
.Font.Name = "New Times Roman"
End With
Set objRng = .ActiveDocument.Range(ActiveDocument.Sentences(2).Start, ActiveDocument.Sentences(4).End)
With objRng
.Font.Name = "New Times Roman"
End With
 
If you refer to a document from outside word application, you need a dot (.) before each word object, in the case above before every ActiveDocument (unless it is a reference to an object defined in VB6.

combo
 
OR.....

create a Document object, and code reference to that.
Code:
Dim ThisDoc As Word.Document
' Set the Word application instance,
' say wrdApp

Set ThisDoc = wrdApp.Documents.Add  ' or whatever

Set objRng = ThisDoc.Range(ThisDoc.Sentences(1).Start, ThisDoc.Sentences(1).End)
This is combo's: "unless it is a reference to an object defined in VB6." So basically, it is better to use an fully qualified high-level object (the document). Otherwise, yes, all instructions must be fully qualified.

However, I am not sure this would totally fix your issue.
I have four lines of Word content. The following block of code is designed to "bold" the first line of the content while keeping the remaining three lines "non-bolded".
The problem is this. The first iteration yields the desired results i.e., the first line is bold and the remaining three normal. The Word doc is visible. The next thing I do is shutdown Word via the 'X' in the corner of the window, not saving the doc. I then re-execute the same code but this time ALL lines are bolded.
You do not show your code, but I suspect there is an issue with "lines".

Do you actually mean paragraphs? There is quite a difference.

This is a sentence. This is another sentence. This paragraph has THREE sentences.<p>
This is a different paragraph, with ONE sentence.<p>
This is another paragraph. It has TWO sentences.<p>

The <p> indicates a paragraph mark.

On the other hand
this is ONE paragraph but it has
FOUR “lines", but is still
just ONE paragraph, and ONE sentence.<p>


BTW: you do not have to use range...perhaps. You could change:
Code:
Set objRng = .ActiveDocument.Range(ActiveDocument.Sentences(1).Start, ActiveDocument.Sentences(1).End)
to:
Code:
Dim oSentence
Set oSentence = ActiveDocument.Sentences(1)
oSentence.Font.Bold = True
This would bold the first sentence in the document.

But again, it would bold the first sentence, not the first paragraph.

This is a sentence. This is another sentence. This paragraph has THREE sentences.<p>

would become:

This is a sentence. This is another sentence. This paragraph has THREE sentences.<p>

Gerry
 
Hello Fumei,
Thank you for the tips. I will certainly make use of this valuable information. The previous response posted by combo corrected the problem.
Thank you again.
Regards,
RPrinceton
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top