How to Modify a Word Document in VB.Net




(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)





How to Modify a Word Document in VB.Net
Log in to answer.
Copyright © dBuggr LLC - All Rights Reserved.
nishantbaxi 12:34 am on February 12, 2010
Step 1Open Microsoft Visual Basic .NET. Create a new Visual Basic project, or open one you’d like to add Word functionality to.
Step 2Right-click your project in Solution Explorer, choose “References,” and choose “Add Reference.” On the Add Reference screen, choose the “COM” tab, select “Microsoft Word 9.0 Object Library,” click “Select” and then “OK.”
Step 3Go to the place in your code that you’d like to modify the Word document. This will depend on what you’d like to do specifically, but one way to do this would be to open your main Form in the Designer, drag a new button onto the form, and double-click the button. This will create a stub subroutine that will be run whenever your button is clicked. At the top of this file, add the following line:
Imports Microsoft.Office.Interop
Step 4Add code to create a Word Application object. It should look like this:
Dim wordApplication As New Word.Application
Step 5Add code to open your Word document:
Dim doc As Word.Document
doc = wordApplication.Documents.Open(”C:\MyDocument.doc”)
Replace the path “C:\MyDocument.doc” with the path of your Word document. If you need to open a different Word document each time your program runs, you’ll need to create a String variable according to the specific needs of your program, and replace the entire path (including the quotation marks) with the name of the variable.
Step 6Use the functions of the _Document interface to modify your “doc” variable. For example, if you want to set the password “1234″ on the Word document, insert this code:
doc.Password = “1234″
See Msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document_members(office.11).aspx for a list of the many properties and functions of the _Document interface.