I used to work with VB6 and I referenced MSExcel 11.0 Library Object (MSoffice 2003). Now with MSOffice 2007, I am only given one choice of referenced MSExcel 12.0 Library Object
here is the code:
Code:
' in form
Option Explicit
Public xlApp As Excel.Application
Public xlWB As Excel.Workbook
Public xlWS As Excel.Worksheet
' in routine where I want to write to MS Excel 2007
Private Sub StepCurrentCmd_Click()
Dim DisplayStr As String
Dim row As Integer
Dim col As Integer
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Add
Set xlWS = xlWB.Worksheets("sheet1")
row = 2
col = 1
xlWS.Cells(row, col).value = DisplayStr ' DisplayStr is an updated string
col = col + 1
End Sub
The error is "Application-defined or Object-defined error"
Essentially the error is at the line:
xlWS.Cells(row, col).value = DisplayStr
I noticed that the letter v in value (above) refuses to be capitalized. In the older application where I was using 11.0 library, the v in value automatically became capitalized. Is this the issue?
this should work. but there have been many posts about it not working in 2007, however i would expect a runtme error on that line if it is not working correctly
test if xlWS is a valid worksheet object
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
xlWS.Cells(row, col).value = DisplayStr
col = col + 1
If (col = 15) Then
col = 1 REM This line used to be col = 0. Excel did not like this.
row = row + 1
End If