After you have instantiated an exposed object from a COM component's class in your application, you can programmatically manipulate it in the same way you would any other object in VB, such as a control or a form.
You must, of course, know how to use the object's methods and properties. If the component application has a class/type library, the Object Browser gives you a list and a brief description of each method or property and possibly more extended documentation if the component developer has provided a Help file along with the class library. You may also want to refer to any documentation on the component's classes as published by the component application's vendor.
The Excel component's various classes, for example, have methods and properties that enable you to manipulate an Excel application and every aspect of an Excel Spreadsheet or Chart. Assuming that objExcel is an instance of Excel.Application (see the previous three sections in this chapter), the following lines of code would terminate the running instance of Excel without prompting the user to save any changes:
Dim wb As Excel.Workbook
For Each wb In objExcel.Workbooks
'consider workbook as saved - don't_prompt wb.Saved = True Next wb
'End Excel application objExcel.Quit
The Excel component's Worksheet class contains a subclass known as the Range object. The Range object uses a string argument that defines the range of cells to be manipulated. Range, in turn, has a Value property that you can either read or write. The effect of setting or getting the Range object's Value property is to read or set the cell value specified in the argument to the Range object. The following line of code assumes that the current VB form contains a TextBox control named txtCellValue and that there's a Worksheet object variable named ws:
txtCellValue.Text = ws.Range("A1").Value
This line of code would assign the contents of cell A1 to the TextBox control.
The following line of code would reverse the process, setting the contents of cell A1 to be the same as the contents of the TextBox control:
ws.Range("A1").Value = txtCellValue.Text
Was this article helpful?