Getting the OLE Automation Object from Linked or Embedded ObjectsUse 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
|
|
![]() |
![]() |
|||