?
Solved

Workbook not visible...

Posted on 2004-09-10
16
Medium Priority
?
543 Views
Last Modified: 2012-06-27
After I quit a vb 6 form, and click on a xl file to open it, excel object (menu, border, etc.) show up; but the workbook itself is not visible (it is transparent, you can still see the desktop thrugh border where the workbook supposed to show).

I want to include a code to in QueryUnload of vb form to handle this situation (to close all excel applicatiion hidden or visible).  Now I am trying to use:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As Long) As Long

Public Sub RemoveAllExcelFiles()

On Error Resume Next
While CBool(FindWindow("XLMAIN",0))               \
Set m_app = GetObject(,"Ecel.Application")          |
'If m_app.visible=False Then                               |
    m_app.Quit                                                    >  When I remove 'exit sub line, it will loop While CBool...Wend
'End If                                                              |    forever, if I include this line...  this is the cause of the proplem above
'exit sub                                                           |    (maybe?)
Wend                                                              /

End Sub
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
  • 8
  • 4
  • 4
16 Comments
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12029735
Oh well, you seem to be going the right way, only to miss it slightly.

As you loop thru the Excel windows, before performning any operation on the handle DO validate it. If NOT m_app is Nothing.

Further, do close all the documents it has open, either by discarding the changes or saving it.

You may loop thru teh collection of all the open documents by looping thru the Excel application object's workbooks collection.
0
 
LVL 15

Expert Comment

by:ameba
ID: 12029798
If it didn't close, there must be some object alive.  All references must be removed (set to Nothing), i.e. REFCOUNT must be 0.
Check your cleanup code.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12030444
Hi RanjeetRain,

Re:> You may loop thru teh collection of all the open documents by looping thru the Excel application object's workbooks collection.

I thoght this is what I was trying to do in the code above.  If not, what is the code (fyi, I am new to vb).

Re: If NOT m_app is Nothing

While CBool(FindWindow("XLMAIN",0))              
Set m_app = GetObject(,"Ecel.Application")        
If NOT m_app is Nothing then Exit Sub                 '<--  Is this what you mean?                        
'If m_app.visible=False Then                              
    m_app.Quit                                                    
'End If                                                                  
'exit sub                                                              
Wend        

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 19

Expert Comment

by:RanjeetRain
ID: 12030496
NO. You are not looping thru the open documents collection. YOu are just looping thru all the open Excel windows. For maximum safety, you MUST close all the documents in all the open windows. Additional care should be taken that you actually purge the object by explicitly setting them to NOTHING.
0
 
LVL 19

Accepted Solution

by:
RanjeetRain earned 1400 total points
ID: 12030536
Public Sub RemoveAllExcelFiles()

On Error Resume Next
While CBool(FindWindow("XLMAIN", 0))
    Set m_app = GetObject(, "Excel.Application")
    If Not m_app Is Nothing Then
        ' loop thru the workbooks collection  here and close all workbooks
        m_app.Quit
    End If
Wend

End Sub
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12030537
Hi ameba,

How one would know when REFCOUNT is zero?  Via some code perhapas on query unload.  

Also, I would set all to nothing in query unload (as RanjeetRain is also pointing it out).

Thanks,

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12030659
Thanks RanjeetRain.  I will try it now.  

Do you think this will resolve never-ending-loop from 'While CBool...  to Wend' problem as well?

brb
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12030727
It shoud, becasue you are removing the reference. However, you may try putting some dummy loops to give the OS the time to actually close Excel.


Public Sub RemoveAllExcelFiles()

On Error Resume Next
While CBool(FindWindow("XLMAIN", 0))
   Set m_app = GetObject(, "Excel.Application")
   If Not m_app Is Nothing Then
       ' loop thru the workbooks collection  here and close all workbooks
       sleep(1)
       m_app.Quit
   End If
Wend

End Sub


public sub Sleep(byval duration as integer)
    dim x as integer
    for x = 1 to duration * 10000
         doevents
    next x
end sub

0
 
LVL 15

Assisted Solution

by:ameba
ameba earned 600 total points
ID: 12031726
eghtebas,
> How one would know when REFCOUNT is zero?
That info is stored in each COM object. It is possible to access it, but that is not the way to do the cleanup.

This seems to work for me, no need for FindFindow. Also, before Quit, Excel is made visible - that might help (e.g. if there is some OLE Busy dialog):

Private Sub CloseAllExcels()
    Dim oExcel As Excel.Application
    Do
        On Error Resume Next
        Set oExcel = GetObject(, "Excel.Application")
        On Error GoTo 0
        If oExcel Is Nothing Then Exit Sub   ' all done
       
        oExcel.Visible = True
        oExcel.DisplayAlerts = False
        oExcel.Quit
        Set oExcel = Nothing
    Loop
End Sub
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12033256
RanjeetRain,
Your code works and there is no need for Sleep() routine.

ameba,
I am greatful for your good code.

Regards,

Mike



0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12035158
It din't work as I thought it would.  Please see follow-up question at:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21127436.html

Mike
0
 
LVL 15

Expert Comment

by:ameba
ID: 12035320
After m_app.Visible add: m_app.ScreenUpdating = True

or: oExcel.ScreenUpdating = True, if you use code without FindWindow.

Is there any dialog showing?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12035326
Would you kindly add a comment to the link as I am trying your code?

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12035585
Re:>Is there any dialog showing?  No.

It seems the problem was I had

xlObj.visible=true  two times in a code (one extra one).  This seems was what was causing the problem.  

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12035736
ameba,

Thank you for your extra help.  Because you didn't want to post on the link and I had the final solution, I deleted the question.

Regards,

Mike
0
 
LVL 15

Expert Comment

by:ameba
ID: 12036430
Hi, sorry, was watching T.^H^H^H^H^H  having interesting evening ;-)
I'm glad you solved it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

777 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