Translating OLE Syntax Examples

Description

The Alpha Anywhere application developer who wants to integrate his application with Microsoft Office will be able to find Visual Basic or Visual FoxPro code samples. These samples can be easily converted to Xbasic format.

The following code was validated with Alpha Anywhere and Above and Excel 2002 SP3.

 Creating a New Excel Spreadsheet

The difference between Xbasic and Visual FoxPro in this code sample is in the name of the function that creates the OLE object. This code opens Excel and returns a pointer to the new automation object.

  •  Alpha Anywhere Xbasic

    dim oExcel as P oExcel=o le.create("Excel.Application") ' Xbasic oExcel.visible=. T. oExcel.workbooks.add()
  • This code gets a pointer to an already open Excel application.

    oExcel=o le.GetObject("","Excel.Application") oExcel.visible=. T. oExcel.workbooks.add()
  •  Visual FoxPro

    dim oExcel as P oExcel=C reateObject("Excel.Application") oExcel.visible=. T. oExcel.workbooks.add()
  •  Visual Basic

    Set oExcel=N ew Excel.Application Set oWorkbook=E xcel.Workbooks.Add oWorkbook.Activate oExcel.Visible=T rue

 Opening an Excel Spreadsheet

In these scripts, the difference between Xbasic and Visual FoxPro is in the way array subscripts are referenced.

  •  Alpha Anywhere Xbasic

    dim oExcel as P dim oWorkbook as P dim fn as GetObject fn=" c:\spread\books.xls" oWorkbook=o le.GetObject(fn) oWorkbook.Application.visible=. T. oWorkbook.windows(1).activate()
  •  Visual FoxPro

    dim oExcel as P dim oWorkbook as P dim fn as GetObject fn=" c:\spread\books.xls" oWorkbook=G etObject(fn) oWorkbook.Application.visible=. T. oWorkbook.windows[1].activate()

 Referencing and Adding Worksheets

  •  Alpha Anywhere Xbasic

    dim oWorksheet1 as P dim oWorksheet2 as P ' Get a reference to the first worksheet oWorksheet1=o Workbook.Worksheets(1) ' Add a new sheet before sheet one oWorksheet2=o Workbook.Worksheets.Add(oWorksheet1)
  •  Visual FoxPro

    dim oWorksheet1 as P dim oWorksheet2 as P ' Get a reference to the first worksheet oWorksheet1=o Workbook.Worksheets[1] ' Add a new sheet before sheet one oWorksheet2=o Workbook.Worksheets.Add(oWorksheet1)
  •  Visual Basic

    dim oWorksheet as Worksheet Set oWorksheet=o Workbook.ActiveSheet

 Using Ranges to Read Cell Values

Both mechanisms for setting the value of the oRange object are valid. The oExcel.ActiveSheet.Range method works with the current worksheet, which is fine for single worksheet workbooks. The oWorksheet.Range approach is appropriate for workbooks with more than worksheet.

  •  Alpha Anywhere Xbasic

    dim oRange as P dim row as N dim col as N ' Two ways to read the value of cell C5 x=o Excel.ActiveSheet.Range("C5").value x=o Worksheet1.Range("C5").value ' Two ways to define a range oRange=o Excel.ActiveSheet.Range("A1:B2") oRange=o Worksheet1.Range("A1:B2")
    						for row=1 to oRange.Rows.Count for col=1 to oRange.Columns.Count x=o Range.cells(row,col).value next col next row
  •  Visual FoxPro

    dim oRange as P dim row as N dim col as N ' Two ways to read the value of cell C5 x=o Excel.ActiveSheet.Range("C5").value x=o Worksheet1.Range("C5").value ' Two ways to define a range oRange=o Excel.ActiveSheet.Range("A1:B2") oRange=o Worksheet1.Range("A1:B2")
    						for row=1 to oRange.Rows.Count for col=1 to oRange.Columns.Count x=o Range.cellsrow,col.value next col next row

 Writing Cell Values and Formulas

The following example sets the value of three cells, then sums them into a fourth cell.

  •  Alpha Anywhere Xbasic

    dim oRange as P oRange=o Worksheet1.Range("E1") oRange.value=1 00 oRange=o Worksheet1.Range("E2") oRange.value=1 00 oRange=o Worksheet1.Range("E3") oRange.value=1 00 oRange=o Worksheet1.Range("E4") oRange.formula="=SUM(E1:E3)"
  •  Visual FoxPro

    oExcel.ActiveSheet.Range("E1").value=1 00 oExcel.ActiveSheet.Range("E2").value=1 00 oExcel.ActiveSheet.Range("E3").value=1 00 oExcel.ActiveSheet.Range("E4").formula="=SUM(E1:E3)"
  •  Visual Basic

    ' two different ways to do the same thing oExcel.Range("E1").Select oExcel.ActiveCell.FormulaR1C1=1 00 oExcel.Range("E2").Select oExcel.ActiveCell.FormulaR1C1=1 00 oExcel.Range("E3").Select oExcel.ActiveCell.FormulaR1C1=1 00 oExcel.Range("E4").Select
    						oExcel.ActiveCell.FormulaR1C1="=SUM(E1:E3)" oWorksheet.Rows.Cells(1, 5)=1 00 oWorksheet.Rows.Cells(2, 5)=1 00 oWorksheet.Rows.Cells(3, 5)=1 00 oWorksheet.Rows.Cells(4, 5)="=SUM(E1:E3)"

 Setting Other Cell Properties

You can retrieve a list of object properties by opening or creating the spreadsheet in the Interactive window, then by evaluating the object. For example:

dim oExcel as P dim oWorkbook as P dim oWorksheet as P dim oRange as P dim fn as C fn=" c:\spread\books.xls" oWorkbook=o le.GetObject(fn) oWorkbook.Application.visible=. T. oWorkbook.windows(1).activate() oWorksheet=o Workbook.Worksheets(1) oRange=o
				Worksheet.Range("E1") ? oRange

Note that color values are defined in Blue, Green, Red sequence.

  •  Alpha Anywhere Xbasic

    oRange.font.name=" Verdana" oRange.font.size=2 4 oRange.font.bold=. T. oRange.font.color=h ex_to_dec("00FF00") ' BGR oRange.interior.color=h ex_to_dec("FFFF00") ' BGR

 Saving and Closing an Excel Spreadsheet

Except for the use of UI_GET_FILE(), the Xbasic and Visual FoxPro code is the same.

  •  Alpha Anywhere Xbasic

    fn=u i_get_file("Save the spreadsheet as ...", "(*.xls)", fn, "X") oWorkbook.SaveAs(fn) oWorkbook.Application.visible=. F. oWorkbook.close() delete oWorkbook delete oExcel
  •  Visual FoxPro

    oExcel.Quit() RELEASE oExcel
  •  Visual Basic

    Call oWorkbook.Close(False) oExcel.Quit Set oExcel=N othing

See Also