Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2010
Views 24,315
Downloads 1,140
Bookmarked 14 times

Word Automation using VB.NET - Part I

By | 1 Feb 2010 | Article
This article 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 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

Member

No one play your role BEST.... than U

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEasy Alternative to Automation PinmemberJohnny Glenn23:43 12 Mar '12  
GeneralMy vote of 1 Pinmembernaeem_libra1:51 17 Apr '11  
GeneralAny Idea Word to PDF Pinmembereg_Anubhava19:18 16 Mar '10  
GeneralRe: Any Idea Word to PDF Pinmemberkoolprasad200319:38 17 Mar '10  
GeneralMy vote of 1 Pinmemberpadmanabhan N23:30 14 Mar '10  
GeneralPoor Article Pinmemberpadmanabhan N23:30 14 Mar '10  
GeneralMy vote of 1 PinmemberJecka3:08 10 Mar '10  
GeneralMy vote of 1 PinmemberNejimon CR23:10 7 Mar '10  
GeneralSome more PinmemberWekkel11:51 4 Feb '10  
GeneralRe: Some more Pinmemberkoolprasad200317:57 4 Feb '10  
GeneralMy vote of 2 PinmemberNagy Vilmos1:11 2 Feb '10  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 1 Feb 2010
Article Copyright 2010 by koolprasad2003
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid