Results 1 to 12 of 12

Thread: [RESOLVED] Call Excel Sub from VB6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Resolved [RESOLVED] Call Excel Sub from VB6

    I have a prog that opens up an excel workbook. I am trying to call a sub that is written in the excel workbook (vba) from my vb6 prog.

    Is this possible?
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Call Excel Sub from VB6

    Try calling the Macro (from within Vb6) like:
    VB Code:
    1. Call [ExcelObject]Module1.Macro1

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: Call Excel Sub from VB6

    so you can call a macro but not a sub on a worksheet?
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Call Excel Sub from VB6

    Either/Or.

    I'm just testing the theory now.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: Call Excel Sub from VB6

    ya i tried that it doesnt wanna work for me

    any other suggestions?
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Call Excel Sub from VB6

    Worked for me:
    VB Code:
    1. Call objExcel.Sheets(1).Test_Sub


    Please post the code your using open Excel from within VB (I'll mod it to suite)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: Call Excel Sub from VB6

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim myExcelApp As Excel.Application
    3.  
    4.     Dim myExcelSheet As Excel.Worksheet
    5.  
    6.  
    7.     ' Create the Excel application.
    8.     Set myExcelApp = New Excel.Application
    9.    
    10.     'open workbook
    11.     myExcelApp.Workbooks.Open "C:\Program Files\Iron Test Stand\Graph.xls"
    12.     myExcelApp.Visible = True
    13.    
    14.     Set myExcelSheet = Sheets("New Graph")
    15.    
    16.     myExcelSheet.Select
    17.    
    18.    
    19.         With myExcelApp.ActiveSheet
    20.             .txtfilename.Activate
    21.             .txtfilename.Text = "C:\Documents and Settings\Jon\Desktop\Iron Tests\2039\123.456-August 08, 2005.csv"
    22.             'CALL CMDBROWSW_CLICK()
    23.             'The above doesnt work HEEEEEEEEEEELPPPPP lol
    24.            
    25.            
    26.         End With
    27.    
    28. End Sub
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Call Excel Sub from VB6

    Try:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim myExcelApp As Excel.Application
    5.  
    6.     Dim myExcelSheet As Excel.Worksheet
    7.  
    8.  
    9.     ' Create the Excel application.
    10.     Set myExcelApp = New Excel.Application
    11.    
    12.     'open workbook
    13.     myExcelApp.Workbooks.Open "C:\Program Files\Iron Test Stand\Graph.xls"
    14.     myExcelApp.Visible = True
    15.    
    16.     Set myExcelSheet = Sheets("New Graph")
    17.    
    18.     myExcelSheet.Select
    19.    
    20.    
    21.         With myExcelApp.ActiveSheet
    22.             .txtfilename.Activate
    23.             .txtfilename.Text = "C:\Documents and Settings\Jon\Desktop\Iron Tests\2039\123.456-August 08, 2005.csv"
    24.             CALL .CMDBROWSW_CLICK()
    25.             'Note the . (Dot) preceeding the Sub!
    26.            
    27.            
    28.         End With
    29.  
    30.   Set myExcelApp = Nothing
    31.  
    32. End Sub

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Call Excel Sub from VB6

    I added Option Explicit, and released Memory (by Setin myExcelApp = Nothing)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: Call Excel Sub from VB6

    Lol im retarded

    I was calling Private Sub cmdBrowse_Click()

    not Public

    DOH

    Thanks for the help coach! It works great!!!
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: [RESOLVED] Call Excel Sub from VB6

    Great (oops, I forgot to mention that my testSub was Public too..... sorry )

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: [RESOLVED] Call Excel Sub from VB6

    Well either way +5 Rep for you
    thanks
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

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