Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetObject or CreateObject... vb6

Posted on 2004-09-15
7
Medium Priority
?
14,009 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 53

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

705 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