Note that there are some explanatory texts on larger screens.

plurals
  1. POOutlook MailItem Save/SaveAs
    primarykey
    data
    text
    <p>I have an outlook add-in that allows the user to save an email into a database. When the user does save the email I modify the email subject so it can be identified as being saved.</p> <p>Saving the email can happen in two ways. Via a button on the tool bar which allows the user to save any email they want, and also via a prompt which appears when a new email is put into the Sent Items folder. Both methods use the same form to save the email!</p> <p>OK, now to the problem ....</p> <p>In the process of saving the email I use the <code>mailItem.SaveAs</code> method to put it into the file store. After this has completed successfully i want to change the subject of the email which still exists in outlook to say that it has been saved successfully. I do this by changing <code>myItem.Subject</code> and then using the <code>mailItem.Save</code> method to save the change.</p> <p>The above works perfectly when the email isn't being saved via the prompt method. So when the user tries to save the email after they send it the <code>mailItem.Save</code> method does not work.</p> <p>I have narrowed it down to it actually working if i put the <code>myItem.Save()</code> line before the <code>myItem.SaveAs()</code> line, but obviously if I do this I can not guarantee the email was actually saved properly.</p> <p>So does any one know of a reason that the <code>mailItem.Save</code> method would want to not work after the <code>mailItem.SaveAs</code> method as been called?</p> <p>Thank you in advance to any suggestions to what might be the problem.</p> <p>EDIT : Code</p> <pre><code>if (_item is Outlook.MailItem) { // if the incoming item is an Outlook mail Item // cast as a mail item Outlook.MailItem myItem = (Outlook.MailItem)_item; if (directoryExists(directoryTemp)) { // if the temporary directory exists bool _profiled = true; // copy the item as type .msg in the temporary location myItem.SaveAs(saveTemp, Outlook.OlSaveAsType.olMSG); // setup impersonation to copy the file to a secure location PImpersonateUser _iU = new PImpersonateUser(); // do impersonation try { _iU.Impersonate("******", "******", "******"); if (File.Exists(savefile)) { // if file already exists in the location // delete existing file File.Delete(savefile); } // move the temporary file to the secure location with the proper name File.Move(saveTemp, savefile); string year = ""; if (ipt_year.SelectedItem != null) { // else if year has been selected year = ipt_year.SelectedItem.ToString(); } _profile.profileEmail(folderString(_subject_), _fileName, year); } catch (Exception e) { _profiled = false; // if impersonation fails cancel the impersonation _iU.Undo(); // show error MessageBox.Show(e.Source + "\n\n" + e.Message + "\n\n" + e.StackTrace, "SaveAs() Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { _iU.Undo(); } if (_profiled) { // if the email was profiled successfully // mark the original email as being profiled markAsProfiled(); } } else { // if temporary file save fails throw error MessageBox.Show("Temporary Directory (" + directoryTemp + ") Does Not Exist!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } </code></pre> <hr> <p>and the markAsProfiled function ...</p> <hr> <pre><code>private void markAsProfiled() { if (_item is Outlook.MailItem) { // if the incoming item is an Outlook mail Item // cast as a mail item Outlook.MailItem myItem = (Outlook.MailItem)_item; // make sure subject doesnt already have a profiled flag in the subject _subject_ = _subject_.Replace("[PROFILED] - ", ""); // add a profiled flag in the subject of the email myItem.Subject = "[PROFILED] - " + _subject_; // add a yellow flag to the email myItem.FlagIcon = Microsoft.Office.Interop.Outlook.OlFlagIcon.olYellowFlagIcon; // save email with changes made myItem.Save(); //MessageBox.Show("Mark as Profiled :: " + myItem.Subject + " :: " + myItem.Saved.ToString() + " :: "); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload