LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Save Method Of Workbook Class Failed

Hi -

I know this is an Excel VBA forum but thought I give the it a try with a C#
program that automates Excel 2007 using VSTO (VS 2008).

I have attached the code below ... the error happens at "printWB.Save();"

I am getting a the information message from Excel of " 'workbookname.xlsx'
in use. Try again later." followed by the exception error "Save Method of
Workbook Class Failed". The messages come intermittantly and therefore am
having a time of it understanding when it happens no less why it happens.

The idea is for the program to run through a list of contracts, populate an
excel sheet with data from a OLAP data cube (server based) for each contract,
make a copy of the excel sheet and save it to a second worbook (values only).
The second workbook is saved after every 25 contracts inorder to reduce
computer resources as the program runs.

Any suggestions are welcome.

Thank you Mark

private void PrintSelectedContract1PerPage()
{

Object oMissing = System.Reflection.Missing.Value;

//Set Print Version
string printVersion = "1 per page";

//Setup Workbook that holds printed pages
printWB =
Globals.ThisWorkbook.Application.Workbooks.Add(oMi ssing);

Excel.Sheets sheets = printWB.Worksheets;
Excel.Worksheet printSheet =
(Excel.Worksheet)printWB.Worksheets[1];
printSheet.Name = reportName;


//Get File name and path
string xlsPath = @"C:\SaveToFile\myfile.xlsx";

//Save workbook
printWB.SaveAs(xlsPath, oMissing, oMissing, oMissing, oMissing,
oMissing, Excel.XlSaveAsAccessMode.xlNoChange, oMissing, oMissing, oMissing,
oMissing, oMissing);

//Set worksheet and page counters
int n = 0; //The total number of contracts
int p = 0; //The total number of contracts including ceded and
net contracts
int rowCnt = 36;
int scale = 52;

//Get first contract
Globals.Sheet2.tblUserInterface_MasterBindingSourc e.MoveFirst();

n++;

do
{

//Tell user which contract its on
Globals.ThisWorkbook.Application.StatusBar = n.ToString() +
" of " +
Globals.Sheet2.tblUserInterface_MasterBindingSourc e.Count.ToString();

//Print Contract to Worksheet
p++;
PrintContractToWorksheet2(printWB, p, rowCnt, printVersion);



//In order to improve performance we save the worksheet and
run the Garbage Collector every 25 contracts
if (n % 25 == 0 || n ==
Globals.Sheet2.tblUserInterface_MasterBindingSourc e.Count)
{

WorkingForm();

printWB.Save(); // = The ERROR MESSAGE OCCURS HERE <=

printWB.Close(oMissing, oMissing, oMissing);


System.Runtime.InteropServices.Marshal.ReleaseComO bject(printWB);

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

printWB = null;

printWB =
Globals.ThisWorkbook.Application.Workbooks.Open(xl sPath, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing);

}

//Move to next contract and increment

Globals.Sheet2.tblUserInterface_MasterBindingSourc e.MoveNext();
n++;


}
while (n <=
Globals.Sheet2.tblUserInterface_MasterBindingSourc e.Count);

//Save the print worksheet
printWB.Save();
printWB.Close(oMissing, oMissing, oMissing);

//Clear Status Bar
Globals.ThisWorkbook.Application.StatusBar = null;

//Clean up references and run Garbage Collectopn

System.Runtime.InteropServices.Marshal.ReleaseComO bject(printWB);

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

printWB = null;

}

private static void
PrintContractToWorksheet2(Microsoft.Office.Interop .Excel.Workbook printWB,
int p, int rowCnt, string printVersion)
{

Object oMissing = System.Reflection.Missing.Value;

WorkingForm();

Excel.Range printRange = null;

if (p == 1)
{
if (printVersion == "1 per page")
Globals.Sheet1.Sheet1_HeaderArea.Copy(oMissing);
else
Globals.Sheet12.Sheet12_HeaderArea.Copy(oMissing);

printRange =
(Excel.Range)((Excel.Worksheet)printWB.Worksheets[1]).Cells[1, 1];

printRange.PasteSpecial(Microsoft.Office.Interop.E xcel.XlPasteType.xlPasteColumnWidths,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, oMissing, oMissing);

printRange.PasteSpecial(Microsoft.Office.Interop.E xcel.XlPasteType.xlPasteFormats,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, oMissing, oMissing);

printRange.PasteSpecial(Microsoft.Office.Interop.E xcel.XlPasteType.xlPasteValues,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, oMissing, oMissing);

}

//Copy the results from the template
if (printVersion == "1 per page")
Globals.Sheet1.Sheet1_ReportArea.Copy(oMissing);
else
Globals.Sheet12.Sheet12_ReportArea.Copy(oMissing);

//Determine the cell that we are pasting the results to
printRange =
(Excel.Range)((Excel.Worksheet)printWB.Worksheets[1]).Cells[(p - 1) * rowCnt
+ 4, 1];

//Paste results then paste values the results

printRange.PasteSpecial(Microsoft.Office.Interop.E xcel.XlPasteType.xlPasteFormats,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, oMissing, oMissing);

printRange.PasteSpecial(Microsoft.Office.Interop.E xcel.XlPasteType.xlPasteValues,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, oMissing, oMissing);

//Clear Clipboard
Clipboard.Clear(); //Not sure this does anything
}

//Method is used to refresh the data linked to the data cube.
// In theory the CalculateUntilAsyncQueriesDone should work alone
but does not and is prone to hang
// Showing the form "frmWorkingForm" with the sleep thread method
afferted the hanging.
// I stumpled onto this workaround by wanting to have the
computer "do something else" until the Aysnc was doen; but I really don't
know why it works.
private static void WorkingForm()
{


Globals.ThisWorkbook.Application.CalculateUntilAsy ncQueriesDone();
frmWorkingForm workingForm = new frmWorkingForm();
workingForm.Show();

System.Threading.Thread.Sleep(0);

workingForm.Close();
}






 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add method of Workbooks class failed Bill Baker[_2_] Excel Programming 0 November 6th 06 04:33 PM
Paste method of worksheet class failed Maxi[_2_] Excel Programming 2 March 8th 06 04:30 PM
Select method of range class failed sa02000[_4_] Excel Programming 1 October 5th 05 01:20 PM
Copy Method of Worksheet Class Failed Steph[_3_] Excel Programming 2 April 7th 04 01:43 PM
Error Excel: 1004 SaveAs method of Workbook class failed sebastien Excel Programming 0 August 13th 03 09:59 AM


All times are GMT +1. The time now is 10:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"