How to Open a Word Document in VB Net

Written by warren rachele
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Open a Word Document in VB Net
(Dynamic Graphics Group/Dynamic Graphics Group/Getty Images)

The .NET library and its related libraries have made the integration of Microsoft Office tools and documents into your Visual Basic (VB) programs much easier than ever. The libraries that provide services to Word, for example, may link to your software so you can work with the word processor documents in their native format. You can broaden the scope of your software's capabilities without the need to re-create everything from scratch, which would defeat the purpose of the .NET libraries.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Click "File" on the menu bar, then click "New" and "Project ... " to start a new project. From the "New Project" dialogue box, click "Windows Form" and then "OK" to start.

  2. 2

    Add a Word object library "Project Reference" by right-clicking on the project's "Solution Explorer" panel and the clicking "Add a Reference ... ." In the "Add Reference" dialogue box, click the "COM" tab and scroll down to select the "Microsoft Word 14.0 Object Library." The version number displayed may differ, depending upon which version of Microsoft Office and its object library you have installed on your computer. Click "OK" to close the dialogue box.

  3. 3

    Right-click the form, then click "View Code" to select it from the menu, opening the code editor. Type the following two lines of text into the top of the code module, above the class declaration statement (for example, "Public Class Form 1"), to import the required name spaces into the project:

    Imports Microsoft.Office.Interop

    Imports Microsoft.Office.Core

  4. 4

    Click the "Form Designer" tab (for this example, the default will have the name "Form1.vb [Design]"). Double-click the form to insert a "Form1_Load" procedure and test the code. Create two Word objects to work with---an application and a document. The application object references the Word program, and the document references the file you are addressing. Type the following two lines of text in the procedure between the "Private Sub" and "End Sub" statements:

    Dim objWord As New Word.Application

    Dim objDocument As New Word.Document

  5. 5

    Type a code statement that opens the Word document (insert your own path and file name) directly after those added in Step 4:

    objDocument = objWord.Documents.Open("C:\myDoc.docx")

    (Replace "myDoc" with the name of the file.)

  6. 6

    Compile the application by pressing the "F7" key. Execute the program by pressing the "F5" key. This sample application comprises a form and a single button; click the button to open the document specified in the program's code.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.