Results 1 to 19 of 19

Thread: VB6 How to read and write to an Excel file

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2003
    Posts
    37

    VB6 How to read and write to an Excel file

    How can Vb6 read from an Excel file and store the data in an Array with two dimensions. The data from cell A1 to store in Arrays a11 , the cell B1 to store in a12 the A2 to store in a21 the
    cell B2 to store in a22 etc.
    Also how can write to an Excel file from An Array With two dimensions and the data to go to the excel file as above
    a11 to cell A1 , a12 to cell B1, a21 to cell A2, a22 to cell B2 etc

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    I suppose your Array is more like a(1,1), a(1,2) ....
    The code to read from file
    VB Code:
    1. Dim xlApp As Excel.Application
    2. Dim xlWB As Excel.Workbook
    3. Dim xlSH As Excel.Worksheet
    4. Dim i As Integer
    5. Dim j As Integer
    6. 'open excel application
    7. Set xlApp = New Excel.Application
    8. 'Open excel workbook
    9. Set xlWB = xlApp.Workbooks.Open(FileName:="C:\YourFile.xls" )
    10. 'Suppose you read from Sheet 1
    11. Set xlSH = xlWB.Worksheets(1)
    12. Reads Cells A1 to E10 into a(1,1) to a(10,5)
    13. For i=1 to 5
    14.    for j=1 to 5
    15.         a(i,j)=xlSh.Cells(i,j).Value
    16.    Next j
    17. Next i
    18. 'Close workbook (optional)
    19. xlWB.Close
    20. 'Quit excel (automatically closes all workbooks)
    21. xlApp.Quit
    22. 'Clean up memory (you must do this)
    23. Set xlWB = Nothing
    24. Set xlApp = Nothing
    The code to write into the file
    VB Code:
    1. Dim xlApp As Excel.Application
    2. Dim xlWB As Excel.Workbook
    3. Dim xlSH As Excel.Worksheet
    4. Dim i As Integer
    5. Dim j As Integer
    6. 'open excel application
    7. Set xlApp = New Excel.Application
    8. 'Open excel workbook
    9. Set xlWB = xlApp.Workbooks.Open(FileName:="C:\YourFile.xls" )
    10. 'Suppose you read from Sheet 1
    11. Set xlSH = xlWB.Worksheets(1)
    12. Writes a(1,1) to a(10,5) into Cells A1 to E10
    13. For i=1 to 5
    14.    for j=1 to 5
    15.         xlSh.Cells(i,j).Value=a(i,j)
    16.    Next j
    17. Next i
    18. 'Close workbook (optional)
    19. xlWB.Close
    20. 'Quit excel (automatically closes all workbooks)
    21. xlApp.Quit
    22. 'Clean up memory (you must do this)
    23. Set xlWB = Nothing
    24. Set xlApp = Nothing

    __________________
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Banned
    Join Date
    May 2003
    Posts
    37
    Unfortunately didnot work
    Reads Cells A1 to E10 into a(1,1) to a(10,5)
    Writes a(1,1) to a(10,5) into Cells A1 to E10
    Both give "Expected: end of Statement"
    And when I had tried to run the program
    Dim xlApp As Excel.Application gave "User -defined type not defined.

  4. #4
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Before you can use/reference Excel in VB, you have to have your project reference the Excel/Office Library. That's the problem. Late, Jeremy
    He who listens well, speaks well.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Oh, sorry, I forget to put the use the coment marks for the Read and Write line, also forget the reference to Excel.
    That happens if you try to help, when your not on your programming computer! sorry
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6

    Thread Starter
    Banned
    Join Date
    May 2003
    Posts
    37
    I Still have problem to run the application correctly.
    opus or JCScoobyRS please complete the application with the
    reference to Excel.
    Thanks

  7. #7
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Here is a simple app to open an Excel File and write to it then display it:

    [reminder]Don't forget to reference the Excel Object Library or the Office Object Library[/reminder]

    VB Code:
    1. Private Sub Form_Load()
    2.      Dim objExcel As Excel.Application
    3.  
    4.      Set objExcel = New Excel.Application
    5.  
    6.      objExcel.Workbooks.Open App.Path & "\Sample.xls" 'Create a Sample.xls file in the directory where your program is at.
    7.      objExcel.Worksheets("Sheet1").Activate 'Activate the proper Workbook Sheet1
    8.  
    9.      objExcel.Cells(1,1).Value = "Hello!" 'The first 1 is for Row Number and the last is for Column Number
    10.      objExcel.Cells(2,1).Value = "1"
    11.      objExcel.Cells(3,1).Value = "2"
    12.      objExcel.Cells(4,1).Value = "3"
    13.      objExcel.Visible = True
    14. End Sub

    For more advanced techniques, search or run a macro and see how VBA does it then alter the code to work for VB. Let me know if this works, Jeremy
    He who listens well, speaks well.

  8. #8
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    For this to work you have to have these:

    1) Excel/Office Library Referenced
    2) A project saved to harddrive so App.Path can work
    3) Sample.xls created and saved in same folder as project

    That's it. Let us know, Jeremy
    He who listens well, speaks well.

  9. #9

    Thread Starter
    Banned
    Join Date
    May 2003
    Posts
    37
    What I dont understand is the reference to the Excel Object Library .How to add the reference to Excel/office Object Library

  10. #10
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    When you have a project, you can follow these directions to reference Excel/Office Libraries:

    1) While inside the project, on the top menu, click Project and go to the bottom where it says References... and click that.
    2) Once in the references, scroll down until you get to:
    Microsoft Office X.X Object Libray and put a check in the box
    beside it. Note: X.X is version number like 10.0 or 9.0

    That is all you have to do to reference Excel/Office. If you need more help, let me know. HTH, Jeremy
    He who listens well, speaks well.

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    When in the IDE, open the menu Project and References (i hope it'S the correct english name for that, it'as the third from the bottom). In the opening window you can select the checkmark next to the MS EXCEL Object libary.
    That's it!
    Dammed too slow!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12

    Thread Starter
    Banned
    Join Date
    May 2003
    Posts
    37
    opus and JCScoobyRS Thank you very much .Now the program is working perfectly. During writing in the output Excel file if i want to
    save changes with out asking me if i want to save changes what I have to add in the program?

  13. #13
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    It should be something like
    VB Code:
    1. xlWb.Save
    Assuning xlWb is an EXCEL-Workbook Object at that time!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  14. #14
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    You also have to have this so the pop up doesn't come up:

    VB Code:
    1. objExcel.DisplayAlerts = False

    That line lets you save, close, exit without the "Yes - No - Cancel" dialog box popping up. Later, Jeremy
    He who listens well, speaks well.

  15. #15
    New Member
    Join Date
    Feb 2003
    Location
    Malaysia
    Posts
    13
    All,

    I tried to use the same way to Export some data from VB apps to Excel file, but I encounter one weird problem. That's is, you must exit your VB application first (or unload the form) before you can open up the Excel file just exported from your applications.

    anybody knows the reason behind this? and what is the work around for this??

  16. #16
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Not true. You have to do this:

    VB Code:
    1. objExcel.Visible = True

    That will make the Excel.Application Object visible. Give that a try and let me know. Thanks, Jeremy
    He who listens well, speaks well.

  17. #17
    New Member
    Join Date
    Feb 2003
    Location
    Malaysia
    Posts
    13
    Hi Jeremy,

    I tried to use that objExcel.Visible = True. The outcome is the same. When I lauched the Excel while my Vb apps is still on, the Excel is running fine, but the "Workbook" sub-form just stuck there, I can not see the workbook exported from the VB apps. Both objExcel.Visible = True or lauching applications manually give the same result. I tried to run it on other PC, the result still the same.

    I herewith attached the file for your reference. Apparently, this sample VB apps (i download it some where) unload the form to make it works.
    Attached Files Attached Files

  18. #18
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    From looking at your project...I'd create a template Excel file and open it in code instead of creating a new one. It's easier to deal with and manage. I have posted code earlier in this thread with instructions and code. If you'd like more help, let me know. Thanks, Jeremy
    He who listens well, speaks well.

  19. #19
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Just so you know though...I just ran your program and it created the Excel file and showed it to me. Kind of wierd. So when you click all the buttons in your program, the Excel object isn't visible or the worksheet isn't visible or what? HTH, Jeremy
    He who listens well, speaks well.

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