A very simple little LotusScript class which will open an existing spreadsheet and allow you to read and set individual cells, save and close the spreadsheet.
Class ExcelReport
Private xlApp As Variant
Private xlSheet1 As Variant
Private xlSheet2 As Variant
Private xlSheet3 As Variant
Private strFilePath As String
Sub new( xlFilename , isVisable )
Set xlApp = CreateObject(“Excel.application”)
xlApp.Workbooks.Open xlFilename
xlApp.Visible = isVisable
strFilePath = xlFilename
End Sub
Function saveFile
xlApp.ActiveWorkbook.SaveAs( strFilePath )
End Function
Function insertData( intSheet As Integer , row As Integer , column As Integer , value As String )
xlApp.Workbooks(1).Worksheets( intSheet ).Cells( row , column ).Value = value
End Function
Function getData( intSheet As Integer , row As Integer , column As Integer ) As String
On Error Goto err_hdl
getData = xlApp.Workbooks(1).Worksheets( intSheet ).Cells( row , column ).Value
Exit Function
err_hdl:
Print Error$ + “in cls: ExcelReport , method: getData , at line ” + Cstr( Erl )
getData = “”
Exit Function
End Function
Function doQuit
xlApp.Quit
Set xlApp = Nothing
End Function
Function makeVisable
xlApp.Visible = True
End Function
End Class
An example of how this can be used follows:
Sub Initialize
Dim report as ExcelReport
Dim iCol As Integer
Dim iRow as Integer
Dim strData as StringSet report = New ExcelReport(“C:\mysheet.xls” , True )
For iRow = 1 To 10
strData = report.getData(1, iRow , 1)
If strData = “” Then
report.insertData(1, iRow, 2, “Null”)
Else
report.insertData(1, iRow, 2, “Not Null”)
End If
Next iRow
report.saveFile
report.doQuit
End Sub
Update Fixed thanks to Matthias.
Matt,
Thanks for this helpfull little class.
Just 2 remarks:
a) In the example, you use “End For” for the end of the loop. As far as I know is this not supported in LotusScript, so just “Next” or “Next iRow” should be used there.
b) In the example, the report variable is not declared previously which will cause an error – at least if Option Declare is used.
Thanks for spotting the “deliberate” mistakes Matthias! I have corrected them now.
Thanks for the great ExcelClass! It is very simple to use. One thing I need is to be able to insert a new column at a certain location in the file (I’m starting with a “template”). Do you know the line or two of code that would accomplish this?