Getting the OLE Automation Object from Linked or Embedded Objects


Use the OLE control's Object property to get the OLE Automation object from a linked or embedded object on a form. Not all applications provide OLE Automation objects. If an object does not support OLE Automation, the Object property returns Nothing.

When working with OLE Automation objects, you should create an object variable to contain the OLE Automation object. For example, the following lines of code declare an object variable and establish a reference to an embedded worksheet when the form loads:

Listing 18.7. Use a module-level object variable to a contain reference to an OLE Automation object.


Option Explicit
Dim mobjExcelSheet
Private Sub Form_Load()
    ' Embed a worksheet in the OLE control named oleExcel.
    oleExcel.CreateEmbed "c:\excel\stock.xls"
    ' Establish a reference to the OLE Automation object for the
    ' embedded worksheet.
    Set mobjExcelSheet = oleExcel.Object
End Sub

In Listing 18.7, the variable mobjExcelSheet has module-level scope ; that is, other procedures in the module have access to the variable. For instance, the following Click event procedure uses the OLE Automation object mobjExcelSheet to print the embedded worksheet:


Private Sub cmdPrintSheet()
    mobjExcelSheet.PrintOut
End Sub

Unlike other applications that support OLE Automation, Microsoft Word requires the following special syntax to get its OLE Automation object:


Set objVar = olecontrol .Object.Application.WordBasic

You must use this special syntax because Word exposes only the WordBasic language for OLE Automation. When working with the Word OLE Automation object, remember that methods and properties apply to the current document, which might not be the one that the OLE control is currently displaying.

Listing 18.8 establish a reference to the WordBasic OLE Automation object.

Listing 18.8. Use the Object.Application.Word method to get the OLE Automation object from a Word OLE object.


Option Explicit
Dim mobjWordBasic
Private Sub Form_Load()
    ' Embed a Word document in the OLE control named oleWord.
    oleWord.CreateEmbed "c:\docs\products.doc"
    ' Establish a reference to the OLE Automation object for the
    ' embedded worksheet.
    Set mobjWordBasic = oleWord.Object.Application.Word
End Sub

Exmaple 18.7 demonstrates how the WordBasic methods apply to the current document. If cmdOpenNew runs before cmdPrintDocument, Word prints the newly opened document rather than the one that the OLE control is currently displaying.

Listing 18.9. Use WordBasic methods to control the Word OLE Automation object.


' Open a new file in Word (changes the current document).
Private Sub cmdOpenNew()
    mobjWordBasic.FileOpen
End Sub
' Print the current document in Word.
Private Sub cmdPrintDocument()
    mobjWordBasic.Print
End Sub

My OLE controls do not always update the display correctly.

If the control appears grayed after you edit an object, try closing the object's application. If that does not help, reload the object on the form. In extreme cases, you might have to capture the OLE object's image and display it within a picture box or image control. For information on how to do so, see the section "Capturing the Object's Picture," earlier in this chapter.

When I edit linked or embedded objects, my application crashes.

Be careful when opening linked or embedded objects for editing. Having more than one object open in the same application might cause that application to crash.

When using an OLE object's OLE Automation object, I frequently encounter Method or property does not exist errors.

Usually, this error indicates that you are using the wrong syntax. Check the application's documentation for the method. Often the problem involves the number of arguments or their data types.

My application doesn't recognize an object that it created.

Even if your application creates an object, it doesn't recognize that object after the SaveToFile method saves it. You can load such objects only by using the ReadFromFile method.