473,812 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cant read in excel data?

23 New Member
Hi,

i cant read in data from excel 97 using vb.net 2003.

I get an exception error .

all i want to do is read in values from excel initially. I want to just test it using msgbox.

Expand|Select|Wrap|Line Numbers
  1. Dim XL As Object 'workaround for excel 97
  2.         XL = Interaction.CreateObject("Excel.Application")
  3.         Dim XlWbk As Excel.Workbook = XL.Workbooks.Add()
  4.  
  5.         Dim MyString As String
  6.         MyString = Now().ToString("yyyy-MM-dd")
  7.         Try
  8.  
  9.             ' XL.Visible = True
  10.  
  11.  
  12.             Dim XlSh As Excel.Worksheet = CType(XlWbk.Sheets(1), Excel.Worksheet)
  13.             Dim range As Excel.Range
  14.             Dim xlWsheet As Excel.Worksheet = XL.Worksheets("Sheet1")
  15.             Dim r As Excel.Range
  16.  
  17.             XlWbk = XL.Workbooks.Open("mytest.xls")
  18.  
  19.             XlWbk.Activate() '' System.Runtime.InteropServices.COMException,
  20.             range = XlSh.Range("A1").Value
  21.  
  22.             XlWbk.Close()
  23.  
  24.  
  25.             XL.Quit()
  26.             MsgBox(r.Cells(0, 1).Value)
  27.  
  28.  
Mar 3 '08 #1
6 2904
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

In VB.net, "Activate" is used for many Objects. May be the error is because of that, use "CType"

Expand|Select|Wrap|Line Numbers
  1. Dim XL As Object 'workaround for excel 97
  2.         XL = Interaction.CreateObject("Excel.Application")
  3.         Dim XlWbk As Excel.Workbook = XL.Workbooks.Add()
  4.  
  5.         Dim MyString As String
  6.         MyString = Now().ToString("yyyy-MM-dd")
  7.         Try
  8.  
  9.             ' XL.Visible = True
  10.  
  11.  
  12.             Dim XlSh As Excel.Worksheet = CType(XlWbk.Sheets(1), Excel.Worksheet)
  13.             Dim range As Excel.Range
  14.             Dim xlWsheet As Excel.Worksheet = XL.Worksheets("Sheet1")
  15.             Dim r As Excel.Range
  16.  
  17.             XlWbk = XL.Workbooks.Open("mytest.xls")
  18.  
  19.             XlWbk= CType(XL.Workbooks.Item(1), Excel.WorkBook)              
  20.  
  21.             XlWbk.Activate() 
  22.             range = XlSh.Range("A1").Value
  23.  
  24.             XlWbk.Close()
  25.  
  26.  
  27.             XL.Quit()
  28.             MsgBox(r.Cells(0, 1).Value)
  29.  
  30.  
REgards
Veena
Mar 3 '08 #2
jagguy
23 New Member
Hi,
I have changed the code and still get an error when i simply try to display a value from excel.

Dim XL As Object

XL = Interaction.Cre ateObject("Exce l.Application")

Dim XlWbk As Excel.Workbook = XL.Workbooks.Ad d()

Try


' XlSh.Range("A2" ).Value = MyString 'Set a value
Dim xlsApp As Excel.Applicati on
Dim xlsWB As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim xlsCell As Excel.Range
Dim xlsDatei As String

xlsApp = New Excel.Applicati on
' xlsApp.Visible = True
xlsWB = xlsApp.Workbook s.Open("book1.x ls")
xlsSheet = xlsWB.Worksheet s(1)
'xlsCell = xlsSheet.Range( "A1")
xlsCell = xlsSheet.Cells( 1, 1).value() 'cast error

MsgBox(xlsCell)
Mar 4 '08 #3
jagguy
23 New Member
also i cant use ctype with activate

CType(XlWbk, Excel.Worksheet ).Activate() 'error

I cant find any basic tutorial with vb.net and excel where you can read data from a excel file and write values back eg just summing up a few columns and write back the total.

Is this all possible because i have spent hours on seemingly just basici connection and basic tasks. Where is the help in vb.net for excel?
Mar 4 '08 #4
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

I have changed the code in 19th line.(prev post)
You have not done the changes in your code..

WorkBook cannot be Casted to Worksheet.. Try This:

CType(XlWbk, Excel.WorkBook) .Activate()

Regards
Veena
Mar 4 '08 #5
jagguy
23 New Member
i still get an error as before with this

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.             Dim XlSh As Excel.Worksheet = CType(XlWbk.Sheets(1), Excel.Worksheet)
  4.             Dim range As Excel.Range
  5.  
  6.             XlWbk = XL.Workbooks.Open("mytest.xls")
  7.             XlWbk = CType(XL.Workbooks.Item(1), Excel.Workbook)
  8.             CType(XlWbk, Excel.Workbook).Activate() 'same error as before
  9.  
  10.  
  11.  
Mar 4 '08 #6
jagguy
23 New Member
i can connect now to a excel sheet and open and save to it.

Is there a way to assign a range of values read in from excel moved to a datagrid?
Mar 6 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
394
by: Dianna K | last post by:
I am opening, reading and extracting an Excel spreadsheet in .Net (VB). It seems to work fine, however, when I am finished and I open up Excel and try to open up the file a message pops up "File Now Available", "select Read-Write or Cancel". Its almost like my code is changing the documents attributes. Am I doing something wrong in my code Any help would be great. Thanks Diann PS: I'm not quite sure why, but it seems that I have to use...
0
4708
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading the Microsoft based files with an OleDbDataAdapter. Then filling the contents of the first worksheet into a dataset. However when I try to add the XML based file to my dataset using an XmlTextReader I can never seem to get it to save to a...
4
5852
by: Marco Aschwanden | last post by:
Hi I would like to 1. import an existing Excel-sheet (a template) 2. and add some data to it 3. and save it under a different name afterwards. To me it seems, that pyExcelerator does not support the reading for modification of an Excel-sheet. It allows only the "parse_xls" but I would like to keep the "formatting" in the template.
0
3677
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle worksheet first.. Unfortunately my code was throwing error like
1
6166
by: sorcerdon | last post by:
Hello experts, I am currently opening an excel file from a VB.net application. The excel spreadsheet gets all its data from a Dataset. I need to hide a column form the results. I dont want the users to have access to that perticular column in the excel spreadsheet. Here is my current code:
1
12466
by: aotemp | last post by:
Hi, Im having a reaaally hard time with something... Im trying to read a cell of data into a String variable. It seems like such a simple task too... Get the excel spreadsheet, get the workbook, get the sheet, get the cell, get the cells data and store it into a string. But it wont work! Im really frustrated. the best I got was "System.___ComObject" coming out of the string.
5
43433
by: barbara_dave | last post by:
Hi All, I need to read data from a Excel spreadsheet, but I got the problem when I tried the code below: StringBuilder sbConn = new StringBuilder(); sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" ); sbConn.Append(excelFile); sbConn.Append(";Extended Properties="); sbConn.Append(Convert.ToChar(34));
3
6253
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" ...
3
5403
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"...
0
10664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10417
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9219
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.