Outlook VSTO - การเรียก TypeText เมื่อเลือกจะพ่นคำสั่งนี้ไม่พร้อมใช้งานข้อยกเว้น

การโทร TypeText บน Selection จะแสดง "คำสั่งนี้ไม่พร้อมใช้งาน" ข้อยกเว้น

ป้อนคำอธิบายรูปภาพที่นี่

ด้านล่างคือรหัสของฉัน

public void AddFilePaths(List<string> urls)
{
    if (urls.Count > 0)
    {
        MailItem mi = null;
        bool newMailItem = false;

        mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        mi.Body = "New email body"; 
        newMailItem = true;

        mi.Display();
        inspector = MyAddIn.Application.ActiveInspector();

        if (mi != null)
        {
            foreach (var url in urls)
            {
                AddPathToActiveInspector(urls);
            }
        }
    }
}

public void AddLinkToCurrentInspector(string url)
{
    var inspector = MyAddIn.Application.ActiveInspector();
    var currMessage = inspector.CurrentItem;
    Microsoft.Office.Interop.Word.Document word = currMessage.GetInspector.WordEditor; 
    dynamic wordapp = word.Application;
    const string text = "\n"; // thisfor some reason will not add new line
    Microsoft.Office.Interop.Word.Selection sel = word.Windows[1].Selection;
    sel.TypeText(text); // this often errors
    string address = url;
    string subAddress = "";
    string screenTip = "";
    string textToDisplay = url; 
    wordapp.ActiveDocument.Hyperlinks.Add(sel.Range, ref address, ref subAddress, ref screenTip, ref textToDisplay);
    if (word.ProtectionType != Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection) word.Unprotect(); 
    sel.TypeText(" "); 
}

person pixel    schedule 16.08.2016    source แหล่งที่มา


คำตอบ (2)


คุณไม่จำเป็นต้องโทร TypeText - เพียงตั้งค่าคุณสมบัติข้อความ:

Application.ActiveInspector.WordEditor.Application.Selection.Text = "test"
person Dmitry Streblechenko    schedule 16.08.2016
comment
น่าเสียดายที่นั่นไม่ได้สร้างความแตกต่าง ขอบคุณมิทรี - person pixel; 17.08.2016
comment
คุณกำลังเรียกสิ่งนี้กับผู้ตรวจสอบในโหมดแก้ไขหรือไม่? หรือเป็นตัวตรวจสอบแบบอ่านอย่างเดียว? - person Dmitry Streblechenko; 17.08.2016
comment
ฉันอัปเดตโพสต์ของฉันเพื่อรวมรหัส ขอบคุณมิทรี - person pixel; 17.08.2016
comment
เหตุใดคุณจึงใช้ MailItem.GetInspector หากคุณมีวัตถุ Inspector อยู่แล้ว อย่าใช้ Window.Selection - ใช้ Application.Selection - person Dmitry Streblechenko; 17.08.2016
comment
หากคุณดูภาพหน้าจอที่ฉันโพสต์ ฉันใช้ Application.Selection (ดูด้านบนในภาพหน้าจอ wordapp.Selection) เช่นเดียวกับ MailItem.GetInspector หากคุณดูภาพหน้าจอ คุณจะเห็นว่าฉันใช้วัตถุ Inspector ที่มีอยู่จาก ActiveInspector ดังนั้น ฉันไม่แน่ใจว่าคุณหมายถึงอะไร แต่นั่นเป็นสาเหตุของปัญหาทั้งหมดจริงๆ รหัสที่ฉันอัปเดตใช้งานได้จริง ชื่นชมมิทรีมาก BTW ฉันจะตั้งค่าตัวตรวจสอบให้อยู่ในโหมดแก้ไขเสมอได้อย่างไร ชื่นชมมาก - person pixel; 17.08.2016
comment
ดูที่บรรทัด word = currMessage.GetInspector.WordEditor คุณกำลังใช้ GetInspector ไม่จำเป็น - คุณสามารถใช้ค่าจาก 'MyAddIn.Application.ActiveInspector' - person Dmitry Streblechenko; 17.08.2016
comment
ฉันรู้ แต่นั่นคือรหัสที่ใช้งานได้จริงกับ Dmitry :) หากคุณดูภาพหน้าจอที่ฉันโพสต์ในตอนแรก คุณจะเห็นว่าฉันใช้ MyAddIn.Application.ActiveInspector และนั่นทำให้เกิดปัญหามากมายสำหรับฉัน ในโหมดแก้ไขข้อบกพร่อง โดยเฉพาะอย่างยิ่งที่ฉันเห็นว่ามันขัดข้องในบรรทัดที่เรียก Selection.TypeText(text) (หรือ Selection.Text = text หรือ Selection.InsertAfter(text)) - person pixel; 17.08.2016

จากคำตอบในคำถามนี้ ฉันยังได้แก้ไขปัญหานี้ด้วย รหัสที่ใช้งานได้ดีกว่าโค้ดด้านบนและง่ายต่อการติดตามคือด้านล่าง ขอบคุณมากสำหรับ @joeshwa:

    public void AddLinkToCurrentInspector(string url)
    {
        object link = url;
        object result = "url";
        object missing = Type.Missing;

        var inspector = MyAddIn.Application.ActiveInspector();
        MailItem currMessage = inspector.CurrentItem;
        Microsoft.Office.Interop.Word.Document doc = currMessage.GetInspector.WordEditor;
        Microsoft.Office.Interop.Word.Selection sel = doc.Windows[1].Selection;
        doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);
        sel.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdLine);
        sel.InsertAfter("\n");
        sel.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdLine);
    }
person pixel    schedule 18.08.2016