View Full Version : New to .Net and trying to create a program I wrote in VBA
mikeosborne
2009-12-10, 05:35 PM
I am still new to programming and very new to the .NET world and I thought I would start by recreating a program I just finished in VBA in VB 2008 Express Edition. and I have hit my first snag I am hoping some one can help me with this right now I just want be able to populate a combo box with the values from a column in an Excel spread sheet. This is the code I am using
Public XLapps As Microsoft.Office.Interop.Excel.Application
Public XLbook As Microsoft.Office.Interop.Excel.Workbook
Public XLsheet As Microsoft.Office.Interop.Excel.Worksheet
XLbook = GetObject(pthSup & "offices.xls")
XLsheet = XLbook.Sheets("Offices")
Dim i As Integer
For i = 1 To XLsheet.UsedRange.Rows.Count
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1))
Next
and it almost works, I get the right number of values in the combo box but every value is
System.__ComObject
Any ideas?
Ed Jobe
2009-12-10, 06:11 PM
What's in a Cells collection? Cells, right? Choose some property of the Cell object that holds a string value and use the ToString method of that property. Another option would be to use a datareader to link to the cells and use the datareader to populate as the datasource for a grid.
I am still new to programming and very new to the .NET world and I thought I would start by recreating a program I just finished in VBA in VB 2008 Express Edition. and I have hit my first snag I am hoping some one can help me with this right now I just want be able to populate a combo box with the values from a column in an Excel spread sheet. This is the code I am using
Public XLapps As Microsoft.Office.Interop.Excel.Application
Public XLbook As Microsoft.Office.Interop.Excel.Workbook
Public XLsheet As Microsoft.Office.Interop.Excel.Worksheet
XLbook = GetObject(pthSup & "offices.xls")
XLsheet = XLbook.Sheets("Offices")
Dim i As Integer
For i = 1 To XLsheet.UsedRange.Rows.Count
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1))
Next
and it almost works, I get the right number of values in the combo box but every value is
System.__ComObject
Any ideas?
Change this line:
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1)).Value2.ToString()
~'J'~
Ed Jobe
2009-12-10, 06:38 PM
Change this line:
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1)).Value2.ToString()
~'J'~
I think that should be:
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1).Value2.ToString())
I think that should be:
cmbOfficeLocation.Items.Add(XLsheet.Cells(i, 1).Value2.ToString())
Agreed :)
~'J'~
dgorsman
2009-12-10, 07:58 PM
VBA makes use of default values - that call to ...Cells() in VBA uses the default property ("Value", I think...), so it is actually ...Cells().Value. In .NET you need to explicitly put all this in.
It's easy to solve the problem you have, but its not easy to solve the problems that will result from doing what you're doing in the way you're doing it. So, I'll have be a bit critical of the other responses for faiing to recognize that or elaborate on why most of the time, you do not want to add strings to controls like the ComboBox or ListBox.
Rather than get into a long-winded discussion about that here, just download and play with this sample code:
http://www.caddzone.com/ExcelClientSample.zip
Brilliance, as always
Many regards,
~'J'~
Thanks, but no brillance here.
That's all basic .NET concepts that are covered in most good learning resources, which is why I strongly advocate that anyone migrating from legacy VB to .NET use those resources to learn about this kind of stuff.
it's merely about using .NET the way it was designed and intended to be used, verses using it as though one were still coding in legacy VB/VBA.
I try to go on this way too, but it not always is a success :)
Think it's by my poor level of knowlege VB.NET yet
Regards,
~'J'~
That's understandable especially if programming is not your full-time job. It takes years to learn .NET, and using legacy VB-style coding is fine in the interim, you just don't want to continue indefinitely.
Most here who are starting with .NET aren't full-time programmers, so the time they can set aside for leaning to use it, is limited, and that's where the quality learning resources become invaluable.
I have no job 15 years and within last 5 years I am engaged only in programming
Certainly, I do it sporadically and without textbooks, therefore I have not reached the big success on this way, simply I try to help there where I can make it
Besides, my IQ is too small also it's the reason
Anyway I'm still trying :)
Thanks for everything, you're the great teacher undoubtedly!
Still your fan,
~'J'~
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.