Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2010
Views 19,891
Downloads 746
Bookmarked 13 times

Word Automation using VB.NET - Part I

By koolprasad2003 | 1 Feb 2010
This article gives you a basic idea of how to automate a Word application using VB.NET
4 votes, 30.8%
1
1 vote, 7.7%
2
4 votes, 30.8%
3
1 vote, 7.7%
4
3 votes, 23.1%
5
2.70/5 - 13 votes
μ 2.81, σa 2.62 [?]

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
GeneralMy vote of 1 Pinmembernaeem_libra2:51 17 Apr '11  
GeneralAny Idea Word to PDF Pinmembereg_Anubhava20:18 16 Mar '10  
GeneralRe: Any Idea Word to PDF Pinmemberkoolprasad200320:38 17 Mar '10  
GeneralMy vote of 1 Pinmemberpadmanabhan N0:30 15 Mar '10  
GeneralPoor Article Pinmemberpadmanabhan N0:30 15 Mar '10  
GeneralMy vote of 1 PinmemberJecka4:08 10 Mar '10  
GeneralMy vote of 1 PinmemberNejimon CR0:10 8 Mar '10  
GeneralSome more PinmemberWekkel12:51 4 Feb '10  
GeneralRe: Some more Pinmemberkoolprasad200318:57 4 Feb '10  
GeneralMy vote of 2 PinmemberNagy Vilmos2: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
Web01 | 2.5.120210.1 | Last Updated 1 Feb 2010
Article Copyright 2010 by koolprasad2003
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid