Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 10.1 Objects and properties in OLE_DEMO.FRM.

Begin VB.Form frmOleDemo
   Caption      =   “OLE Demonstration ”
   Begin VB.OLE OLE1
   End
   Begin VB.Menu mnuFile
      Caption         =   “&File”
      Begin VB.Menu mnuFileSave
         Caption         =   “&Save”
      End
      Begin VB.Menu mnuFileSep
         Caption         =   “-”
      End
      Begin VB.Menu mnuFileExit
         Caption         =   “&Exit”
      End
   End
   Begin VB.Menu mnuObject
      Caption         =   “&Object”
      Begin VB.Menu mnuInsert
         Caption         =   “&Insert”
      End
      Begin VB.Menu mnuObjectDelete
         Caption         =   “&Delete”
      End
   End
End

The program code is shown in Listing 10.2. It starts by declaring a few constants and global variables in the form’s General Declarations section. For simplicity’s sake, I have defined a constant for the name of the OLE data file, OLE_DEMO.DAT. Here’s how the program works. When it’s running, the user can select the Insert Object command to display the Insert Object dialog box, and then use this dialog box (as described earlier in the chapter) to insert either a new object or one based on an existing file. The inserted object can be activated and edited using the server application; pressing Esc ends in-place activation and returns to the program’s own menu. For example, Figure 10.5 shows an inserted Excel spreadsheet activated for editing. Note how the Visual Basic program displays Excel’s menu while the object is activated. After editing the object, press Esc to “deactivate” the object and redisplay the Visual Basic program’s menus.

Once an object has been inserted, you can use the File Save command to save it to disk under the predefined file name. The Object Delete command deletes the object from the OLE control. The program keeps track of whether the inserted object has been saved to disk. Use Object Insert to insert a different object, if desired.

When the program begins, it checks to see if the data file exists on disk. If it does, you are offered the option of loading it into the OLE control. I assume that the file name OLE_DEMO.DAT will not conflict with an existing file on your disk; if it does, change the constant definition in the program to use another name.

This program is a good demonstration of how the capabilities of the OLE control make it easy to program OLE support into our Visual Basic programs. OLE opens a whole new world of possibilities. The power of our programs is no longer limited to the code we have the time and skill to write, or to the custom controls that you can afford to buy.


Figure 10.5  The OLE_DEMO program with an embedded Excel worksheet activated for in-place editing.

Listing 10.2 Code in OLE_DEMO.FRM.

Option Explicit
Option Explicit
‘ Constant for OLE object type.
Const OLE_EMBEDDED = 1
‘ Constant for the OLE data file name.
Const FILE_NAME = “OLE_DEMO.DAT”
‘ Global variables and flags.
Dim DataSaved As Boolean
Dim ObjectPresent As Boolean
Private Sub Form_Load()
Dim FileNum As Long, Reply As Integer
ObjectPresent = False
DataSaved = True
‘ See if the OLE data file exists. If it does, offer
‘ the option of loading it.
If Dir$(FILE_NAME) <> “” Then
    Reply = MsgBox(“Load OLE data from disk?”, vbYesNo + vbQuestion, _        “Load Object”)
    If Reply = vbYes Then
        Screen.MousePointer = 11
        FileNum = FreeFile
        Open FILE_NAME For Binary As #FileNum
        OLE1.ReadFromFile FileNum
        ObjectPresent = True
        Close #FileNum
        Screen.MousePointer = 0
    End If
End If

End Sub

Private Sub Form_Resize()
‘ Size the OLE control to fill the form.
OLE1.Move 0, 0, frmOleDemo.ScaleWidth, frmOleDemo.ScaleHeight

End Sub

Private Sub mnuFile_Click()

‘ Enable Save menu command only if an object exists.

If ObjectPresent Then
    mnuFileSave.Enabled = True
Else
    mnuFileSave.Enabled = False
End If

End Sub

Private Sub mnuFileExit_Click()

Dim Reply As Integer

‘ If the object has not been saved, offer the option.

If Not DataSaved Then
    Reply = MsgBox(“Save OLE object before quitting?”, _
        vbYesNoCancel + vbQuestion, “Delete Object”)
    If Reply = vbYes Then
        Call SaveObject
    ElseIf Reply = vbCancel Then
        Exit Sub
    End If

End If

End

End Sub


Private Sub mnuFileSave_Click()

Call SaveObject

End Sub


Private Sub mnuInsert_Click()

Dim Reply As Integer

‘ If an object is already present in the OLE control, ask the user if it

‘ should be deleted. If the reply is no exit sub.
If ObjectPresent Then
    Reply = MsgBox(“Delete current object?”, vbYesNo + vbQuestion, _
        “Insert Object”)
    If Reply = vbYes Then
        Call mnuObjectDelete_Click
    Else
        Exit Sub
    End If
End If
‘ Permit only embedded objects.
    OLE1.OLETypeAllowed = OLE_EMBEDDED
‘ Display the OLE Insert Object Dialog.
    frmOleDemo.OLE1.InsertObjDlg
    ObjectPresent = True
    DataSaved = False
    Screen.MousePointer = 0
End Sub
Private Sub mnuObject_Click()
‘ Enable the Delete menu command
‘ only if an object is present.
If ObjectPresent Then
    mnuObjectDelete.Enabled = True
Else
    mnuObjectDelete.Enabled = False
End If
End Sub
Private Sub mnuObjectDelete_Click()
Dim Reply As Integer
‘ If the object has not been saved, offer the
‘ user the option of saving it.
If Not DataSaved Then
    Reply = MsgBox(“Save the object before deleting it?”, vbYesNoCancel _
        + vbQuestion, “Delete Object”)
    If Reply = vbYes Then
        Call SaveObject
    ElseIf Reply = vbCancel Then
        Exit Sub
    End If
End If
‘ Delete the object.
OLE1.DELETE
ObjectPresent = False
DataSaved = True
End Sub
Private Sub OLE1_DblClick()
‘ The OLE object is automatically activated for editing because its
‘ AutoActivate property has been left at the default value of 2. Otherwise
‘ we would have to execute the DoVerb method to activate it.
DataSaved = False
End Sub
Private Sub OLE1_Updated(Code As Integer)
‘ If the data is changed by the server, clear the Saved flag.
DataSaved = False
End Sub
Private Sub SaveObject()
Dim FileNum As Integer
‘ Save the OLE object.
FileNum = FreeFile
Open FILE_NAME For Binary As #FileNum
OLE1.FileNumber = FileNum
OLE1.SaveToFile FileNum
DataSaved = True
Close #FileNum
End Sub

OLE Automation

The first part of this chapter presented two sides of the OLE triangle: linking data objects and embedding data objects. These are powerful tools—but triangles have three sides, so something must be missing. There is, indeed: OLE automation —the newest component of OLE, and in some ways, the most powerful. In the remainder of this chapter, we’ll take a look at the basics of OLE automation in Visual Basic.

How Does It Work?

At the heart of OLE automation is a special kind of data object called a programmable data object. Like nonprogrammable objects, programmable data objects can be embedded or linked in a container application. What makes them special is the ability to accept messages from other applications. These messages can contain commands instructing the data object to perform actions on its data. The messages can also pass data to and from the programmable object.

Here’s a simple example. Suppose you need to add up a column of numbers in your Visual Basic program. Rather that writing the code yourself, you could use OLE automation to send the numbers to an Excel spreadsheet object, instruct it to add them up, then return the result to the program. You wouldn’t use OLE automation for such a simple task, of course, but it gives you an idea of what OLE automation is all about. Server applications that expose programmable objects can be considered custom controls whose capabilities are available to other applications.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.