Word Automation using VB.NET
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
Index
Introduction
Working with Word interop
Start with code
Read content of Word file
Create a new word file and write in it
Print a document without opening it
Convert word document to HTML page
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"
here is snap
Start with code
At the top of page we need to import reference
Imports word = Microsoft.Office.Interop.Word
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
'Open new instance
dim objApp as New Word.Application 'create new object of word application
dim objDoc as New Word.Document 'create new object of word document
objDoc = objApp.Documents.Open(//word file path to open file)
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 and write in it
we can create a new word file using word automation and write text in it
'Open new instance
objApp = New Word.Application
objDoc = New Word.Document
objDoc = objApp.Documents.Add() 'add new word file to documents collection
objDoc.Activate() ' activate newly created file
objApp.Selection.TypeText("This the First text") 'insert string in word file
objDoc.SaveAs(//Path of file to save) 'save word file
'Dispose the word objects
objDoc.Close()
objApp.Quit()
objDoc = Nothing
objApp = Nothing
Print a document without opening it
Dim objApp As Word.Application
Dim objDoc As Word.Document
objApp = New Word.Application()
objDoc = objApp.Documents.Open("//Path of a file to Open")
objDoc.PrintOut( _
Background:=True, _
Append:=False, _
Range:=Word.WdPrintOutRange.wdPrintCurrentPage, _
Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
Copies:="1", _
Pages:="1", _
PageType:=Word.WdPrintOutPages.wdPrintAllPages, _
PrintToFile:=False, _
Collate:=True, _
ManualDuplexPrint:=False)
objDoc.Close();
objApp.Quit();
objDoc = Nothing
objApp = Nothing
Convert word document to HTML page
we can convert word file to web page using word automation
Dim objApp As Word.Application
Dim objDoc As Word.Document
objApp = New Word.Application()
objDoc = objApp.Documents.Open("//Path of a file to Open")
oDoc.SaveAs(FileName:=.FileName.ToString.Replace(".doc", ".htm"), FileFormat:=Word.WdSaveFormat.wdFormatHTML)
oDoc.Saved = True
oDoc.Close(SaveChanges:=False)
oDoc = Nothing
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.
Thanks
Suggestion are most welcome
Thanks
koolprasad2003
The document I have created is more than one page, how do I position it at the top when the Word document opens for editing ?