R
Rick
Can someone help me write better code to use Excel
programming, from VB6? See the code below. I'm able to
open up an Excel workbook, and perform the task
repeatedly. However, if I leave that particular Excel
workbook open, I get multiple instances of Excel. In
other words, it keep opening up more read-only Excel
workbooks of the same file name and path.
I have been reading up a lot about it, and some of it is
starting to slowly make sense. Apparently we don't have
to CreateObject if it is early binding - where we pick the
Excel application from the reference library. Also with
early binding, then in Excel 2000 or later, we can write
either:
Set m_XLApp = New Excel.Application or
Set m_XLApp = GetObject(,"Excel.Application")
And they do the same thing. The advantage of GetObject is
that you have two parameters, in front of and behind the
comma.
That helps me understand CreateObject and GetObject
somewhat. Am I correct with this? We read about this
from programmers, and we need English/VBprogrammer
interpreters for those of us that only speak English.
<grin>. It's so confusing when the language or rules
change with each new version of Excel or VB.
With the code below, I hope I got everything written to
run a very simple program for testing purposes only. The
program multiples one number on one sheet times 2 and
makes it equal to another location on another sheet. Then
I have the Excel workbook file name being written on
another cell. Finally, I have a treeview control on a
form, named trvNo1. This treeview has one thing added to
it, based on what it reads in Excel. That's it - just a
couple of simple procedures to test this.
If you could copy and paste the code in VB6 (for the cmdOK
button on the VB6 form), I'd sure appreciate it. Also on
this form you'd have a treeview control (trvNo1). Then
you could use an Excel sample with made-up numbers and
text (from A drive or C drive or whatever).
If you could get this to run without multiple instances of
the same Excel workbook being pulled up, you would make
one part-time programmer extremely happy. I've been
trying so many different things with no result. Thanks.
Also anyone other comments to make this work better, it
would be greatly appreciated. I'm still learning a lot
about this, from a VB6 perspective. Thanks again.
If Bob Phillips is out there and reads this, I want to
thank you for helping me get this far. I appreciate the
tips that you've given me in the past. Bob Kilmer has
been helpful too.
Option Explicit
Dim m_XLApp As Application
Dim m_XLWorkbook As Excel.Workbook
'---------------------------------------------------
Private Sub cmdOK_Click()
Dim nodX As Node
Dim FileNamePath As String, FileName As String
Dim S1 As Worksheet, S2 As Worksheet
On Error Resume Next
FileName = "SampleProgram"
FileNamePath = "A:\SampleProgram.xls"
Set m_XLApp = GetObject(, "Excel.Application")
m_XLApp.Visible = True
Set m_XLWorkbook = m_XLApp.Workbooks(FileName)
If m_XLWorkbook Is Nothing Then
Set m_XLApp = Excel.Application
m_XLApp.Visible = True
Set m_XLWorkbook = m_XLApp.Workbooks.Open(FileName:= _
FileNamePath)
m_XLWorkbook.RunAutoMacros Which:=xlAutoOpen
Else
Set m_XLWorkbook = m_XLApp.Workbooks(FileName)
End If
'************************
'* Run Program *
'************************
Set S1 = m_XLWorkbook.Sheets(1)
Set S2 = m_XLWorkbook.Sheets(2)
S1.Select
Range("A1").Select
S1.Cells(5).Value = S2.Cells(1).Value * 2
S1.Cells(6).Value = FileName
Set nodX = trvNo1.Nodes.Add(, , "R1", S1.Cells(5,
4).Value)
nodX.EnsureVisible
Set m_XLApp = Nothing
End Sub
programming, from VB6? See the code below. I'm able to
open up an Excel workbook, and perform the task
repeatedly. However, if I leave that particular Excel
workbook open, I get multiple instances of Excel. In
other words, it keep opening up more read-only Excel
workbooks of the same file name and path.
I have been reading up a lot about it, and some of it is
starting to slowly make sense. Apparently we don't have
to CreateObject if it is early binding - where we pick the
Excel application from the reference library. Also with
early binding, then in Excel 2000 or later, we can write
either:
Set m_XLApp = New Excel.Application or
Set m_XLApp = GetObject(,"Excel.Application")
And they do the same thing. The advantage of GetObject is
that you have two parameters, in front of and behind the
comma.
That helps me understand CreateObject and GetObject
somewhat. Am I correct with this? We read about this
from programmers, and we need English/VBprogrammer
interpreters for those of us that only speak English.
<grin>. It's so confusing when the language or rules
change with each new version of Excel or VB.
With the code below, I hope I got everything written to
run a very simple program for testing purposes only. The
program multiples one number on one sheet times 2 and
makes it equal to another location on another sheet. Then
I have the Excel workbook file name being written on
another cell. Finally, I have a treeview control on a
form, named trvNo1. This treeview has one thing added to
it, based on what it reads in Excel. That's it - just a
couple of simple procedures to test this.
If you could copy and paste the code in VB6 (for the cmdOK
button on the VB6 form), I'd sure appreciate it. Also on
this form you'd have a treeview control (trvNo1). Then
you could use an Excel sample with made-up numbers and
text (from A drive or C drive or whatever).
If you could get this to run without multiple instances of
the same Excel workbook being pulled up, you would make
one part-time programmer extremely happy. I've been
trying so many different things with no result. Thanks.
Also anyone other comments to make this work better, it
would be greatly appreciated. I'm still learning a lot
about this, from a VB6 perspective. Thanks again.
If Bob Phillips is out there and reads this, I want to
thank you for helping me get this far. I appreciate the
tips that you've given me in the past. Bob Kilmer has
been helpful too.
Option Explicit
Dim m_XLApp As Application
Dim m_XLWorkbook As Excel.Workbook
'---------------------------------------------------
Private Sub cmdOK_Click()
Dim nodX As Node
Dim FileNamePath As String, FileName As String
Dim S1 As Worksheet, S2 As Worksheet
On Error Resume Next
FileName = "SampleProgram"
FileNamePath = "A:\SampleProgram.xls"
Set m_XLApp = GetObject(, "Excel.Application")
m_XLApp.Visible = True
Set m_XLWorkbook = m_XLApp.Workbooks(FileName)
If m_XLWorkbook Is Nothing Then
Set m_XLApp = Excel.Application
m_XLApp.Visible = True
Set m_XLWorkbook = m_XLApp.Workbooks.Open(FileName:= _
FileNamePath)
m_XLWorkbook.RunAutoMacros Which:=xlAutoOpen
Else
Set m_XLWorkbook = m_XLApp.Workbooks(FileName)
End If
'************************
'* Run Program *
'************************
Set S1 = m_XLWorkbook.Sheets(1)
Set S2 = m_XLWorkbook.Sheets(2)
S1.Select
Range("A1").Select
S1.Cells(5).Value = S2.Cells(1).Value * 2
S1.Cells(6).Value = FileName
Set nodX = trvNo1.Nodes.Add(, , "R1", S1.Cells(5,
4).Value)
nodX.EnsureVisible
Set m_XLApp = Nothing
End Sub