Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GetObject or CreateObject... vb6

Posted on 2004-09-15
7
Medium Priority
?
14,103 Views
Last Modified: 2008-01-09
From vb form, I am selecting a workbook which it may or may not already open.  

Public Sub OpenExcelFile(strPath As String)

Dim xlWb As Excel.Workbook

If <file is already open?> Then                    ' What logic can I use here, or I have use error number to handle it?
   Dim xlObj As Excel.Application
   Set xlObj = GetObject(,"Excel.Application")
Else
   Dim xlObj As New Excel.Application
   Set xlObj = CreateObject(,"Excel.Application")
End if

Set xlWb = xlObj.OpenWorkBook(strPath)

Set xlObj = Nothing
Set xlWb = Nothing

Please QC above code also.

Thanks,
0
Comment
Question by:Mike Eghtebas
  • 4
  • 2
7 Comments
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 12072096
Try something like:

...
Dim xlObj As Excel.Application
Dim xWb As Excel.WorkBook
Dim xtmpWb As Excel.WorkBook
Dim isFound as boolean

on error resume next
   Set xlObj = GetObject(,"Excel.Application")

if xlObj is nothing then Set xlObj = CreateObject(,"Excel.Application")

isFound  = false
For each xtmpWb in xlObj.workbooks
   if xtmpWb.FullName = strPath then
          Set xWb = xtmpWb
          isFound = true
          exit for
   end if
Next

if isfound = false then Set xlWb = xlObj.OpenWorkBook(strPath)

on error goto 0
...

?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12072111
If there is no excel open, it won't go beyond:

Set xlObj = GetObject(,"Excel.Application")

I think. brb
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12072391
Now, I can see your logic regarding Set xlObj = GetObject(,"Excel.Application").

Btw, what happens if we had two workbooks with the same name but in different folder?  How following code would know what 'FullName' to consider:

if xtmpWb.FullName = strPath then

Mike
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 54

Expert Comment

by:Ryan Chong
ID: 12072519
i think the xtmpWb.FullName returns the full path of the workbook, isn't it?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12072577
I used:

if frmMain.cboFolder & "\" & xtmpWb.Name = strPath then

Because there maybe to identical xl file present in two different folders; therefore, xtmpWb.FullName cann't tell what xtmpWb we intend to open.

You were great as always.

Regards,

Mike
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 12072600
If GetObject returns an Excel instance then rather than loop through all open workbooks you could attempt to set xWb to yourbook name - if an error occurs then the file is closed

Dim xlObj      As Excel.Application
Dim xWb        As Excel.Workbook
Dim xtmpWb     As Excel.Workbook
Dim isFound    As Boolean

    On Error Resume Next
    Set xlObj = GetObject(, "Excel.Application")

    If xlObj Is Nothing Then
        Set xlObj = CreateObject("Excel.Application")
    Else
        On Error Resume Next
        Set xtmpWb = xlObj.Workbooks("aa.xls")
        If Err.Number <> 0 Then Set xlWb = xlObj.Workbooks.Open("C:\test\aa.xls")
        On Error GoTo 0
    End If

Cheers

Dave
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12085718
Dave,

Thank you for the code you provided.  As you know, your post was after the question has been closed -- to late to split.  

Although I am using the solution from ryancys, but I appreciate having your code.

Regards,

Mike
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question