Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB 6 Creating an MS Excel File - Error 1

Status
Not open for further replies.

campbere

Technical User
Oct 10, 2000
146
0
0
US
I am having problems creating a MS Excel file from my Visual Basic Application. I keep getting an error when I try to assign a name to a worksheet. The error says ojbect doesn't support this propert or mehtodIM006.

Here is the snippett of code I have to this point:

Dim objXL As Excel.Application

Dim strname As String

Set objXL = CreateObject("Excel.Application")

strname = "Rick"

objXL.Workbooks.Add

With objXL.ActiveWorkbook
.Worksheets.Add
.Worksheets.Name (strname)
End With

objXL.Visible = True

Can someone help? Also if you know of a place with documentation and examples I would appreciate it. I have been searching on MSDN but it doesn't appear to have much information.

Thank you.
 
objXL is your application object. I think that what you want to see is the WorkSheet Object. You might try something like the following:

Dim WrkBook As Excel.Workbook
Dim WrkSheet As Excel.Worksheet

Set WrkBook = objXL.Workbooks.Add
If Not (WrkBook Is Nothing) Then
Set WrkSheet = WrkBook.Worksheets(1)
If Not (WrkSheet Is Nothing) Then
WrkSheet.Activate
WrkSheet.Visible = True

Don't forget to set all of the objects to nothing, and the Quit the Excel Application when you're done.

If Not (WrkSheet Is Nothing) Then
Set WrkSheet = Nothing
End If
If Not (WrkBook Is Nothing) Then
WrkBook.Close
Set WrkBook = Nothing
End If
If Not (objXL Is Nothing) Then
If Not (objXL.Workbooks Is Nothing) Then
objXL.Workbooks.Close
End If
objXL.Quit
Set objXL = Nothing
End If Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hey there,

How about trying this:

With objXL.ActiveWorkbook
.Worksheets.Add
.Worksheets("Sheet1").Name = strname
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top