Results 1 to 11 of 11

Thread: [RESOLVED] Visual Basic 6 Through Excel

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [RESOLVED] Visual Basic 6 Through Excel

    hi everyone

    I have a program made by VB6 contains some textboxes and some commandbuttuns,On my desktop there is an existing Excel file (my2k.xls).

    NOW: I want the user to write some numbers and letters in the textboxes and then clicks the command button.
    The letters and numbers should be transferred in single cells on that Excel file (my2k.xls).For example :

    user writes : ORDER 123

    The program transfers that to the excel sheet on specific cells like : O R D E R 1 2 3 each letter or number in single cell.

    Witch means : first cell (S8 = "O") second cell (T8 = "R") etc..


    The Q's now:

    1- How to make VB6 (and not VBA) connects with that existing Excel file and communicate with it?

    2-How to transfer texts from VB6 to certain cells on that Excel sheet?

    I really need this and I wish someone could help.

    If not too much, attached open source example would be helpful.

    Thank you very much for your respond

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Visual Basic 6 Through Excel

    check out the tutorial, for automating office applications including excel, in the second thread in the office development forum
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Thumbs up Re: Visual Basic 6 Through Excel

    Thank you very much westconn1

    First problem solved so far

    In process to solve the second one and get the job done.

    I used this thread :

    http://www.vbforums.com/showthread.php?t=391665

    Was so helpful and rich of infos

    <--------<< This guy will get promoted so soon

  4. #4

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Visual Basic 6 Through Excel

    Guess What ??
    Not solved yet !!
    The second question I mean !!
    And I'm 100% sure it's so easy..But maybe I'm not making this clear for many.
    so plz let me know..

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Visual Basic 6 Through Excel

    post the code you have so far,

    basically
    workbookobject.sheets("sheet1").range("b99").value = "my test"
    use whatever workbook object you have already created
    or if you have created a sheet object, you can omit the workbook object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Visual Basic 6 Through Excel

    Thanks for your respond

    my codes are :

    General:
    Code:
    Dim oXLApp As Excel.Application         'Declare the object variables
    Dim oXLBook As Excel.Workbook
    Dim oXLSheet As Excel.Worksheet
    FormLoad:
    Code:
    Private Sub Form_Load()
    Set oXLApp = New Excel.Application    'Create a new instance of Excel
    Set oXLBook = oXLApp.Workbooks.Open("C:\Program Files\2K Writer\2Kilo Form.xls") 'Open an existing workbook
    Set oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet
    End Sub

    CommandButton:
    Code:
    oXLSheet.Range("S8").Formula = Text1.Text     'here is my problem (It works but not like I want it)
    I want (text1.text) to be Distributed in S8 , T8 , U8 and V8

    let's say text1.text = "NICE"

    After Command click it should be :

    S8 = N
    T8 = I
    U8 = C
    V8 = E



    I hope it's clear now and thank you again.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Visual Basic 6 Through Excel

    To work with individual characters from a string use the Mid function, eg:
    Code:
    MsgBox Mid("hello", 5, 1) 'displays just the 5th character
    To determine how long a string is, use the Len function, eg:
    Code:
    MsgBox Len("hello") 'displays 5
    So to work with each character in a string, use a loop:
    Code:
    Dim intLoop as Integer
      For intLoop = 1 to Len(Text1.Text)
        MsgBox Mid(Text1.Text, intLoop, 1)  'displays each character in turn
      Next intLoop
    All you need to do now is change MsgBox to something that puts the data into Excel. Note that it will be easier to use .Cells rather than .Range

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Visual Basic 6 Through Excel

    if the letters are spaced or anything they could be split into an array and the array assigned to the cells in a range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Visual Basic 6 Through Excel

    Sorry didn't work !!

    The project has many textboxes to be pasted or transferred to the sheet..

    I need a small change to this code:
    Code:
    oXLSheet.Range("S8").Formula = Text1.Text
    Or maybe it's something to do with the Excel sheet to make what is pasted in S8 distributed into the individual cells.

    If not. How to pick litters from one textbox? like I only want to copy the 5th letter from text1.

    Thank you again.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Visual Basic 6 Through Excel

    like I only want to copy the 5th letter from text1.
    si gave you the code to do that before
    letter = mid(text1, 5 ,1)

    you can make a loop to put your string into different cells
    this will put into row 2
    vb Code:
    1. for i = 1 to len(text1)
    2.      oxlsheet.cells(2, i).value = mid(text1, i, 1)
    3. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [RESOLVED]: Visual Basic 6 Through Excel

    Thank you very much

    my problem got solved finally

    I used this code and it worked very well:
    Code:
    Dim intLoop As Integer
    For intLoop = 1 To Len(Text1)
         oXLSheet.Cells(7, 19 + intLoop).Value = Mid(Text1, intLoop, 1)
    Next
    Best Regards
    And thank you again.
    Last edited by Joye; Jul 19th, 2009 at 01:51 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