Tuesday, August 22, 2006 6:33 PM
Hi Hope some body can help with this: -
I have just installed Visual Studio 2005 Professional onto my PC and want to write an application that opens an Excel spreadsheet and reads data from a specific range in the sheet.
Problem is I cannot seem to import the Excel objects into the program. Looking around on the net I have found some code like this: -
ImportsExcel = Microsoft.Office.Interop.Excel
ImportsOffice = Microsoft.Office.Core
However this just produces an error to the effect that the office and excel lines are not recognised.
Can anyone tell me what the correct import would be to enable me to use the excel objects?
Tuesday, August 22, 2006 10:51 PMModerator
That's a good start, but you also need to add a reference to "Microsoft Excel 11.0 Object Library." Once you've added the reference, you can use the code in this article to get you started:
Wednesday, August 23, 2006 10:18 AM
Many thanks Jonathon
That did the trick.
Tried flagging this as the correct answer but got a script error.
Friday, September 08, 2006 9:26 AMCan this also be utilised using visual studio 6.0?
Friday, September 08, 2006 12:48 PM
Yes, there is a link at the bottom of the Visual Basic.NET article for Visual Basic 6.0:
Wednesday, September 27, 2006 12:35 PM
I also have the same requirement , that is populate an excel spread-sheet through visual basic , but the same problem persist for me also , I tried the link Jonathan Aneja have provided , but it's not working as of now. So can anybody kindly provide me the details required to add a reference to "Microsoft Excel 11.0 Object Library".
Thanks In Advance,
Wednesday, September 27, 2006 4:29 PM
I don't have VS open in front of me but from memory you select 'Add Reference' on the Project menu.
Hope this helps
Thursday, September 28, 2006 5:40 AM
Thanks for your comment and I was able to add reference for "Microsoft Excel 11.0 Object Library" , but even after adding a reference , I was able to see only "Microsoft.Office.Core" and "Microsoft.Office.Interop" was not present. So can you kindly help me out getting this done.
Tuesday, October 03, 2006 5:13 PM
I just ran this in VB express and it worked so give it a try: -
Create a form with a button and a checkbox called FillWithStrings
Then add this code to the form: -
ImportsOffce = Microsoft.Office.Core
objApp =New Excel.Application()
objBooks = objApp.Workbooks
objBook = objBooks.Add
objSheets = objBook.Worksheets
objSheet = objSheets(1)'Get the range where the starting cell has the address 'm_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.Range("A1", Reflection.Missing.Value)
range = range.Resize(5, 5)If (Me.FillWithStrings.Checked = False) Then
saRet(iRow, iCol) = iRow * iColNext iCol Next iRow 'Set the range value to the array.
range.Value = saRetElse 'Create an array. Dim saRet(5, 5) As String 'Fill the array. Dim iRow As Long Dim iCol As Long For iRow = 0 To 5
saRet(iRow, iCol) = iRow.ToString() +"|" + iCol.ToString() Next iCol Next iRow 'Set the range value to the array.
range.Value = saRetEnd If 'Return control of Excel to the user.
objApp.UserControl =True 'Clean up a little.
objBooks =Nothing End Sub
Hope it helps
Wednesday, October 04, 2006 12:51 PM
This works fine , thanks for your kind reply and the query I have raised in my last post was the Microsoft.Office.Core is getting imported but Microsoft.Office.Interop interface was not even present ( I'm using Visual Studio 2005 Proffessional Edition ) . Anyway I was able to accomplish the requirement with the code you have given , again thanks a lot for your response.
Thanks & Regards,
Saturday, May 26, 2007 10:05 AM