PC Review


Reply
Thread Tools Rate Thread

closing excel file through code

 
 
Namrata
Guest
Posts: n/a
 
      6th Dec 2007
Hi,



I am using Excel for reporting. For opening excel file I have write code :



exlApp = null;

exlApp = new ApplicationClass();

//try

//{

object missing = System.Reflection.Missing.Value;

string path = System.IO.Path.GetFullPath(@"Templates\Log.csv").ToString();

excelWorkbook = exlApp.Workbooks.Open(path, missing,

1,

missing,

Excel.XlTextQualifier.xlTextQualifierNone,

missing,

missing,

missing,

true, //COMMA

missing,

missing,

missing,

missing

);

path = "";

path = System.IO.Path.GetFullPath(@"Reports").ToString();

path = path + @"\" + frmSiteName.siteName + " GenSumm " + DateTime.Now.Day +
"_" + DateTime.Now.Month + "_" + DateTime.Now.Year + " " + DateTime.Now.Hour
+ "." + DateTime.Now.Minute + ".csv";

excelWorkbook.SaveAs(path, missing, missing, missing, missing, missing,
XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);

object pwd = "cgclogic";

excelWorkbook.Unprotect(pwd);

Excel.Sheets excelSheets = excelWorkbook.Worksheets;

excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("Log Report");

excelWorksheet.Unprotect(pwd);







And before that form termination, write code :

excelWorkbook.Save();

exlApp.Workbooks.Close();


exlApp.Quit();

exlApp = null;

excelWorksheet = null;

excelWorkbook = null;



This form is opening through MDI.



But even after form close 'Windows Task Manager' is showing 'Excel' in
process.
And after application colse it is removing from task manager.

When that file in the reports folder try to open, it is giving error : "
This file is already opened . DO you want to reopen the file ?" or " This
file is being modified by other user . Open a read-only copy."



please tell how to close that file using code.

 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      6th Dec 2007
Only a guess but try changing

> exlApp.Quit();
>
> exlApp = null;
>
> excelWorksheet = null;
>
> excelWorkbook = null;
>


to -

excelWorksheet = null;

ensure all unsaved wb's are closed, after saving or with close false
excelWorkbook = null;
exlApp.Quit();
exlApp = null;

Regards,
Peter T


"Namrata" <(E-Mail Removed)> wrote in message
news:797B4E2F-1BF0-4FA9-990B-(E-Mail Removed)...
> Hi,
>
>
>
> I am using Excel for reporting. For opening excel file I have write code :
>
>
>
> exlApp = null;
>
> exlApp = new ApplicationClass();
>
> //try
>
> //{
>
> object missing = System.Reflection.Missing.Value;
>
> string path = System.IO.Path.GetFullPath(@"Templates\Log.csv").ToString();
>
> excelWorkbook = exlApp.Workbooks.Open(path, missing,
>
> 1,
>
> missing,
>
> Excel.XlTextQualifier.xlTextQualifierNone,
>
> missing,
>
> missing,
>
> missing,
>
> true, //COMMA
>
> missing,
>
> missing,
>
> missing,
>
> missing
>
> );
>
> path = "";
>
> path = System.IO.Path.GetFullPath(@"Reports").ToString();
>
> path = path + @"\" + frmSiteName.siteName + " GenSumm " + DateTime.Now.Day

+
> "_" + DateTime.Now.Month + "_" + DateTime.Now.Year + " " +

DateTime.Now.Hour
> + "." + DateTime.Now.Minute + ".csv";
>
> excelWorkbook.SaveAs(path, missing, missing, missing, missing, missing,
> XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);
>
> object pwd = "cgclogic";
>
> excelWorkbook.Unprotect(pwd);
>
> Excel.Sheets excelSheets = excelWorkbook.Worksheets;
>
> excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("Log Report");
>
> excelWorksheet.Unprotect(pwd);
>
>
>
>
>
>
>
> And before that form termination, write code :
>
> excelWorkbook.Save();
>
> exlApp.Workbooks.Close();
>
>
> exlApp.Quit();
>
> exlApp = null;
>
> excelWorksheet = null;
>
> excelWorkbook = null;
>
>
>
> This form is opening through MDI.
>
>
>
> But even after form close 'Windows Task Manager' is showing 'Excel' in
> process.
> And after application colse it is removing from task manager.
>
> When that file in the reports folder try to open, it is giving error : "
> This file is already opened . DO you want to reopen the file ?" or " This
> file is being modified by other user . Open a read-only copy."
>
>
>
> please tell how to close that file using code.
>



 
Reply With Quote
 
