Question regarding creating, saving and emailing EXCEL document

Hi,

I am using C# and VSTO to populate an EXCEL document from a database. I then want to email this EXCEL document as an attachment to a specified number of users.

Everything works except when I try to open the attachement. I receive the following error "The file could not be accessed. Try one of the following: Make sure the specified folder exists, Make sure the folder that contains the file is not read-only, Make sure the file name does not contain any of the following characters.......".

I tried sending a file, .xls and .txt, that already exists and the attachment opens up correctly. I think VSTO is not properly closing excel before emailing it. Does anyone have any ideas on how to fix this

Below is the code:

Globals.ThisWorkbook.ThisApplication.Quit();

Globals.ThisWorkbook.ThisApplication.SaveWorkspace(@"c:\test.xls");

try

{

SmtpClient aSmtp = new SmtpClient("x.x.x.x");

//Mail Initialization

aMail.Subject = "test" + " - " + dt.ToShortDateString();

Attachment excelAttachment = new Attachment(@"c:\test.xls");

aMail.Attachments.Add(excelAttachment);

aSmtp.Send(aMail);

MessageBox.Show("Message sent succesfully");

}

catch (Exception ex)

{

MessageBox.Show("Exception = " + ex);

}

Thanks

ds




Answer this question

Question regarding creating, saving and emailing EXCEL document

  • TomBB

    Hi ds

    Does the information provided in this thread on disconnecting the VSTO functionality help

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=503556&SiteID=1



  • zhuyanjun

    Oh, sorry about that. What I have done is added the following two lines of code:

    Globals.ThisWorkbook.RemoveCustomization();

    Globals.ThisWorkbook.SaveAs(@"c:\test.xls", missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);

    This has helped me to be able to email the data without the VSTO customization. But, when I open the document, it is still asking if I want to update the links, when I select Dont Update, it gives an error message, but, all my data is on the Excel sheet as I want it.



  • Mateusz Rajca

    You might want to take this to a new thread, as you're getting out of the area where I can help you :-)

    Be sure to mention what you need (to email a VSTO spreadsheet to people who haven't got VSTO). The link I showed you, and what you're doing now. Then describe the problem. You further need to provide

    - What kinds of links the document has (to what, how you created them, whether they're available from the recipients machine, anything relevant)

    - the EXACT wording of the message the recipient gets when opening the document

    IOW, we need as complete a picture as possible about the file and the state it's in.



  • Batuhan

    Just an update:

    When I do a SaveAs, I can open the file, but it is still trying to run the VSTO code.

    Globals.ThisWorkbook.SaveAs(@"c:\test.xls", missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);

    So, to clarify my problem, I am trying to create this Excel file, but send an Excel file of this data as an attachment. I dont want to send the Excel document as an attachment, that then tries to run again when it is opened. I just want to send the data.

    Thanks

    ds



  • PCSQL66

    Hi Cindy,

    Yes, that helps alot! I now have it working with one slight problem. When I open the Excel document, it asks if I would like to update the spreadsheet. I select "Don't Update", it gives an error with the title "Office document customization is not available". Is there any way to get rid of this error box

    Thanks

    ds



  • leonreet

    Since you don't list the things you've done, it's a bit of a grab-bag, but...

    It sound as if you need to remove the datacached items



  • Question regarding creating, saving and emailing EXCEL document