Discussion Locked

VB6 and Excel 97

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to be able to access and manipulate an excel 97 spreadsheet (with multiple sheets) from my own VB6 application, like you can do with Access Db files. I mainly need to be able to call macros to perform various actions on the data in the spreadhseet.

Failing that, I want to be able to open excel and the file from my app, then return to my app when finished.

Can anyone help?

M.R.Chambers

9 replies

by birk

Here's some sample code taken from an Access application of mine that should get you started. Do a searc on "automation" in the Excel Help file for more info.

Set xlapp = GetObject(, "Excel.Application")
If Err.number <> 0 Then
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo Err_ccsplit_Click

xlapp.Workbooks.Open "c:\academy\enrollment\ccsplitmaster.xls" ' if error getting this one, resume next
xlapp.Workbooks.Open "\\edpc\ed-c\academy\enrollment\ccsplitmaster.xls"
xlapp.Visible = True
xlapp.Sheets("Sheet1").Select
xlapp.Range("A1").Select
xlapp.ScreenUpdating = False ' turn off screen updating
' delete old detail sheet & rebuild it from Master
xlapp.Sheets("Detail").Select
xlapp.DisplayAlerts = False ' turn off message to OK delete sheet
xlapp.ActiveWindow.SelectedSheets.delete
xlapp.DisplayAlerts = True
xlapp.Sheets("Master").Select
xlapp.Sheets("Master").Copy after:=xlapp.Sheets("Master")
xlapp.Sheets("Master (2)").Select
xlapp.Sheets("Master (2)").name = "Detail"
xlapp.Sheets("Master").Select
xlapp.Rows("1:3").Select
xlapp.Selection.Copy
xlapp.Sheets("Detail").Select
xlapp.Rows("1:3").Select
xlapp.ActiveSheet.Paste
xlapp.Range("A1").Select

by amwalsh

Look for this file on your system: (default location) c:/Program Files/Microsoft Office/Office/VBAXL8.HLP. This is included with Office, but I think you need to do a custom install. Look for 'Visual Basic For Applications Help File' or something similar.

by tclere

This should get you started. Once you have the Excel Object, you can manipulate the workbook/worksheets like you do with VBA. You can record macros to get sample code that you need for specific tasks.

'Excel Objects
Public xlApp As Object
Public xlWB As Workbook
Public xlSht As Worksheet

'Initialize Excel Application
On Error Resume Next
'Try to get an existing Excel application
Set xlApp = GetObject, "Excel.Application")

If Err.Number <> 0 Then 'Excel was not running
Err.Clear
'Start a new Excel application
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo ErrHndlr

Workbooks.Open FileName:="C:\Test\TestWB.xls", ReadOnly:=False
Set xlWB = ActiveWorkbook

'Select the Sheet
Set xlSht = Worksheets("Sheet1")
xlSht.Select

'Change contents of cell
xlSht.Range("A1").FormulaR1C1="New Text"

Let me know if you need help with a specific task. There are so many things you can do, I am not sure what type of samples you need.

by rrv

Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Add
If xlBook.Sheets.Count = 0 Then
xlBook.Sheets.Add
End If

Select Case xlBook.Sheets.Count
Case 1
...
Case 2
Case 3
Case 4
Case 5
End Select

Application.MacroOptions Macro:=MacroName, _
HasShortcutKey:=True, ShortcutKey:="A"

With ActiveWorkbook.Names(1)
If .MacroType = xlFunction Then
call MacroName(parameters)
End If
End With

xlapp.quit savechanges:=true
set xlapp=nothing

This discussion is closed to new replies.