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 TheScripts.com 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!
'----COPY EVERYTHING BELOW THIS LINE INTO A NEW CODE MODULE
'Word Document Creation Class
'By: Chris Schimanski
' 4/20/2007
' ---------------------------
'Wrapper class to create new
'Word Documents easily.
'
'Usage:
'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
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()
Try
myDoc.SaveAs(myDocName)
Catch
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()
MyBase.Finalize()
End Sub
End Class