?
Solved

Controlling Excel in VB - close method

Posted on 2003-02-20
8
Medium Priority
?
150 Views
Last Modified: 2013-11-25
I am writing an application in visual basic and use an excel sheet to build up a preview to eventually print out. I've got a "create preview" button in my vb app to show the excel file generated with data from a flexgrid in my vb application. Everything works fine, it fits, it looks like i want it to look etc,  except for one thing. If i close the preview, and go back to my vb application and click the "create preview" button again, the generated excel file will open again BUT it won't show the data, instead it will show me the usual "File, Edit etc" menu but where the data should be, i see the background, behind that excel file, locked. Which is most of the time my own application. I debugged it and i don't get any errors and everything gets written to the cells, it just won't show it the second time. Yes, i've set the excel object to nothing at the end.

What i do notice though is when i close the excel object in my code (excelobject.close) after the preview has been generated and press the button again, it will show up as it's supposed to. Only one problem, closing it in the code will also close the excel window and i don't want that. I want the user to close it, and then still being able to click it again. I thought that when the user closed the excel window, it would equal the close method?

i've been trying a lot of things to get past that problem but in vain. Can someone help me?
0
Comment
Question by:ZeepEend
[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 Comments
 
LVL 11

Expert Comment

by:Steiner
ID: 7986272
Maybe setting all objects (e.g. the excel application, any workbooks...) to Nothing when your code is finished will help.

Greets
Steiner
0
 

Author Comment

by:ZeepEend
ID: 7986333
i've only got one excel object and reach each workbook (well only one) through it
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 7987368
Xcel.Application.Visible = True
Xcel.Parent.Windows(1).Visible = True

first line sets excel visible to the user.
second line shows the workbook or sheet.
are you try to transfer controll to the user?
are you using getobject or createobject (check for existing instance)?
0
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!

 

Author Comment

by:ZeepEend
ID: 7992241
The reason why it won't show up is because the excel object is somehow still in the memory when the user closes the excel window, unlike the close() method. But the close() method makes the excel window shut down, even when the user is still looking at it because vb keeps running in the background, it doesn't halt and wait for the excel window to close, like a messagebox, it just keeps on running the code. I tried putting the close() method before i'd set the excel object again, but then i get errors when the user has shut down the excel window manually.

What i do in the code is:

-Declare the excel object in option explicit:
  Dim Exob As Excel.Workbook

-I've already made a template excel file, stored on my harddisk, which i open in vb and fill up with data to show the user and print out. So next step is, i open the template file in a sub like this:
  Set Exob = Excel.Workbooks.Open(App.Path & "\perklant2.xls")

-After setting the excel object to the template file, i fill it up with data. After that's done, i show it to the user:
  Exob.Application.ActiveWorkbook.Saved = True
  Exob.Application.Visible = True
  Exob.Parent.Windows(1).Visible = True

-At the end of the sub, i set the excel object back to nothing:
  Set Exob = Nothing


Actually, the excel window is still open when the object gets set to nothing because of visual basic still running in the background. But whether i put it before the set or after the set in some sub right before the set sub, it doesn't matter. First time it will work perfectly, everything shows, but not the second time.

I've been thinking of some alternatives. Maybe the problem can be solved by creating my own preview form and open the excel file in there. That way the user cannot close the excel window itself and i can close it with the close() method. But how do i do this?
0
 

Author Comment

by:ZeepEend
ID: 7992267
The reason why it won't show up is because the excel object is somehow still in the memory when the user closes the excel window, unlike the close() method. But the close() method makes the excel window shut down, even when the user is still looking at it because vb keeps running in the background, it doesn't halt and wait for the excel window to close, like a messagebox, it just keeps on running the code. I tried putting the close() method before i'd set the excel object again, but then i get errors when the user has shut down the excel window manually.

What i do in the code is:

-Declare the excel object in option explicit:
  Dim Exob As Excel.Workbook

-I've already made a template excel file, stored on my harddisk, which i open in vb and fill up with data to show the user and print out. So next step is, i open the template file in a sub like this:
  Set Exob = Excel.Workbooks.Open(App.Path & "\perklant2.xls")

-After setting the excel object to the template file, i fill it up with data. After that's done, i show it to the user:
  Exob.Application.ActiveWorkbook.Saved = True
  Exob.Application.Visible = True
  Exob.Parent.Windows(1).Visible = True

-At the end of the sub, i set the excel object back to nothing:
  Set Exob = Nothing


Actually, the excel window is still open when the object gets set to nothing because of visual basic still running in the background. But whether i put it before the set or after the set in some sub right before the set sub, it doesn't matter. First time it will work perfectly, everything shows, but not the second time.

I've been thinking of some alternatives. Maybe the problem can be solved by creating my own preview form and open the excel file in there. That way the user cannot close the excel window itself and i can close it with the close() method. But how do i do this?
0
 

Accepted Solution

by:
ZeepEend earned 0 total points
ID: 7992845
Nevermind, my colleague solved the problem. Thanks for the suggestions.

Apparently i needed two excel objects, an application object and a workbook object. Setting those objects to nothing at the end seemed to work.


0
 

Expert Comment

by:CleanupPing
ID: 8901393
ZeepEend:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8963880
ZeepEend, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Refund points and save as a 0-pt PAQ.

DanRollins -- EE database cleanup volunteer
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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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