Topic A1: Controlling Another Application from VB

advertisement
Controlling Another Application from VB
You can quickly turn simple VB programs into powerful applications by linking them to
programs on your computer that support COM automation. These programs include all of
the Microsoft Office applications, Microsoft Mappoint, AutoCAD, Corel Draw, and
many others.
To get started controlling one of these programs, you need to add a Reference to it in
your VB project. From the Project menu, select the Properties item (at the bottom of the
list, with the name of your project followed by “Properties…”). Click the “References”
tab on the left, and then the “Add…” button near the middle of the window that appears.
In the “Add Reference” dialog box, click the COM tab. Scroll down to find the
application’s object library. Here are a few of the object libraries:




Word 2003: Microsoft Word 10.0 Object Library
Word 2007: Microsoft Word 12.0 Object Library
MapPoint: Microsoft MapPoint 13.0 Object Library
Etc.
Select the object library you want, and click the OK button to add the reference.
Now, when you declare a variable, you will find even more choices after you type “As”.
For example, with Word 2003, when you type “As” you will be able to select “Word”,
and after you type a period you will see many choices. In most cases, you will want to
select Word.Document. This is a class, just like the classes you’ve been using in your
programs. Declaring a variable of type Word.Document isn’t enough. You will have to
instantiate it using “New.” Here’s how it’s done in Word 2003:
Public Class Form1
Dim worddoc As Word.Document
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
worddoc = New Word.Document
worddoc.Application.Visible = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
worddoc.Content.Text &= "Hello! "
End Sub
End Class
Note that I declare the worddoc variable at form level, so I’ll be able to write to or
otherwise manipulate the Word document from any procedure within the form. The line
worddoc = New Word.Document
instantiates the document, making it a real object, while the next line makes it visible.
You can create word documents in the background and save them without ever seeing
them, but for now you’ll want to see your code work, so make the application visible.
The Office 2007 programs work a little differently. The references are in
Microsoft.Office.Interop—for example, Microsoft.Office.Interop.Word. Therefore, your
code looks like this:
Dim worddoc As Microsoft.Office.Interop.Word.Document
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
worddoc = New Microsoft.Office.Interop.Word.Document
worddoc.Application.Visible = True
End Sub
After this, the worddoc variable should work pretty much the same with either 2003 or
2007. Explore the many options available when you type “worddoc” followed by a
period. Almost anything you can do in Word with keyboard and mouse you can do
automatically in your VB program.
Other programs are accessed in similar fashion, although you will almost certainly need
to use online help and experiment a bit to get them to do what you want.
Download