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 TestWordDocClas s()
' 'Declare your clsWordDoc object
' Dim MyDoc As New clsWordDoc
' 'Call the CreateDocument method with a path & name.
' MyDoc.CreateDoc ument("C:\Docum ents and Settings\christ ophers\Desktop\ TestDoc" & Replace(Now.ToS hortTimeString, ":", "") & ".doc")
' 'Add some formatted text.
' MyDoc.AppendPar agraph("Paragra ph 1:", False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendPar agraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdF onts.Arial)
' 'Insert a page break.
' MyDoc.InsertPag eBreak()
' 'Add some formatted text.
' MyDoc.AppendPar agraph("Paragra ph 2: , False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendPar agraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdF onts.Comic)
' 'Save & close the document
' MyDoc.FinalizeW ordDocument()
'End Sub
'
Imports System.Windows. Forms.Form
Imports Office = Microsoft.Offic e.Core
Imports Word = Microsoft.Offic e.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...f or settings that you don't need or want to mess with.
Private objMissing As Object = System.Reflecti on.Missing.Valu e
Private myApp As Word.Applicatio n
Private myDoc As Word.Document
Private myDocName As String
Public Sub CreateDocument( ByVal sName As String)
myDocName = sName
myApp = New Word.Applicatio n
myDoc = myApp.Documents .Add(objMissing , objMissing, objMissing, objMissing)
myDoc.Activate( )
End Sub
Public Sub AppendParagraph (ByVal sText As String, _
Optional ByVal bFinalizeParagr aph As Boolean = False, _
Optional ByVal bBold As Boolean = False, _
Optional ByVal bUnderScore As Boolean = False, _
Optional ByVal iFont As cwdFonts = cwdFonts.USE_DE FAULT)
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.WdUnderlin e.wdUnderlineSi ngle
Else : myApp.Selection .Font.Underline = Word.WdUnderlin e.wdUnderlineNo ne
End If
myApp.Selection .TypeText(sText )
If bFinalizeParagr aph Then myApp.Selection .TypeParagraph( )
End Sub
Public Sub InsertPageBreak ()
myApp.Selection .InsertBreak()
End Sub
Public Sub FinalizeWordDoc ument()
Try
myDoc.SaveAs(my DocName)
Catch
MsgBox("The document could not be saved.", MsgBoxStyle.Cri tical, "Report Not Created")
End Try
myDoc.Close(Fal se, objMissing, objMissing)
myApp.Quit(Fals e, objMissing, objMissing)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub
End Class
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 TestWordDocClas s()
' 'Declare your clsWordDoc object
' Dim MyDoc As New clsWordDoc
' 'Call the CreateDocument method with a path & name.
' MyDoc.CreateDoc ument("C:\Docum ents and Settings\christ ophers\Desktop\ TestDoc" & Replace(Now.ToS hortTimeString, ":", "") & ".doc")
' 'Add some formatted text.
' MyDoc.AppendPar agraph("Paragra ph 1:", False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendPar agraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdF onts.Arial)
' 'Insert a page break.
' MyDoc.InsertPag eBreak()
' 'Add some formatted text.
' MyDoc.AppendPar agraph("Paragra ph 2: , False, True, True)
' 'Append some text and end the paragraph.
' MyDoc.AppendPar agraph(" This is the normal text of the paragraph.", True, False, False, clsWordDoc.cwdF onts.Comic)
' 'Save & close the document
' MyDoc.FinalizeW ordDocument()
'End Sub
'
Imports System.Windows. Forms.Form
Imports Office = Microsoft.Offic e.Core
Imports Word = Microsoft.Offic e.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...f or settings that you don't need or want to mess with.
Private objMissing As Object = System.Reflecti on.Missing.Valu e
Private myApp As Word.Applicatio n
Private myDoc As Word.Document
Private myDocName As String
Public Sub CreateDocument( ByVal sName As String)
myDocName = sName
myApp = New Word.Applicatio n
myDoc = myApp.Documents .Add(objMissing , objMissing, objMissing, objMissing)
myDoc.Activate( )
End Sub
Public Sub AppendParagraph (ByVal sText As String, _
Optional ByVal bFinalizeParagr aph As Boolean = False, _
Optional ByVal bBold As Boolean = False, _
Optional ByVal bUnderScore As Boolean = False, _
Optional ByVal iFont As cwdFonts = cwdFonts.USE_DE FAULT)
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.WdUnderlin e.wdUnderlineSi ngle
Else : myApp.Selection .Font.Underline = Word.WdUnderlin e.wdUnderlineNo ne
End If
myApp.Selection .TypeText(sText )
If bFinalizeParagr aph Then myApp.Selection .TypeParagraph( )
End Sub
Public Sub InsertPageBreak ()
myApp.Selection .InsertBreak()
End Sub
Public Sub FinalizeWordDoc ument()
Try
myDoc.SaveAs(my DocName)
Catch
MsgBox("The document could not be saved.", MsgBoxStyle.Cri tical, "Report Not Created")
End Try
myDoc.Close(Fal se, objMissing, objMissing)
myApp.Quit(Fals e, objMissing, objMissing)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub
End Class