Excel SaveAs method hangs process on some machines

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have an unusual problem in that I have four machines and my code works fine for two machines but not for the other two.  The code does work on a machine with Vista(32bit)/Office 2007 and a machine with XP/Office 2003.  It does not work on a machine with Vista(32bit)/Office 2003 and a machine with XP/Office 2003.  The problem is with all machines I can open, save, edit, and close Excel.  However, on the two machines with the problem, if I save(or saveas) the workbook and close the program, the Excel process wont close in the Task Manager.<br/> <br/> Im using VS2005 and all machines have .Net 3.5 SP1.  For Office 2007 I referenced the 2007PIAs and for Office 2003 I referenced the 2003PIAs.  To simplify the problem, I made a simple program (found below) and reproduced the same results.  Are there any references or something else Im missing?  Any thoughts?<br/>
<pre lang="x-c# using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelAutomation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Excel.Application objApp;
Excel.Workbooks objBooks;
Excel._Workbook objBook;

private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnStop.Enabled = true;

DialogResult saveResult = sfdExit.ShowDialog();

//exit if user cancels
if (saveResult == DialogResult.Cancel)
return;

try
{
objBook.SaveAs(sfdExit.FileName, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
catch
{
MessageBox.Show("SaveAs Problem");
}
}

private void btnStop_Click(object sender, EventArgs e)
{
btnStop.Enabled = false;
btnStart.Enabled = true;
}



private void Form1_Load(object sender, EventArgs e)
{
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Missing.Value);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
objBook.Close(false, Missing.Value, Missing.Value);
objApp.Quit();

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

Marshal.FinalReleaseComObject(objBook);
objBook = null;
Marshal.FinalReleaseComObject(objBooks);
objBooks = null;
Marshal.FinalReleaseComObject(objApp);
objApp = null;

GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}[/code]
Thanks for any suggestions or help.

View the full article
 
Back
Top