2

in my project I need to save user customized excel to .mht file and immediately reload the file into memory, code is following

var app = new Excel.Application();   
var wsCurrent = app.ActiveWorkbook.ActiveSheet;
object format = Excel.XlFileFormat.xlWebArchive;
var codename = application.ActiveSheet.CodeName; 

wsCurrent.SaveAs(outputFile, format, Type.Missing, Type.Missing, Type.Missing, 
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

however, I found that due to the natural of excel, ince we save the file, it will lock the process until the excel application closed. in other word, I could not load the saved .mht file unless I close excel which I do not want to, any one better idea to do this and please give me a clue to the solution, many thanks in advance, cheers,

cuongle
  • 74,024
  • 28
  • 151
  • 206
Jian_H
  • 173
  • 1
  • 4
  • 18

1 Answers1

4

You need to create a workbook variable equal to Application.Activeworkbook, save the activesheet of this object and then call close on the workbook, Excel can remain open, like so:

var app = new Excel.Application();
Excel.Workbook workbook = app.ActiveWorkbook;
Excel.Worksheet wsCurrent = workbook.ActiveSheet;
Excel.XlFileFormat format = Excel.XlFileFormat.xlWebArchive;

wsCurrent.SaveAs(@"C:\test\test.xlsx", format);

workbook.Close();
JMK
  • 27,273
  • 52
  • 163
  • 280
  • 1
    Thanks JMK it does works, however, due to I don't want close orginal instance of worksheet so, application.Worksheets.Copy(Type.Missing, Type.Missing); will give me the answer – Jian_H Oct 03 '12 at 01:43