473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating a Word document using VB.NET

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
Apr 20 '07 #1
0 7721

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

Similar topics

6
by: Kerri McDonald | last post by:
We have an application where the user fills out many screens and when they are done, we are supposed to display the text they entered in a word or excel format. That is fairly easily accomplished...
7
by: Zeke | last post by:
I'm using the following code to create word document but the problem is if you go to task manager you'll see a WINWORD.EXE process is running but not the application, here is the code: ...
0
by: volume | last post by:
Hi all, In an effort to try to impress the boss, I would like to log accounting information to an Excel spreadsheet using C# and .NET. This would be a mockup only, not a real solution - yet! We...
4
by: Nikhil Patel | last post by:
Hi all, I need to generate a word document and save it on the server from an ASP.Net application. Basically I want to load a word template and insert some field values from a dataset and save...
3
by: m3ckon | last post by:
Hi there, I can succesfully create a word doc from my asp.net page, but I have 2 issues I need to resolve in order to use it in my app: 1) Creating a table: I seem unable to create a table,...
1
by: Novice | last post by:
Hi all, I'm using C# in my ASP.NET application and cannot seem to access the MS word libraries to create a word document for some reason. I have used both of these tutorials:...
0
by: goof1972 | last post by:
Hello All, I have a VB.NET web application (1.1 framework) that creates a new Word document through interop.word.dll. I have a Windows Server 2003 with Office XP Professional installed. I've...
2
by: tamaker | last post by:
I have a registration form where a user is able to, upon submission of the form, have their submission entered into a simple database... now Im looking to create a word document on the fly from...
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.