Outlook VSTO (C#) mailItem.GetInspector.WordEditor returns the wrong email body after some number of email

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

}

New contributor

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.

New contributor

ZQ G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật