[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Workbook not visible...

Posted on 2004-09-10
16
Medium Priority
?
553 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
Technology Partners: 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!

 
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

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.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 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