|
-
Nov 2nd, 2005, 05:52 AM
#1
Thread Starter
New Member
(RESOLVED)accessing excel 2003 from visual studio 2005 express
can someone assist i am trying to access excel 2003 from Visual Studio Express 2005 express i am using the code below which opens excel but then gives me an error of com exception was unhandled and highlights the code line " owb= oxl.workbooks.add", any ideas.
VB Code:
Public Class Form1
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
' Make sure Excel is visible and give the user control
' of Excel's lifetime.
oXL.Visible = True
oXL.UserControl = True
' Make sure that you release object references.
oRng = Nothing
oSheet = Nothing
oWB = Nothing
oXL.Quit()
oXL = Nothing
Exit Sub
Err_Handler:
MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
End Sub
End Class

Edit: Added [vbcode][/vbcode] tags for clarity. - Hack
Last edited by cassie1; Nov 4th, 2005 at 06:00 AM.
Reason: Resolved
-
Nov 2nd, 2005, 06:43 AM
#2
Re: accessing excel 2003 from visual studio 2005 express
-
Nov 2nd, 2005, 05:11 PM
#3
Lively Member
Re: accessing excel 2003 from visual studio 2005 express
Have you added a reference to Excel?
This site explains how to automate excel utilising .Net
http://support.microsoft.com/kb/301982/EN-US/
Hope this helps.
-
Nov 3rd, 2005, 04:10 AM
#4
Thread Starter
New Member
Re: accessing excel 2003 from visual studio 2005 express
Hi sonic Boom
yes ive tried that but it doesnt work i keep getting (com exception was unhandled ) and it highlights the code "owb=oxl.workbooks.add", but it does open excel. Any suggestions
-
Nov 3rd, 2005, 07:15 AM
#5
Re: accessing excel 2003 from visual studio 2005 express
Maybe your excel application is not properly created using the CreateObject("Excel.Application") and use this
VB Code:
Dim oXL As New Excel.Application
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 3rd, 2005, 09:11 AM
#6
Thread Starter
New Member
Re: accessing excel 2003 from visual studio 2005 express
Hi Jorge,tried that but gave me (New cannot be used on an interface)
Cassie 1
-
Nov 3rd, 2005, 04:20 PM
#7
Lively Member
Re: accessing excel 2003 from visual studio 2005 express
This is how I normally connect to an Excel spreadsheet
VB Code:
' Start Adding Details to a New Excel Workbook
Dim objApp As Excel.Application
Dim objBook As Excel._Workbook
Dim objBooks As Excel.Workbooks
Dim objSheets As Excel.Sheets
Dim objSheet As Excel._Worksheet
Dim range As Excel.Range
Dim PR As DialogResult
Try
' Create a new instance of Excel and start a new workbook.
objApp = New Excel.Application
objBooks = objApp.Workbooks
objBook = objBooks.Add
objSheets = objBook.Worksheets
objSheet = objSheets(1)
' Your code goes here
' Exit out of Excel
objsheets = nothing
objSheet = Nothing
objBook = Nothing
objBooks = Nothing
objApp.Quit()
objApp = Nothing
Catch ex As Exception
' Inform the user of the error.
MessageBox.Show("An error occured! Error: " & ex.Message, _
"Title", MessageBoxButtons.OK, MessageBoxIcon.Error)
' Exit out of Excel
objsheets = nothing
objSheet = Nothing
objBook = Nothing
objBooks = Nothing
objApp.Quit()
objApp = Nothing
End Try
Hope this helps
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|