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
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RanjeetRainConnect With a Mentor Commented:
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
 
RanjeetRainCommented:
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
 
amebaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
RanjeetRainCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
RanjeetRainCommented:
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
 
amebaConnect With a Mentor Commented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
RanjeetRain,
Your code works and there is no need for Sleep() routine.

ameba,
I am greatful for your good code.

Regards,

Mike



0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
amebaCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Would you kindly add a comment to the link as I am trying your code?

Mike
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
amebaCommented:
Hi, sorry, was watching T.^H^H^H^H^H  having interesting evening ;-)
I'm glad you solved it.
0
All Courses

From novice to tech pro — start learning today.