locked
outlook add-in,Word.Application.Selection null some time on some PC RRS feed

  • Question

  • Hi, I have an outlook add-in,

    the add-in will read a rtf template file and insert the file into the mail body.

    but, now I encounter a problem,on some pc, some time the rtf file can be insert successfully , some time failed.

    and the error is : Word.Selection is null.

    my code is as below:

    public void InsertFile(Outlook.Inspector inspector)

    {

        object missing = System.Reflection.Missing.Value;

    try        {

                    Word.Application wordAppl;
                    Word.Document wordDoc;
                    Word.Selection wordSel;
                    insp = inspector;
                    wordDoc = insp.WordEditor as Word.Document;
                    object replaceAll = Word.WdReplace.wdReplaceAll;
                    object units = Microsoft.Office.Interop.Word.WdUnits.wdStory;
                    object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                    object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                    if (wordDoc == null)
                    {
                        Logging.LogError("wordDoc object is null,return");
                        return;
                    }
                    wordAppl = wordDoc.Parent as Word.Application;

                    if (wordAppl == null)
                    {
                        Logging.LogError(" wordAppl object is null,return ");
                        return;
                    }

                    wordSel = (Word.Selection)wordAppl.Selection;
                    if (wordSel != null)
                    {
                        Logging.LogError( wordSel is not null,wordSel: " + wordSel);
                    }
                    if(wordSel == null)            //here, wordSel object give out to be null
                    {
                        Logging.LogError(" wordSel object is null, return");
                        return;
                    }

      object falseRef = false;

     wordSel.Range.InsertFile(fileName, ref missing, ref falseRef, ref falseRef, ref falseRef);

    }

    Wednesday, July 24, 2013 8:07 AM

All replies

  • general rule, try to not depend on selection, use ranges and bookmarks instead.
    Wednesday, July 24, 2013 1:36 PM
  • I add   wordDoc.Activate(); but now get another exception throw out:

    " ERROR: Object reference not set to an instance of an object."

    when the below code is executed:

    body.InsertFile(Globals.ThisAddIn.Application.ActiveInspector());

    public void InsertFile(Outlook.Inspector inspector)

    try        {

                   Word.Application wordAppl;
                    Word.Document wordDoc;
                    Word.Selection wordSel;
                    insp = inspector;
                    wordDoc = insp.WordEditor as Word.Document;
                    object replaceAll = Word.WdReplace.wdReplaceAll;
                    object units = Microsoft.Office.Interop.Word.WdUnits.wdStory;
                    object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                    object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;           ...................................

      catch (Exception ex)
                {
                    Logging.LogError("Error while updating message body", ex);
                }

    who can help me?


    • Edited by jennyliang Tuesday, July 30, 2013 2:52 AM add exception code
    Tuesday, July 30, 2013 2:50 AM