Results 1 to 7 of 7

Thread: (RESOLVED)accessing excel 2003 from visual studio 2005 express

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    3

    Resolved (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:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.  
    5.         ' This call is required by the Windows Form Designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, _
    13. ByVal e As System.EventArgs) Handles Button1.Click
    14.         Dim oXL As Excel.Application
    15.         Dim oWB As Excel.Workbook
    16.         Dim oSheet As Excel.Worksheet
    17.         Dim oRng As Excel.Range
    18.  
    19.         ' Start Excel and get Application object.
    20.         oXL = CreateObject("Excel.Application")
    21.         oXL.Visible = True
    22.  
    23.         ' Get a new workbook.
    24.         oWB = oXL.Workbooks.Add
    25.         oSheet = oWB.ActiveSheet
    26.  
    27.         ' Make sure Excel is visible and give the user control
    28.         ' of Excel's lifetime.
    29.         oXL.Visible = True
    30.         oXL.UserControl = True
    31.  
    32.         ' Make sure that you release object references.
    33.         oRng = Nothing
    34.         oSheet = Nothing
    35.         oWB = Nothing
    36.         oXL.Quit()
    37.         oXL = Nothing
    38.  
    39.         Exit Sub
    40. Err_Handler:
    41.         MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
    42.     End Sub
    43.  
    44. End Class







    Edit: Added [vbcode][/vbcode] tags for clarity. - Hack
    Last edited by cassie1; Nov 4th, 2005 at 06:00 AM. Reason: Resolved

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: accessing excel 2003 from visual studio 2005 express

    Moved to .NET

  3. #3
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    3

    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

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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:
    1. Dim oXL As New Excel.Application

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    3

    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

  7. #7
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    Re: accessing excel 2003 from visual studio 2005 express

    This is how I normally connect to an Excel spreadsheet

    VB Code:
    1. ' Start Adding Details to a New Excel Workbook
    2.         Dim objApp As Excel.Application
    3.         Dim objBook As Excel._Workbook
    4.         Dim objBooks As Excel.Workbooks
    5.         Dim objSheets As Excel.Sheets
    6.         Dim objSheet As Excel._Worksheet
    7.         Dim range As Excel.Range
    8.         Dim PR As DialogResult
    9. Try
    10. ' Create a new instance of Excel and start a new workbook.
    11.             objApp = New Excel.Application
    12.             objBooks = objApp.Workbooks
    13.             objBook = objBooks.Add
    14.             objSheets = objBook.Worksheets
    15.             objSheet = objSheets(1)
    16.  
    17. ' Your code goes here
    18.  
    19. ' Exit out of Excel
    20.             objsheets = nothing
    21.             objSheet = Nothing
    22.             objBook = Nothing
    23.             objBooks = Nothing
    24.             objApp.Quit()
    25.             objApp = Nothing
    26.            
    27. Catch ex As Exception
    28.             ' Inform the user of the error.
    29.             MessageBox.Show("An error occured! Error: " & ex.Message, _
    30.             "Title", MessageBoxButtons.OK, MessageBoxIcon.Error)
    31.  
    32.             ' Exit out of Excel
    33.             objsheets = nothing
    34.             objSheet = Nothing
    35.             objBook = Nothing
    36.             objBooks = Nothing
    37.             objApp.Quit()
    38.             objApp = Nothing
    39.  
    40.         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
  •  



Click Here to Expand Forum to Full Width