dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersManoj kumar sahu
Rakesh Chaubey
Ultimaterengan
Deepak Negi
Asheej T K
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » .NET Framework

Word Automation using VB.NET


Posted Date:     Category: .NET Framework    
Author: Member Level: Diamond    Points: 25


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
Word_Ref




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





Did you like this resource? Share it with your friends and show your love!


Responses to "Word Automation using VB.NET"
Guest Author: Gary Heath     25 Feb 2013
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 ?


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: C# Constructor - An Overview
    Previous Resource: Introduction to AJAX Controls
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Word automation  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.