I am trying to write a scrip in VSTO that can export attachments and the email (as pdf)
The goal is to export the pdf which has the same information as when you print to pdf (aka it should include information such as from, to, subject, attachments etc.
From what I understand, there is no direct export to PDF for outlook VSTO (please correct me if I am wrong because it would bypass this whole issue). So I am saving the email as a word document before adding the required information myself.
My code loops through each MailItem in selection and passes it into a class CustomMailItem that handles the export.
It works well until it doesn’t. At some point, the document (oDoc below) defaults to returning the body of the first email in my selection (which is not passed into the CustomMailItem object). I am certain that the mailItem in the CustomMailItem object is the correct item since the headers and other exported items (.msg) are correct. This seems to be triggered at some specific MailItems but I cannot identify what makes those items special nor follow the logic of why and how that happens.
Edit: After some tracing, seems like the trigger is a email that takes longer than usual to load. However, no amount of
System.Threading.Thread.Sleep(sleepDelay);
inspector.Display();
inspector.Activate();
seems to be helping the email load in this scenario for some reason.
Main Body (non-relevant code removed)
private void exportAllSelected_Click(object sender, EventArgs e)
{
CustomMailItem thisCustomMailItem = null;
Word.Application wordApp = null;
int maxItems = 0;
int currentIndex = 0;
try
{
#region Initialise
GetExportOptions();
if (exportOptions.word || exportOptions.pdf)
{
wordApp = new Word.Application();
wordApp.Visible = true; //Debug only
}
#endregion
// Run with background worker and progress bar
ProgressHelper.RunWithProgress((worker, progressTracker) =>
{
#region Explorer
progressTracker.UpdateStatus($"Getting mail items");
Application outlookApp = Globals.ThisAddIn.Application;
Explorer explorer = outlookApp.ActiveExplorer();
maxItems = explorer.Selection.Count;
#endregion
foreach (object item in explorer.Selection)
{
MailItem mailItem = null;
try
{
//Checks to skip non MailItems
#region Export
thisCustomMailItem = new CustomMailItem(mailItem, exportOptions, wordApp);
thisCustomMailItem.ExportAll();
#endregion
}
catch (Exception ex)
{
// Log Error if required
}
finally
{
if (mailItem != null) { Marshal.ReleaseComObject(mailItem); }
if (thisCustomMailItem != null) { thisCustomMailItem.ReleaseItems(); }
}
}
});
}
catch (Exception ex)
{
// Log Error if required
}
finally
{
if (thisCustomMailItem != null) { thisCustomMailItem.ReleaseItems(); }
if (wordApp != null)
{
wordApp.Quit();
Marshal.FinalReleaseComObject(wordApp);
wordApp = null;
}
}
}
Relevant Code within CustomMailItem:
Word.Document tempDoc;
Inspector inspector;
private void GetWordDoc()
{
#region Save word document from mailItem
inspector = mailItem.GetInspector;
System.Threading.Thread.Sleep(100);
if (!(inspector.IsWordMail() && inspector.EditorType == OlEditorType.olEditorWord))
{
throw new Exception("Selected item is not word mail");
}
Word.Document oDoc = inspector.WordEditor;
bool isODocNull = oDoc == null;
oDoc.SaveAs2(paths.word);
System.Threading.Thread.Sleep(100);
#region Discard
Marshal.FinalReleaseComObject(oDoc);
oDoc = null;
inspector.Close(OlInspectorClose.olDiscard);
Marshal.FinalReleaseComObject(inspector);
inspector = null;
#endregion
#endregion
#region Format Word Doc
RetryForBusy(() => { tempDoc = wordApp.Documents.Open(paths.word); }); // Manual inspection shows the contents of this document defaults to the first email in selection at some point.
// Do other operations with Word Doc Here (e.g. add sender, subject, export as PDF)
#endregion
}
ZQ G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Bypassed the issue by exporting as rtf and opening that instead of trying to save the email as a word document.
ZQ G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.