By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,997 Members | 543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,997 IT Pros & Developers. It's quick & easy.

Creating a Word document using VB.NET

P: 1
I looked to find a canned solution to create a Word document in my application and just couldn't find anything that just gets to the point. I would think, and I may be making too strong of an assumption here, that anyone looking for this kind of solution would just want a simple example that they can work from and expand upon.

In my case, I just want to output a new Word document. I found that creating a simple wrapper class would make this process a "no-brainer".

Since I use quite often to find answers, I thought I would post one and maybe help out someone else for a change.

Thanks for the great site!


'Word Document Creation Class
'By: Chris Schimanski
' 4/20/2007
' ---------------------------
'Wrapper class to create new
'Word Documents easily.
'Sub TestWordDocClass()
' 'Declare your clsWordDoc object
' Dim MyDoc As New clsWordDoc
' 'Call the CreateDocument method with a path & name.
' MyDoc.CreateDocument("C:\Documents and Settings\christophers\Desktop\TestDoc" & Replace(Now.ToShortTimeString, ":", "") & ".doc")
' 'Add some formatted text.
' MyDoc.AppendParagraph("Paragraph 1:", False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendParagraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdFonts.Arial)
' 'Insert a page break.
' MyDoc.InsertPageBreak()
' 'Add some formatted text.
' MyDoc.AppendParagraph("Paragraph 2: , False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendParagraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdFonts.Comic)
' 'Save & close the document
' MyDoc.FinalizeWordDocument()
'End Sub
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
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()
MsgBox("The document could not be saved.", MsgBoxStyle.Critical, "Report Not Created")
End Try
myDoc.Close(False, objMissing, objMissing)
myApp.Quit(False, objMissing, objMissing)
End Sub

Protected Overrides Sub Finalize()
End Sub
End Class
Apr 20 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.