How to write a multi-formatted string to a Word doc?

G

Guest

Hi, I want to join some variables together into a string and write it to
word, and if say a variable is < 0 I want it to be red. How can I do this?

This is the code I am trying to use:

Set wPar = wDoc.Paragraphs.Add
strText = "This is the lead"
strText = strText & "This is the string I want to apply formatting to in the
middle of the sentence"
strText = strText & "This is the end of the sentence."
With wPar.Range
.Text = strText
.Font.Name = "Arial"
.Font.Size = 8
.Bold = False
End With
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Fred,

You can only apply the formatting once the string is in the Word document.
Use something like this:

Dim Range1 As Range
Dim Str1 As String, Str2 As String, Str3 As String
Dim lenrange1 As Long
Dim wpara As Paragraph
Set wpara = ActiveDocument.Paragraphs.Add
Str1 = "This is the beginning. "
lenStr1 = Len(Str1)
Str2 = "This is the middle. "
lenStr2 = Len(Str2)
Str3 = "This is the end"
wpara.Range.Text = Str1 & Str2 & Str3
Set Range1 = wpara.Range
Range1.Start = Range1.Start + lenStr1
Range1.End = Range1.Start + lenStr2
Range1.Font.Color = wdColorRed


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 

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