Namrata
Guest
Posts: n/a
 
      6th Dec 2007
Thanks for reply !
I change the code as:
> excelWorksheet = null;
>
> ensure all unsaved wb's are closed, after saving or with close false
> excelWorkbook = null;
> exlApp.Quit();
> exlApp = null;


But excel file still remains open.

"Peter T" wrote:

> Only a guess but try changing
>
> > exlApp.Quit();
> >
> > exlApp = null;
> >
> > excelWorksheet = null;
> >
> > excelWorkbook = null;
> >

>
> to -
>
> excelWorksheet = null;
>
> ensure all unsaved wb's are closed, after saving or with close false
> excelWorkbook = null;
> exlApp.Quit();
> exlApp = null;
>
> Regards,
> Peter T
>
>
> "Namrata" <(E-Mail Removed)> wrote in message
> news:797B4E2F-1BF0-4FA9-990B-(E-Mail Removed)...
> > Hi,
> >
> >
> >
> > I am using Excel for reporting. For opening excel file I have write code :
> >
> >
> >
> > exlApp = null;
> >
> > exlApp = new ApplicationClass();
> >
> > //try
> >
> > //{
> >
> > object missing = System.Reflection.Missing.Value;
> >
> > string path = System.IO.Path.GetFullPath(@"Templates\Log.csv").ToString();
> >
> > excelWorkbook = exlApp.Workbooks.Open(path, missing,
> >
> > 1,
> >
> > missing,
> >
> > Excel.XlTextQualifier.xlTextQualifierNone,
> >
> > missing,
> >
> > missing,
> >
> > missing,
> >
> > true, //COMMA
> >
> > missing,
> >
> > missing,
> >
> > missing,
> >
> > missing
> >
> > );
> >
> > path = "";
> >
> > path = System.IO.Path.GetFullPath(@"Reports").ToString();
> >
> > path = path + @"\" + frmSiteName.siteName + " GenSumm " + DateTime.Now.Day

> +
> > "_" + DateTime.Now.Month + "_" + DateTime.Now.Year + " " +

> DateTime.Now.Hour
> > + "." + DateTime.Now.Minute + ".csv";
> >
> > excelWorkbook.SaveAs(path, missing, missing, missing, missing, missing,
> > XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);
> >
> > object pwd = "cgclogic";
> >
> > excelWorkbook.Unprotect(pwd);
> >
> > Excel.Sheets excelSheets = excelWorkbook.Worksheets;
> >
> > excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("Log Report");
> >
> > excelWorksheet.Unprotect(pwd);
> >
> >
> >
> >
> >
> >
> >
> > And before that form termination, write code :
> >
> > excelWorkbook.Save();
> >
> > exlApp.Workbooks.Close();
> >
> >
> > exlApp.Quit();
> >
> > exlApp = null;
> >
> > excelWorksheet = null;
> >
> > excelWorkbook = null;
> >
> >
> >
> > This form is opening through MDI.
> >
> >
> >
> > But even after form close 'Windows Task Manager' is showing 'Excel' in
> > process.
> > And after application colse it is removing from task manager.
> >
> > When that file in the reports folder try to open, it is giving error : "
> > This file is already opened . DO you want to reopen the file ?" or " This
> > file is being modified by other user . Open a read-only copy."
> >
> >
> >
> > please tell how to close that file using code.
> >

>
>
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      6th Dec 2007
Are all your object variables released in opposite order to which they were
referenced.

Just for testing, make the instance visible
exlApp.visible = true

close all workbooks and release your objects

do not do exlApp.Quit

Do you have any workbooks open, if so why, eg, xl is waiting for user to
dismiss some message perhaps
also check for any hidden workbooks, alt-F11 and look in projects panel in
the VBE

Manually quit
Is your workbook still open

If no workbooks are open, repeat with Quit and destroy your exlApp

Regards,
Peter T

