Click here to Skip to main content
Click here to Skip to main content

Word Automation using VB.NET - Part I

By , 1 Feb 2010
Rate this:
Please Sign up or sign in to vote.

Introduction

Many developers do not know about Word automation and its advantages. So here is a simple demo program that gives you a brief idea of Word automation using VB.NET. and Word 2003.

Background

Actually my team leader encouraged me to write something on Word automation that would be useful to my juniors. So I searched on MSDN and got some information to automate Word using VB.NET.

Getting Started

To automate Word, you need Word to be installed on your machine. You can use Word 2007/2003. Here we have used Word 2003. Create a new window based application and add a reference to "Microsoft Word 11.0 Object Library". See the following screenshot to add a reference from the COM tab:

After successfully referencing the assembly, start the code.

Import the Namespace

Imports Microsoft.Office.Interop.Word

This will helps us to use its predefined methods and properties.

Create the Objects

Now create Application and Document objects with it's new instance.

Dim objApp As Word.Application
Dim objDoc As Word.Document
objApp = New Word.Application()
objDoc = objApp.Documents.Open("//Path of a file to Open")

Dispose the Objects

Once Word objects are created, after Word has finished, they should be disposes by using the following methods. GC will collect those objects until we make them NULL.

objDoc.Close()
objApp.Quit()
objDoc = Nothing
objDoc = Nothing

Read the Contents of Already Saved Word File

The following code will read the contents of the already saved Word file:

'Open new instance
objApp = New Word.Application
objDoc = New Word.Document

objDoc = objApp.Documents.Open(//File Path)
objDoc.Activate()
MessageBox.Show(objDoc.Content.Text) //Shows the content from word file

'Dispose the word objects
objDoc.Close()
objApp.Quit()
objDoc = Nothing
objApp = Nothing

Create a New Word File

The following code will create a new Word file and save it:

'Open new instance
objApp = New Word.Application
objDoc = New Word.Document

objDoc = objApp.Documents.Add()
objDoc.Activate()
objDoc.SaveAs(//Path of file to save)

'Dispose the word objects
objDoc.Close()
objApp.Quit()
objDoc = Nothing
objApp = Nothing

Create a New Word File and Write into it

The following code will create a new Word file and write some text in it and save it:

'Open new instance
objApp = New Word.Application
objDoc = New Word.Document

objDoc = objApp.Documents.Add()
objDoc.Activate()
objApp.Selection.TypeText("This the First text")
objDoc.SaveAs(//Path of file to save)

'Dispose the Word objects
objDoc.Close()
objApp.Quit()
objDoc = Nothing
objApp = Nothing

Points of Interest

As WINWORD is the heavy object, we have to use the Close() and Quit() methods to kill WINWORD instance from task manager. Otherwise it will be pending in a taskmanager. The number of WINWORD instances will go on increasing if we create a NEW object of Word.Application.

Summary

The attached code will give you a brief idea of Word automation techniques. Using the above methods, one can easily automate Word.

History

  • 1st February, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

koolprasad2003
Software Developer
India India
MCP,MCTS.
Microsoft MVP 2014 [ASP.NET/IIS]
 
No one play your role BEST.... than U

Comments and Discussions

 
Questionquestion about add text Pinmemberseryouf8-Apr-13 13:23 
AnswerRe: question about add text Pinmemberkoolprasad200324-Jun-13 4:39 
GeneralMy vote of 1 PinmemberBiel Simon21-Jun-12 0:11 
QuestionEasy Alternative to Automation PinmemberJohnny Glenn12-Mar-12 23:43 
GeneralMy vote of 1 Pinmembernaeem_libra17-Apr-11 1:51 
GeneralAny Idea Word to PDF Pinmembereg_Anubhava16-Mar-10 19:18 
GeneralRe: Any Idea Word to PDF Pinmemberkoolprasad200317-Mar-10 19:38 
GeneralRe: Any Idea Word to PDF Pinmemberkoolprasad200325-Feb-14 23:46 
GeneralMy vote of 1 Pinmemberpadmanabhan N14-Mar-10 23:30 
GeneralPoor Article Pinmemberpadmanabhan N14-Mar-10 23:30 
GeneralMy vote of 1 PinmemberJecka10-Mar-10 3:08 
GeneralMy vote of 1 PinmemberNejimon CR7-Mar-10 23:10 
GeneralSome more PinmemberWekkel4-Feb-10 11:51 
GeneralRe: Some more Pinmemberkoolprasad20034-Feb-10 17:57 
GeneralMy vote of 2 PinmemberNagy Vilmos2-Feb-10 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 1 Feb 2010
Article Copyright 2010 by koolprasad2003
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid