Home
  ASP
  VB
  PHP
  Contact Us

 


Partners
Visual Basic News & Information Source


       

How to use OLE automation?

Many beginners start developing programs in VB. They hear about OLE, Automation, ActiveX, COM, etc. but they are not aware of all these technologies. If you are a beginner and you want to know more about OLE, then you should read this article. It will help you get started writing code using OLE automation capabilities.

In this program, we'll use Word object. We'll use two objects, one of the Word Application class and one of the Word Document class. We'll open the Word Application and a document. User will be able to save the document. Follow the steps below to create the application:

Start a new EXE project. Select Microsoft Word 9.0 object library from the references dialog box. Remember, you should have Word instance on your system. Add a new module to the project. In the module's general declarations section, add the following code:

Public objapp as new word.application
Public objdoc as object

Open the project's form and add four buttons to the form.
Buttons are:

1.Launch
2.Save
3.Close
4.Exit

Now double click the "Launch" button and add the following code:

Private sub cmdLaunch_click()
If Not(objdoc is nothing) then 
msgbox "Application already running" 
exit sub
End if

objapp.visible=true

'Add a document to the application

Set objdoc:objapp.documents.add()

'Write some text in the document

objdoc.content = "This is my first OLE application"

End Sub

Let's discuss the code. We check if the application is already running, if it's not then we display the application. We add a new document to the open word application and add some text to the application.

Now, let's add code to the save button:

Private Sub cmdSave_click()

If objDoc is nothing then

msgbox "Application not running"
else
objdoc.Save
End if
End sub

We check whether the application is running or not. If it's running then we save the document otherwise we display a message telling the user that the application is not running.

Private Sub cmdClose-Click()
If objDoc is nothing then
Insbu"Application not running"
else
objApp.Quit
Set objDoc:Nothing
Set objApp:Nothing
End if
End Sub

We check if the application is running or not. If it's running we close the application using the "Quit" method of the "Application" class. Note it's important to remove the application from the memory. The following lines remove the objects from the memory:

Set objDoc=Nothing
Set objApp=Nothing

Private Sub cmdExit_Click()

If objdoc is nothing then
Unload me
end
else
msgbox "Please close application first"
End if
End Sub

Unload the open form and use the "End" command to destroy instance of the application. That's it!

Summary

This article shows you how to use OLE automation. The article shows how to create a new Word document from the VB environment. We have used two classes, Application and Document class, to accomplish the task of creating a new Word document.

 

 

    


 


 

Home | ASP | VB | PHP | Contact Us


© 2002 Wali Systems Inc.