How to Display a Word Document in VB.NET

Written by warren rachele
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Display a Word Document in VB.NET
Display a Word document in VB.NET. (text and words image by Lario Tus from Fotolia.com)

A valuable feature that programmers may add to their applications is the ability to automate the access to files produced by another application. The ubiquitous presence of the Microsoft Word program makes its files a popular target for these automation features. The ability to interface directly with the object library of the Word software through Visual Basic makes it easy to display the documents in their native environment. Exploring the object library further demonstrates that features beyond simply displaying a document are within reach for your application.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Select "File">"New">"Project" from the menu. Click on "Other Languages">"Visual Basic" in the "Installed Templates" panel and select "Windows Form Application" as the project type. Click "OK" to start the new project.

  2. 2

    Click "Project">"Add Reference" on the menu to add an interface to the Microsoft Word library to your project. In the "Add Reference" dialogue, select the "COM" tab. Scroll down the list of COM interfaces until you find the "Microsoft Word XX Object Library" (where XX represents the version number) and select it. The version number will vary according to the version of Office that is installed on your computer. Click "OK" to close the dialogue and add the reference.

  3. 3

    Add the necessary namespaces to your program so that the Word function names are exposed. Click "View">"Code" or press "Ctrl-Alt-0" to display the code window. Type the following two lines as the first lines of code in the file:

    Imports Microsoft.Office.Core

    Imports Microsoft.Office.Interop

  4. 4

    Add the code statements to display a document using the Word application. Generally, this code is included in an event handler procedure so that the application can execute it at the user's request. As an example, place a button control on the form and double-click the control to add an event handler to the code. Add two variables for the application and document objects within the body of the procedure.

    Private Sub Button1_Click(. . .

    Dim objWord As New Word.Application
    
    Dim objDocument As New Word.Document
    

    End Sub

  5. 5

    Add a statement that calls the application to open the specified document. This code is also added to the button click event handler:

    Private Sub Button1_Click(. . .

    Dim objWordApp As New Word.Application
    
    Dim objDocument As New Word.Document
    
    
    
    objDocument = objWordApp.Documents.Open("C:0Data\MySample.docx")
    

    End Sub

  6. 6

    Compile the application by pressing "F7". Execute the program by pressing "F5". When the user clicks on the button, the Word program is started and the specified file is displayed.

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.