Solved

Workbook not visible...

Posted on 2004-09-10
16
509 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
  • 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 33

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
 
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 350 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 33

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 33

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Assisted Solution

by:ameba
ameba earned 150 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 33

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 33

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 33

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 33

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 33

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now