Skip Navigation LinksHome > Categories > Code from a Category

Word Automation using VB.NET



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1198
Add Date: 10/12/2012
In this article i have explained how to automate word applications with the help of VB.NET.
Many peoples are not aware of COM interop applications, this is a basic article that gives idea about the word automations.

Word Automation using VB.NET

Introduction
Recently i have worked with Interop objects, may be you have heard about this word. Interop are unmanaged object. basically COM Interop is a technique to included COM objects in .NET to interact objects. Component Object Model (COM) interop expose object and it's functionality to other components and use them to host various applications

Working with Word interop
In this article we are going to deal with Word interop objects. we can automate word files using this interoperability to automate word programmatically we need to add it's word interop assemblies.

we need Office 2003 or 2007 and VB.NET (here i used office 2003)

now open Visual studio and create new project in VB.NET. After that go to solution explorer Rightclick and add reference
new window will open go to COM tab and select "Microsoft Word 11.0 Object Library" if you have 2007 then you should select "Microsoft Word 12.0 Object Library"



Start with code
At the top of page we need to import reference

yes, we import namespace now start with the actual operation.

Read content of Word file
Here is our first task to read content of word file.
we need to create a object of Document and application. here is code



Create a new word file and write in it
we can create a new word file using word automation and write text in it


Print a document without opening it


Convert word document to HTML page
we can convert word file to web page using word automation


Real world fact
Word is heavy interop object and it is unmanaged resource so GC unable to understand and collect it after use, we have to make force code to collect the object from meory.

for this perticular code i have used Close() and Quit() methods to dispose word objects. Word automation may slow down the system processes.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.