ExcelApplication

From Xojo Documentation
Jump to: navigation, search

Class (inherits from OLEObject)

Used to automate Microsoft Excel. Supported on the Windows platform only.


Notes

The language that you use to automate Microsoft Office applications is documented by Microsoft and numerous third-party books on Visual Basic for Applications (VBA). Microsoft Office applications provide online help for VBA. In Office 2007, click the Microsoft Office button and then click Options. Then select Popular and select the Show Developer Tab in the Ribbon checkbox.

You will then see a Visual Basic button in the Code group in the ribbon and menubar now includes a Help menu which leads to the VB online help.

To access the online help in Office 2003, choose Macros from the Tools Menu of your MS Office application, and then choose Visual Basic Editor from the Macros submenu. When the Visual Basic editor appears, choose Microsoft Visual Basic Help from the Help menu. The help is contextual in the sense that it provides information on automating the Office application from which you launched the Visual Basic editor.

If VBA Help does not appear, you will need to install the VBA help files. On Windows Office 2003, Office prompts you to install the VBA help files when you first request VBA help. You don't need the master CD.

Microsoft has additional information on VBA at http://msdn.microsoft.com/vbasic/ and have published their own language references on VBA. One of several third-party books on VBA is "VB & VBA in a Nutshell: The Language" by Paul Lomax (ISBN: 1-56592-358-8).

Examples

The following example transfers the information in a ListBox to Excel and tells Excel to compute and format a column total. The code is in a PushButton's Action event handler and it assumes that a two-column ListBox, ListBox1, is in the window. It contains the following data.

Item Price
Apples 1.77
Oranges 1.13
Bananas 0.40
Grapes 0.80



Dim excel As New ExcelApplication
Dim book As ExcelWorkbook
Dim sheet As ExcelWorksheet

excel.Visible = True
book = excel.Workbooks.Add
excel.ActiveSheet.Name = "Expenses Report"
For i As Integer = 0 To ProduceList.listcount - 1
excel.Range("A" + Str(i + 1), "A" + Str(i + 1)).Value = ProduceList.Cell(i, 0)
excel.Range("B" + Str(i + 1), "B" + Str(i + 1)).Value = ProduceList.Cell(i, 1)
Next
excel.Range("A"+Str(ProduceList.listcount+1), "A"+ _
Str(ProduceList.listcount+1)).Value = "Total"
excel.Range("B1", "B"+Str(ProduceList.listcount)).Style = "Currency"
excel.Range("B"+Str(ProduceList.listcount+1), "B"+ _
Str(ProduceList.listcount+1)).Value = "=SUM(B1:B" +_
Str(ProduceList.listcount) + ")"

Exception err as OLEException
MsgBox err.message


See Also

Office Automation, Office, OLEException, OLEObject, PowerPointApplication, WordApplication classes.


Personal tools
Namespaces

Variants
Actions
Main
Documentation
Community
Toolbox
Help