Cell Value updates / spreadsheet save issue

astrogal
10-09-2001, 09:54 AM
I have a tool that opens an Excel template, reads data from a text file and places the parsed data into pertinent cells in the spreadsheet. The spreadsheet template has formulas which uses the data that is input. I then save the spreadsheet and reopen to ensure the formulas are calulated from the new data. The data in the cells that have the formulas however do not seemed to be refreshed on the save when an instance of Excel is not already open. If an instance of Excel is already open the tool works fine. The tool does however have code to open Excel so I would think it should work. Here's part of it. Help would really be appreciated!

Set xlApp = New Excel.Application 'open Excel
xlApp.Visible = True
Set xlBook1 = GetObject(ttq_template$) 'open the appropriate workbook where ttq_template$ is the path string
xlBook1.Parent.Windows(xlBook1.Name).Visible = True
Set xlSheet1 = xlBook1.Worksheets(1) 'open first sheet of workbook

Code does some stuff, like parsing data file and placing in certain spreadsheet cells....

'The save part...
xlSheet1.SaveAs "apppathonmysystem\" + cmdout_ttq$ 'saving the spreadsheet as the outputname

'The Close part...
'Closing book to refresh data
If xlBook1.Name <> "" Then
xlBook1.Close
End If
Set xlBook1 = Nothing
Set xlSheet1 = Nothing
If xlApp Is Excel.Application Then
xlApp.Quit
End If
Set xlApp = Nothing

'The reopen part...
Set xlApp = New Excel.Application 'open Excel
xlApp.Visible = True
Set xlBook1 = GetObject("apppathonmysystem\" + cmdout_ttq$ + ".xls") 'open the appropriate workbook
xlBook1.Parent.Windows(xlBook1.Name).Visible = True
Set xlSheet1 = xlBook1.Worksheets(1) 'open first sheet of workbook

When I step through and Excel is open on my system (could be just application, no workbook Or w/blank workbook Or existing non-related workbook, it doesn't matter) this works fine. If there isn't an instance those cells have #Value as if the cells with the formulas weren't refreshed.

Thanks!

Timbo
10-09-2001, 07:38 PM
don't bother with all that saving/opening stuff, just force the app to recalculate (or each sheet containing formulae) before the save..

'xlApp.Calculate'
or
'xlSheet1.Calculate'


"He who dares my son!.. He who dares.." - Del-Boy Trotter

astrogal
10-10-2001, 06:09 AM
Thanks Timbo,

This did simplify my code quite a bit but did not solve the problem I am experiencing. I must have identified the problem incorrectly. I'll have to hone in on the problem and post a new question.

Thanks again!

astrogal
10-10-2001, 09:28 AM
Ok, here's more detail on the problem I've been having. The main issue I'm having is that since my code opens Excel and the appropraite workbook, etc then the tool should work when an existing instance of Excel is NOT already open, right? I've taken Timbo's last suggestion of doing the xlBook1.Calculate to refresh the data (does save me some extra process time and lines of code, thanks) but the problem seems to lie earlier on in the opening of the App.
Has anyone seen this problem before?

Thanks.

Timbo
10-10-2001, 06:48 PM
so the .Calculate method does not affect the formuae?

Did you try the 'Sheet1.Calculate' (ie. sheet specific) method - I would have thought this would work even without initialising the Excel Application object...
you can use the 'GetObject()' method without it and should still be able to reference all the objects and methods of the file.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum