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

Word Automation using VB.NET - Part I

, 11 Sep 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip gives you a basic idea of how to automate a Word application using VB.NET.

Introduction

Many developers do not know about Word automation and its advantages. So, here is a simple demo program which 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 could 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 Microsoft 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 its 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 disposed by using the following methods. GC will collect those objects until we make them NULL.

objDoc.Close()
objApp.Quit()
objDoc = Nothing
objApp = 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 = 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 = 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 = 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.

More Articles on Word Automation

You may find more articles on Word automation at the following link:

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)

Share

About the Author

koolprasad2003
Software Developer (Senior)
India India
MCP,MCTS.
Microsoft MVP 2014 [ASP.NET/IIS]
Click here for more .NET Tips

Comments and Discussions

 
GeneralMy vote of 1 PinmemberCindy Meister12-Sep-14 7:44 
GeneralRe: My vote of 1 Pinmemberkoolprasad200312-Sep-14 18:49 
QuestionMethod to properly release COM objects PinmemberMike Meinz11-Sep-14 13:34 
QuestionHave you consider... PinmemberNelek10-Sep-14 22:38 
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
Web04 | 2.8.141015.1 | Last Updated 11 Sep 2014
Article Copyright 2010 by koolprasad2003
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid