CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Trying to read data from the cells of a pre-existing Excel file

    (don't forget to add the reference)

    try:
    Code:
    		With OpenFileDialog1
    			.Filter = "XLS files (*.xls)|*.xls|All files (*.*)|*.*"
    			.FilterIndex = 1
    			.Title = "Open Template"
    			.ShowDialog() '<-first mistake
    		End With
    		If OpenFileDialog1.FileName <> "" Then
    			FilePath = OpenFileDialog1.FileName
    			Dim xlapp = CType(CreateObject("Excel.Application"), Excel.Application)
    			Dim xlbook As Excel.Workbook '<-Better for  writting code
    			Dim xlsheet As Excel.Worksheet
    			xlbook = xlapp.Workbooks.Open(FilePath)
    			xlsheet = CType(xlbook.Worksheets(1), Excel.Worksheet)
    			customername(1) = xlsheet.Cells(3, 2).value '<-explicit.value
    			customeraddress(1) = xlsheet.Cells(3, 3).value
    			customerphone(1) = xlsheet.Cells(3, 4).value
    			xlsheet = Nothing
    			xlbook.close(False)
    			xlbook = Nothing
    			xlapp = Nothing
    		End If
    Last edited by Marraco; March 14th, 2008 at 12:33 PM.

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

Featured