Results 1 to 30 of 30

Thread: [RESOLVED] VB6 & Excel newbie question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Resolved [RESOLVED] VB6 & Excel newbie question

    I am relatively new with working with Office from inside VB, and have never done this before, so would like some tips.

    I have VB6 and Office 97. What I want is to know how to do basic stuff to an Excel sheet from inside a VB program, such as:

    open an existing workbook
    Find/Go to a particular worksheet inside that workbook
    Find the last row in use on that sheet
    Find the contents of a cell in that row
    Copy a formula from one cell to another, so that the formula in the new cell has references to the new row it sits in
    Write values from variables into cells on the sheet
    How to get the current date and put it into a cell
    How to set the format of a cell or range of cells

    What I'm doing is trying to write a program that will update a bunch of workbooks/sheets from inside one program, rather than my opening each sheet or book and manually typing in changes.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    To automate Excel from VB6....
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. Private moApp As Excel.Application
    4.  
    5. Private Sub Command1_Click()
    6.     Dim oWB As Excel.Workbook
    7.    
    8.     moApp.Visible = True
    9.     Set oWB = moApp.Workbooks.Open("C:\Book1.xls")
    10.     oWB.Sheets(2).Activate
    11.     MsgBox "Last used row: " & oWB.Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Row
    12.     MsgBox "Contents: " & oWB.Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Value
    13.    
    14.     oWB.Close SaveChanges:=False
    15.     Set oWB = Nothing
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Set moApp = New Excel.Application
    20. End Sub
    21.  
    22. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    23.     If TypeName(moApp) <> "Nothing" Then
    24.         moApp.Quit
    25.     End If
    26.     Set moApp = Nothing
    27. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, where you have:

    oWB.Sheets(2).Activate

    What if I want to reference a sheet by its name, which is stored in a string text variable? Such as 'Alydar286'?

    It looks like you answered the first half of my question list, and that in itself is a help, thanks! Now, to get at that other half...

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    You can reference sheet names instead of index numbers.
    VB Code:
    1. Application.WorkBooks("Book1.xls").Sheets("Alydar286").Select 'Or.Activate
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Here are some more answers.
    VB Code:
    1. Dim strName As String
    2. strName = "RobDog888"
    3. oWB.Sheets(2).Cells(1, 1).Value = strName
    4. oWB.Sheets(2).Cells(2, 1).Value = Date
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Thank you! My project is humming right along except for a snag I just hit...

    I have Office 2000 AND Office 97 loaded on my machine. The reason is I have older programs that need Office 97 and won't function with Office 2000 documents.

    Now, when I use this code to open up an existing spreadsheet, it opens it in Excel 2000. How can I tell it to open it using Excel 97 instead?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Hard way: ShellExecute API to start Excel 97 and pass the workbook to open as a parameter. Only issue is knowing the fill path to the 97 location. Then once its started you will have to GetObject to attach to the Application Instance.

    Easy way: place code in your app to make it late bound and use which ever version is on the users system. Then select case on the version and only do things that that version supports.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Hard way: ShellExecute API to start Excel 97 and pass the workbook to open as a parameter. Only issue is knowing the fill path to the 97 location. Then once its started you will have to GetObject to attach to the Application Instance.

    How do you do this?

    Easy way: place code in your app to make it late bound and use which ever version is on the users system. Then select case on the version and only do things that that version supports.

    It's not a matter of things that are supported, it's the format. I suppose it would be ok to open and use 2000 to make simple changes, but then the program would have to Save As the file to 97 format, or the other older programs would no longer be able to open it.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Easy way: Use latebinding so the version could be any. Then when you need to save the workbook, save it using the 95/97 Excel file format.
    VB Code:
    1. ActiveWorkbook.SaveAs Filename:="C:\Book1.xls", FileFormat:=xlExcel9795
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Well, then that leads me to the question, what is latebinding?

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Ok, Early Binding vs. Late Binding...
    VB Code:
    1. 'Early Binding
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. Private moApp As Excel.Application
    4.  
    5. 'Late Binding
    6. 'No reference added to Excel at all
    7. Private moApp As Object
    Early binding locks in your reference to that version of Excel. Late binding does not. It only binds to the version that is available on the system.

    With Early binding you get the intellisense popups showing the functions, methods, and properties of your object variable. Late binding does not support any intellisense since it doesnt know what it is until runtime.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Early binding locks in your reference to that version of Excel. Late binding does not. It only binds to the version that is available on the system.

    Ok, the problem with that is, my system has BOTH versions available, so it's going with the newer version. I tried chang my reference from version 9 (Office 2000) to version 8 (Office 97) and it still opened the sheet using Excel 2000.

    I'm thinking I may have to just uninstall Office 2000, since the only thing I use it for anyway is on occasion opening an Access 2000 database. But I'd like to be able to just tell my program use THIS version of Excel!

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Then were back to the ShellExecute API thing. If this is for deployment then you will also have to dynamically find the location of Excel in order to shell it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    No, this particular program will only be used on my own system. But I do know the exact path to my Excel 97 executable. So, how do I go about doing this?

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Here is the code that will open an excel workbook using the specified version.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_SHOWMAXIMIZED As Long = 3
    8.  
    9. Private Sub Command1_Click()
    10.     ShellExecute Me.hwnd, "Open", "Excel.exe", "D:\Book1.xls", "D:\Program Files\Microsoft Office\OFFICE11", SW_SHOWMAXIMIZED
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, now how do I get to where I can move around in the workbook to different sheets and put stuff in cells, etc.? You mentioned GetObject earlier?

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    I'm working on an example but its a bit more complex. Will be back in a few.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, I'll be waiting. The project works well to this point. I pull information out of a database to populate some combo lists, select the stuff I want on the form, enter a few strings into some textboxes, click the button, and the correct version of Excel now comes up and opens the sheet I want to work on. Just need to figure out how to send my info to the sheet.

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Thats an easy one. To save the workbook you do this
    VB Code:
    1. Application.WorkBooks("Book1.xls").Save
    2.  
    3. 'Or without the prompt dialog if its a new workbook.
    4. Application.WorkBooks("Book1.xls").SaveAs FileName:="C:\Book1.xls"
    5. Application.WorkBooks("Book1.xls").Saved = True
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    It's not the saving I need, it's the stuff before the saving. I have all these variables now with info in them that I need to put into the sheet into cells. I need to do all the stuff that I was talking about at the beginning of the thread: finding the last row, putting values into cells, copying formulas from one cell to another, etc.

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Oh, ok. Then its post #5 to write values to a particular cell and sheet.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, cutting and pasting some relevant code from other posts, I have something roughly like this:

    VB Code:
    1. Private moApp As Excel.Application
    2.  
    3. Private Sub Command1_Click()
    4.     Dim oWB As Excel.Workbook
    5.     Dim strName As String
    6.    
    7.     moApp.Visible = True
    8.     Set oWB = moApp.Workbooks.Open("C:\Book1.xls")
    9.    
    10.     oWB.Sheets(2).Activate
    11.     Application.WorkBooks("Book1.xls").Sheets("Alydar286").Select 'Or.Activate
    12.    
    13.     MsgBox "Last used row: " & oWB.Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Row
    14.     MsgBox "Contents: " & oWB.Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Value
    15.  
    16.     strName = "RobDog888"
    17.     oWB.Sheets(2).Cells(1, 1).Value = strName
    18.     oWB.Sheets(2).Cells(2, 1).Value = Date
    19.    
    20.     Application.WorkBooks("Book1.xls").Save
    21.  
    22.     'Or without the prompt dialog if its a new workbook.
    23.     Application.WorkBooks("Book1.xls").SaveAs FileName:="C:\Book1.xls"
    24.     Application.WorkBooks("Book1.xls").Saved = True
    25.  
    26.     oWB.Close SaveChanges:=False
    27.     Set oWB = Nothing
    28. End Sub
    29.  
    30. Private Sub Form_Load()
    31.     Set moApp = New Excel.Application
    32. End Sub

    Now, will these sorts of things work given that I had to use the API call to ShellExecute in order to open Excel and the sheet rather than the normal way? Also, with the mention of GetObject, I'm confused as to whether I can use the regular code to do what I want to do once the sheet is opened with the API call, or if I have to somehow use GetObject and do something else.

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    You dont need the SaveAs code if you want to save the workbook as the same name. In that case just the .Save is sufficient.

    We are only going to need the api if you need to open a certain version of excel. If not then the default version that openss will be easiest and allow us to use the code like we are.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Right, but in my case it was determined that I do need the API unless I uninstall Office 2000 from my system. My program now uses the API to open an instance of Exel 97 and opens the Workbook in question that I need. For ease of illustration, I think I'll just post my code here:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim Excel As Excel.Application
    4.     Dim Book As Excel.Workbook
    5.     Dim Row As Integer
    6.     Dim Column As String
    7.     Dim Msg, Style, Title, Response
    8.  
    9.     Msg = "Process this sale ?"
    10.    
    11.     Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
    12.     Title = "Process Sale"
    13.  
    14.     Response = MsgBox(Msg, Style, Title)
    15.         If Response = vbYes Then    ' User chose Yes.
    16.    
    17.             Set Excel = New Excel.Application
    18.              
    19.             ShellExecute Me.hwnd, "Open", "Excel.exe", "C:\FHR\StableBankAccounts.xls", "C:\Program Files\Microsoft Office\Office", SW_SHOWMAXIMIZED
    20.            
    21.             Excel.Visible = True
    22.            
    23.             ' *** Place for code to do cell manipluations
    24.    
    25.         End If
    26.  
    27. End Sub

    This is my code as it currently sits. It opens Excel 97 and opens the workbook 'StableBankAccounts', which consists of multiple sheets, all with names such as 'Alydar286'. What I want to do is go to a particular sheet by name, find the last row, put my variables into cells on that row, copy a forumla from one cell to another, then save the sheet and close everything.

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    It will end up looking something like this without the api part complete.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim oExcel As Excel.Application
    4.     Dim oBook As Excel.Workbook
    5.     Dim iRow As Integer
    6.     Dim strColumn As String
    7.     Dim strMsg As String, strStyle As String, strTitle As String
    8.     Dim iResponse As Integer
    9.     Dim lRet As Long
    10.  
    11.     strMsg = "Process this sale ?"
    12.    
    13.     strStyle = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
    14.     strTitle = "Process Sale"
    15.  
    16.     iResponse = MsgBox(Msg, Style, Title)
    17.         If iResponse = vbYes Then    ' User chose Yes.
    18.    
    19.             'Set oExcel = New Excel.Application 'Not needed until later (API part)
    20.              
    21.             lRet = ShellExecute(Me.hwnd, "Open", "Excel.exe", "C:\FHR\StableBankAccounts.xls", "C:\Program Files\Microsoft Office\Office", SW_SHOWMAXIMIZED)
    22.            
    23.             ' ToDo: *** Place API code to get the Excel App object
    24.  
    25.  
    26.             'oExcel.Visible = True
    27.             'Set oBook = oExcel.WorkBooks(StableBankAccounts.xls)
    28.             ' *** Place for code to do cell manipluations
    29.             oBook.Sheets(1).Cells(1, 1).Value = "Something"
    30.             'Do other writting to cells
    31.             '...
    32.             '...
    33.             'Save updates and clean up obejcts
    34.             oBook.Save
    35.             oBook.Close
    36.             Set oBook = Nothing
    37.             oExcel.Quit
    38.             Set oExcel = Nothing
    39.         End If
    40.  
    41. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, now you've confused me again. I thought the ShellExecute WAS the api part. What's missing?

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    Yes, it is "part" of the APIs needed. The other part is iterating through the processes and only checking the Excel ones for the correct workbook, just in case you have more then one instance open.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 & Excel newbie question

    The GetObject function does not discriminate on the process, only the class type and server name - "Excel.Application"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ahhh, ok, I see. I will wait for that part, then.

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: VB6 & Excel newbie question

    Ok, Rob, I don't need the API any more.

    While waiting for your reply, I decided to see what would happen if I didn't use the API at all or the ShellExecute and did it the usual way, and let Excel 2000 do it. I got it to open my workbook, find the proper sheets, put the proper data in the proper cells, and save the sheet using SaveAs. Then I tried opening it in Excel 97, and it still opens fine and works like normal! So I guess I don't need to specify Excel 97 after all, and we can consider this question at last resolved!

    Of course, I now have another question, but that's a topic for another thread.

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