Run Other Applications

Being able to start one application from another is one of the significant advantages of Windows. Within Visual Basic you may want to start another application to load data from Excel, to display a web page, to edit text files, or to perform some other task not easily done in Excel itself. Visual Basic provides the functions shown in Table 3-19 to run other applications.

Table 3-19. Visual Basic functions for running other applications

Function

Use to

AppActivate

Switch focus to a running application

CreateObject

Start an ActiveX application and get an object reference to that application

GetObject

Get a running ActiveX application and get an object reference to that application

SendKeys

Send keystrokes to a running Windows application

Shell

Start an application using its file (.exe) name

CreateObject and GetObject work only with Windows applications that have support for ActiveX automation built in to them. Most Microsoft products and many other Windows products support that type of automation, which is sometimes also called OLE automation .

ActiveX or OLE automation allows you to use the internal objects, properties, and methods of the application in the same way that you control Excel from Visual Basic. For example, the following code starts Microsoft Word from Excel, creates a new document, inserts some text, and saves the file:

 Sub UseWord( ) Dim word As Object, doc As Object Set word = CreateObject("Word.Application") ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.