Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Workbook not visible...

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
Avatar of RanjeetRain
RanjeetRain

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.
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.
Avatar of Mike Eghtebas

ASKER

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        

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.
ASKER CERTIFIED SOLUTION
Avatar of RanjeetRain
RanjeetRain

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RanjeetRain,
Your code works and there is no need for Sleep() routine.

ameba,
I am greatful for your good code.

Regards,

Mike



It din't work as I thought it would.  Please see follow-up question at:

https://www.experts-exchange.com/questions/21127436/Workbook-not-visible-folllow-up.html

Mike
After m_app.Visible add: m_app.ScreenUpdating = True

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

Is there any dialog showing?
Would you kindly add a comment to the link as I am trying your code?

Mike
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
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
Hi, sorry, was watching T.^H^H^H^H^H  having interesting evening ;-)
I'm glad you solved it.