How can I format color and size of a paragraph in a word document using (VS2008Express)?

Posted on 2009-12-29
Last Modified: 2013-11-27
I currently have an application which takes information provided in textbox controls and when a user clicks a button the program creates a word document (.doc) and inputs the information as basic text. The wrapper class that I have attached is the code that I am using to input the text. How can I format the text using colors?

Could I somehow use something like:
    Public Enum cwdColors
        USE_DEFAULT = 0
        Navy = 1
        Orange = 2
        Black = 3
    End Enum
    Private sColors() As String = {"Auto", "Navy", "Orange", "Black"}

and then pass another optional parameter to the append paragraph to maybe select a color out of those options?

Thanks in advance.
'Word Document Creation Class

'By: Chris Schimanski

' 4/20/2007

' ---------------------------

'Wrapper class to create new

'Word Documents easily.


Imports System.Windows.Forms.Form

Imports Office = Microsoft.Office.Core

Imports Word = Microsoft.Office.Interop.Word

Public Class clsWordDoc

    'Enumerate some simple fonts for external declaration.

    Public Enum cwdFonts

        USE_DEFAULT = 0

        Arial = 1

        TNR = 2

        Courier = 3

        Comic = 4

    End Enum


    Private sFonts() As String = {"Times New Roman", "Arial", "Times New Roman", "Courier", "Comic Sans MS"}

    'Private variables for internal uses

    'objMissing...for settings that you don't need or want to mess with.

    Private objMissing As Object = System.Reflection.Missing.Value

    Private myApp As Word.Application

    Private myDoc As Word.Document

    Private myDocName As String

    Public Sub CreateDocument(ByVal sName As String)

        myDocName = sName

        myApp = New Word.Application

        myDoc = myApp.Documents.Add(objMissing, objMissing, objMissing, objMissing)


    End Sub

    Public Sub AppendParagraph(ByVal sText As String, _

    Optional ByVal bFinalizeParagraph As Boolean = False, _

    Optional ByVal bBold As Boolean = False, _

    Optional ByVal bUnderScore As Boolean = False, _

    Optional ByVal iFont As cwdFonts = cwdFonts.USE_DEFAULT)

        myApp.Selection.Font.Name = sFonts(iFont)

        If bBold Then : myApp.Selection.Font.Bold = 3

        Else : myApp.Selection.Font.Bold = 0

        End If

        If bUnderScore Then : myApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle

        Else : myApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone

        End If


        If bFinalizeParagraph Then myApp.Selection.TypeParagraph()

    End Sub

    Public Sub InsertPageBreak()


    End Sub

    Public Sub FinalizeWordDocument(ByVal filestring As String)




            MsgBox("The document could not be saved.", MsgBoxStyle.Critical, "Report Not Created")

        End Try


    End Sub

    Public Sub Closedocument()

        myDoc.Close(False, objMissing, objMissing)

        myApp.Quit(False, objMissing, objMissing)

    End Sub

    Protected Overrides Sub Finalize()


    End Sub


End Class

Open in new window

Question by:djdrew
    LVL 96

    Expert Comment

    by:Bob Learned
    I would say that you could use the same name for the enum as the color, and then just convert the enum value to a string, without having to have an array of color names:

        Public Enum cwdColors
        End Enum


    Accepted Solution

    I jumped the gun asking the question here. I figured out that you have to set the Microsoft.Office.Interop.Word.Application.Selection.Font.Color to a member of the word.wdcolor list.

    In the Code snippet below, I did a basic If statement to set the color using an optional parameter.
       Public Sub AppendParagraph(ByVal sText As String, _
        Optional ByVal bFinalizeParagraph As Boolean = False, _
        Optional ByVal bBold As Boolean = False, _
        Optional ByVal bUnderScore As Boolean = False, _
        Optional ByVal iFont As cwdFonts = cwdFonts.USE_DEFAULT, _
        Optional ByVal iColor As String = "Auto")
            myApp.Selection.Font.Name = sFonts(iFont)
            If iColor = "Orange" Then
                myApp.Selection.Font.Color = Word.WdColor.wdColorOrange
            ElseIf iColor = "Navy" Then
                myApp.Selection.Font.Color = Word.WdColor.wdColorBlue
            ElseIf iColor = "Black" Then
                myApp.Selection.Font.Color = Word.WdColor.wdColorBlack
                myApp.Selection.Font.Color = Word.WdColor.wdColorAutomatic
            End If

    Open in new window

    LVL 76

    Expert Comment

    For the size, there is a Font property which sets or returns the size in points.

    MyRange.Font.Size = 12

    Colour is done in a similar way. The numeric value is a Long integer. There is a set of Word constants,  including  wdColorGreen, wdColorDarkRed and wdColorAutomatic, that can be used.

    MyRange.Font.Color = edColorBlue

    LVL 76

    Expert Comment

    Sorry, didn't refresh the screen before posting the result of my research.
    LVL 96

    Expert Comment

    by:Bob Learned
    Another way is to create your own enums, borrowing from Word colors:

    Public Enum FontColor
       Auto = wdColorAutomatic
       Orange = wdColorOrange
       Blue = wdColorBlue
    End Enum

    That way, you can get a list of names, and fill drop-downs, lists, etc.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now