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:
Other People Are Reading
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.
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.
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:
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
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.)
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for