?
Solved

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

Posted on 2009-12-29
5
Medium Priority
?
683 Views
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)
        myDoc.Activate()
    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

        myApp.Selection.TypeText(sText)



        If bFinalizeParagraph Then myApp.Selection.TypeParagraph()
    End Sub
    Public Sub InsertPageBreak()
        myApp.Selection.InsertBreak()
    End Sub
    Public Sub FinalizeWordDocument(ByVal filestring As String)
        Try

            myDoc.SaveAs(myDocName)
        Catch
            MsgBox("The document could not be saved.", MsgBoxStyle.Critical, "Report Not Created")
        End Try

        myApp.Documents.Open(filestring)

    End Sub
    Public Sub Closedocument()
        myDoc.Close(False, objMissing, objMissing)
        myApp.Quit(False, objMissing, objMissing)
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub


   

End Class

Open in new window

0
Comment
Question by:djdrew
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26140397
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
        Auto
        Navy
        Orange
        Black
    End Enum

0
 

Accepted Solution

by:
djdrew earned 0 total points
ID: 26140484
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
        Else
            myApp.Selection.Font.Color = Word.WdColor.wdColorAutomatic

        End If

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26140555
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

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26140567
Sorry, didn't refresh the screen before posting the result of my research.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26140729
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.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 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