"Namrata" <(E-Mail Removed)> wrote in message
news:8CAA4395-7A02-4518-9DAA-(E-Mail Removed)...
> Thanks for reply !
> I change the code as:
> > excelWorksheet = null;
> >
> > ensure all unsaved wb's are closed, after saving or with close false
> > excelWorkbook = null;
> > exlApp.Quit();
> > exlApp = null;

>
> But excel file still remains open.
>
> "Peter T" wrote:
>
> > Only a guess but try changing
> >
> > > exlApp.Quit();
> > >
> > > exlApp = null;
> > >
> > > excelWorksheet = null;
> > >
> > > excelWorkbook = null;
> > >

> >
> > to -
> >
> > excelWorksheet = null;
> >
> > ensure all unsaved wb's are closed, after saving or with close false
> > excelWorkbook = null;
> > exlApp.Quit();
> > exlApp = null;
> >
> > Regards,
> > Peter T
> >
> >
> > "Namrata" <(E-Mail Removed)> wrote in message
> > news:797B4E2F-1BF0-4FA9-990B-(E-Mail Removed)...
> > > Hi,
> > >
> > >
> > >
> > > I am using Excel for reporting. For opening excel file I have write

code :
> > >
> > >
> > >
> > > exlApp = null;
> > >
> > > exlApp = new ApplicationClass();
> > >
> > > //try
> > >
> > > //{
> > >
> > > object missing = System.Reflection.Missing.Value;
> > >
> > > string path =

System.IO.Path.GetFullPath(@"Templates\Log.csv").ToString();
> > >
> > > excelWorkbook = exlApp.Workbooks.Open(path, missing,
> > >
> > > 1,
> > >
> > > missing,
> > >
> > > Excel.XlTextQualifier.xlTextQualifierNone,
> > >
> > > missing,
> > >
> > > missing,
> > >
> > > missing,
> > >
> > > true, //COMMA
> > >
> > > missing,
> > >
> > > missing,
> > >
> > > missing,
> > >
> > > missing
> > >
> > > );
> > >
> > > path = "";
> > >
> > > path = System.IO.Path.GetFullPath(@"Reports").ToString();
> > >
> > > path = path + @"\" + frmSiteName.siteName + " GenSumm " +

DateTime.Now.Day
> > +
> > > "_" + DateTime.Now.Month + "_" + DateTime.Now.Year + " " +

> > DateTime.Now.Hour
> > > + "." + DateTime.Now.Minute + ".csv";
> > >
> > > excelWorkbook.SaveAs(path, missing, missing, missing, missing,

missing,
> > > XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);
> > >
> > > object pwd = "cgclogic";
> > >
> > > excelWorkbook.Unprotect(pwd);
> > >
> > > Excel.Sheets excelSheets = excelWorkbook.Worksheets;
> > >
> > > excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("Log Report");
> > >
> > > excelWorksheet.Unprotect(pwd);
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > And before that form termination, write code :
> > >
> > > excelWorkbook.Save();
> > >
> > > exlApp.Workbooks.Close();
> > >
> > >
> > > exlApp.Quit();
> > >
> > > exlApp = null;
> > >
> > > excelWorksheet = null;
> > >
> > > excelWorkbook = null;
> > >
> > >
> > >
> > > This form is opening through MDI.
> > >
> > >
> > >
> > > But even after form close 'Windows Task Manager' is showing 'Excel' in
> > > process.
> > > And after application colse it is removing from task manager.
> > >
> > > When that file in the reports folder try to open, it is giving error :

"
> > > This file is already opened . DO you want to reopen the file ?" or "

This
> > > file is being modified by other user . Open a read-only copy."
> > >
> > >
> > >
> > > please tell how to close that file using code.
> > >

> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re:Closing an excel file without saving it.(look through the code ple) Satyam Reddy Microsoft Excel Programming 2 14th May 2008 12:27 PM
code for closing out an excel file while in the browser? =?Utf-8?B?VG9t?= Microsoft Excel Programming 1 2nd Sep 2006 08:35 PM
Code for Closing all Excel Applications =?Utf-8?B?TWF0dA==?= Microsoft Access 3 16th Dec 2004 09:32 PM
Closing an opened Excel file, Code? =?Utf-8?B?TWF0dA==?= Microsoft Access 0 25th Oct 2004 08:01 PM
Code for closing an Excel file from Access =?Utf-8?B?TWF0dA==?= Microsoft Access 1 6th Oct 2004 04:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:43 AM.