General discussion

Locked

Text colour using Word object in VB

By alburns23 ·
How can I change the colour of text I am writing to a Word document I have created in VB using the Word object. The code sample I am using is shown below:

Public Sub WriteText(ByVal SomeText As String)

Dim objRange As Word.Range

Set objRange = objDocument.Bookmarks.Item("\endofdoc").Range

objRange.Font.Name = "Arial"
objRange.Font.Bold = False
objRange.Font.ColorIndex = wdRed
'objRange.Font.Color = wdColorRed
objRange.ParagraphFormat.SpaceAfter = 1
objRange.InsertAfter SomeText
objRange.InsertParagraphAfter

Set objRange = Nothing

End Sub

When I run my program, the font type does change to Arial & the text is written in bold but the colour stays at the default of black. What am I doing wrong?

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Gast?n Nusimovich In reply to Text colour using Word ob ...

You should try the following code:

Dim objRange As Word.Range
Dim objDocument As Object

Set objDocument = Word.ActiveDocument

Set objRange = objDocument.Bookmarks.Item("\endofdoc").Range

objRange.Font.Name = "Arial"
objRange.Font.Bold = False


objRange.ParagraphFormat.SpaceAfter = 1
objRange.InsertAfter SomeText
objRange.InsertParagraphAfter
objRange.Select
Selection.Font.ColorIndex = wdRed

Set objRange = Nothing


Pay attention to the selection of the text first, and after that, then you apply the font color property to the selected text.

Good luck

Collapse -

by alburns23 In reply to

Poster rated this answer.

Collapse -

by Tank252ca In reply to Text colour using Word ob ...

Try setting your range, outputting the text and then applying the formatting. If I move the formatting commands in front of the InsertText method it does not work, but the below code produced red underlined text.

Place an OLE control on a form and in the click event enter:

myOleObject.CreateEmbed "", "Word.Document.8"
Set appWRD = myOleObject.object.Application
Set myRange = appWRD.ActiveDocument.GoTo(What:=wdGoToLine, Which:=wdGoToLast)
myRange.Select

myRange.InsertAfter "Some text" & vbCrLf
myRange.Underline = wdUnderlineSingle
myRange.Font.ColorIndex = wdRed

Set appWRD = Nothing


Now, the problem I am having is trying to apply two differen types of formatting on two different lines within a newly created documet. I want to underline the text inserted into the first line, start a new paragraph and turn the underline off.

pstrText = "Some text"
Set appWRD = oleAddendums.object.Application

' insert some underlined text
appWRD.ActiveDocument.Range(Start:=0, End:=0).InsertBefore Text:=pstrText & vbCrLf
appWRD.ActiveDocument.Range.Underline = wdUnderlineSingle
appWRD.ActiveDocument.Range.Collapse (wdCollapseEnd)

' add new paragraph (carriage return/line feed) to end of inserted text and turn off underline
appWRD.ActiveDocument.Range(Start:=Len(pstrText) + 1, End:=Len(pstrText) + 1).InsertAfter " " & vbCrLf
appWRD.ActiveDocument.Range.Select
appWRD.ActiveDocument.Range.Underline = wdUnderlineNone
' move cursor to end of document
Set myRange = appWRD.ActiveDocument.GoTo(What:=wdGoToLine, Which:=wdGoToLast)
myRange.Select
Set appWRD = Nothing

If I just insert the underlined text and move to the end of the document, underlining remains on for whatever other text is inserted via code or entered into Word by the user. Turning underline off in the second part of the code also removes the underline from the first line. I can't seem to separate the formatting for the two parts.

Collapse -

by alburns23 In reply to Text colour using Word ob ...

Yes, that's worked. Many thanks for your reply.

Collapse -

by alburns23 In reply to Text colour using Word ob ...

This question was closed by the author

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums