Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:24:04+00:00 2026-05-30T16:24:04+00:00

I am using EXCEL interop for genearting Excel and then i am attaching it

  • 0

I am using EXCEL interop for genearting Excel and then i am attaching it as an attachment to mail and then send the mail.After mail send i want to delete generated file. On deleting it is throwing error:

this file cannot be deleted as it is is used by another process

I searched on SO and other sites and found interesting facts about COM Components.
reference:- How do I properly clean up Excel interop objects?

if i am not attaching it as attachment then file is getting deleted. Before deletion I am removing all COM references.but when i am using it as attachments it is throwing error:
my code is like :

    workbook.SaveAs(root + statics + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    // Garbage collecting
                    // Clean up references to all COM objects
                    // As per above, you're just using a Workbook and Excel Application instance, so release them:
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    Marshal.FinalReleaseComObject(m_objRange);

                    Marshal.FinalReleaseComObject(worksheet);
                    workbook.Close(false, Type.Missing, Type.Missing);
                    Marshal.FinalReleaseComObject(workbook);
                    app.Quit();
                    Marshal.FinalReleaseComObject(app);

                    MailMessage mm = new MailMessage("def@gmail.com", "xyz@gmail.com", "TestMsg", "Hi");
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;

                    //Commenting Below two lines works fine ....!!!!!!!!!....WHY..???

                    //Attachment data = new Attachment(root + statics + ".xls");
                    //mm.Attachments.Add(data);

                    client.Send(mm);

                    File.Delete(root + statics + ".xls");

how to remove references after attaching it to mail.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:24:06+00:00Added an answer on May 30, 2026 at 4:24 pm

    Just Adding data.Dispose() after sending mail is working fine for me.here is modified code:

    workbook.SaveAs(root + statics + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    
                        // Garbage collecting
                        // Clean up references to all COM objects
                        // As per above, you're just using a Workbook and Excel Application instance, so release them:
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        Marshal.FinalReleaseComObject(m_objRange);
    
                        Marshal.FinalReleaseComObject(worksheet);
                        workbook.Close(false, Type.Missing, Type.Missing);
                        Marshal.FinalReleaseComObject(workbook);
                        app.Quit();
                        Marshal.FinalReleaseComObject(app);
    
                        MailMessage mm = new MailMessage("def@gmail.com", "xyz@gmail.com", "TestMsg", "Hi");
                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                        client.Credentials = CredentialCache.DefaultNetworkCredentials;
    
    
                        Attachment data = new Attachment(root + statics + ".xls");
                        mm.Attachments.Add(data);
                        //Now Working Fine:-
                         data.Dispose();
                        client.Send(mm);
    
                        File.Delete(root + statics + ".xls");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP.Net application I'm generating a Excel file using MsExcel Object Libby (Com-InterOp)
I work on writing the excel Add-In using Excel Interop. I want to get
I'm using the following code to generate an Excel file using Microsoft.Interop.Excel. The problem
Anyone have code to set margins(top,left,right,bottom) using excel interop and vb.net. I think it
I'm using the Excel interop in C# ( ApplicationClass ) and have placed the
I'm using the Excel COM interop to insert images (specifically EPS) into a spreadsheet.
I am creating an Excel Worksheet from C# using Microsoft.Office.Interop.Excel, and I have not
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I'm writing an Excel Addin using COM Interop from .net. I have a command
I am creating an excel report in vb.net using the office interop. When the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.