Word add-in causes Word 2016 stopped working

Add-in Express™ Support Service
That's what is more important than anything else

Word add-in causes Word 2016 stopped working
 
Pancham Kumar




Posts: 6
Joined: 2016-08-24
Hi,
I have a word add-in that was developed with add-in express 7.5 and visual studio 2013, and then upgraded to the add-in express 8.2 so that it can support WORD 2016. It is successfully upgraded and works fine in word 2010, but when I try to run it in word 2016,(especially when I try to open a particular document which triggers one of my add-in functionality like opening a popup and creating a new instance of task pane and populating it with some data. Another popup opens saying "Word stopped working" and after sometime it says "Word has stopped working click the below button to close the program". However If I open just word application and select blank document and then try to perform some of my add-in feature, it works totally fine.

After some google search, specifically from Microsoft forum I got an solution which guided me to upgrade my add-in to the latest version, I created a setup project with a new version but with no resolution. I still get the same error.

Any idea what could be the possible reason?

Thanks for any help

Regards,
Pancham
Posted 02 Sep, 2016 09:34:54 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Pancham,

I suggest that you debug this situation to find the code line (method call) responsible for the crash.

You can add a number of debug messages to your code; use System.Diagnostics.Debug.WriteLine(). You collect the messages at run time using DebugView (see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx).


Andrei Smolin
Add-in Express Team Leader
Posted 02 Sep, 2016 09:47:50 Top
Pancham Kumar




Posts: 6
Joined: 2016-08-24
Hello Andrei

Thanks for the instant reply, I tried debugging my application using visual studio. There were few calls (say 5-6) to
adxWordEvents_DocumentChange like for opening the word application itself, opening the popup, opening the document etc.

private void adxWordEvents_DocumentChange(object sender, EventArgs e)
        {
            Word.WdCursorType oldCursor = WordApp.System.Cursor;
            bool oldScreenUpdateSetting = WordApp.ScreenUpdating;
            try
            {
                WordApp.ScreenUpdating = false;
                WordApp.System.Cursor = Word.WdCursorType.wdCursorWait;
                Word.Document activeDocument = WordApp.ActiveDocument;
                if (activeDocument != null)
                {
                    //some code goes here
                }
            }
            catch (Exception ex)
            {
                RibbonTab.Visible = false;
            }
            finally
            {
                CommonUtils.releaseComObject(activeDocument);
                WordApp.ScreenUpdating = oldScreenUpdateSetting;
                WordApp.System.Cursor = oldCursor;
            }
        }


for some of the those calls Word.Document activeDocument = WordApp.ActiveDocument; throws an Exception

System.Runtime.InteropServices.COMException was caught
HelpLink=wdmain11.chm#37016
HResult=-2146824040
Message=This command is not available because no document is open.
Source=Microsoft Word
ErrorCode=-2146824040
StackTrace:
at Microsoft.Office.Interop.Word.ApplicationClass.get_ActiveDocument()
at WordConnect.AddinModule.adxWordEvents_DocumentChange(Object sender, EventArgs e) in c:\Users\pancham.gupta\Documents\Visual Studio 2013\Projects\SampleAddin_1.3\AddinModule.cs:line 317
InnerException:



Could be this a possible reason?

I appreciate your help.

Regards,
Pancham
Posted 02 Sep, 2016 11:52:29 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Pancham,

I can't tell. I strongly recommend that you wrap getting ActiveDocument in a separate try/catch block. Also, to avoid an expected exception, I wouldn't call ActiveDocument if Documents.Count = 0.

Hope CommonUtils.releaseComObject reacts correctly if it is passed a null.


Andrei Smolin
Add-in Express Team Leader
Posted 05 Sep, 2016 08:49:03 Top
Pancham Kumar




Posts: 6
Joined: 2016-08-24
Hi Andrei Smolin,

Thanx for the response. CommonUtils.releaseComObject() has a null check. that is not a problem. I tried checking Documents.count >0 as


if(WordApp.Documents.Count > 0 && WordApp.ActiveDocument != null)
{
   //some code goes here
}


but WordApp.ActiveDocument still throws the same error if the WordApp.Documents.Count is 1.

Is this accounts for the actual problem? Because even though it throws an error it is properly handled in catch block.
If not, what else can be the possible reason?

Another thing,
After opening the said document, I close all opened blanks documents.

private void myCustomMethod();
        {
            //some code goes here related to opening the document

            closeBlankDocuments(WordApp);
            
            //spme code goes here
        }

 public void closeBlankDocuments(Word._Application WordApp)
        {
            Word.Documents openDocuments = null;
            Word.WdCursorType oldCursor = WordApp.System.Cursor;
            bool oldScreenUpdateSetting = WordApp.ScreenUpdating;
            object missing = Type.Missing;
            object refFalse = false;
            try
            {
                WordApp.ScreenUpdating = false;
                WordApp.System.Cursor = Word.WdCursorType.wdCursorWait;
                openDocuments = WordApp.Documents;
                FileUtilService fileService = FileUtilFactory.getInstance();
                for (int index = 1; index <= openDocuments.Count;index++ )
                {
                    Word.Document currentDocument = openDocuments[index];
                    String value = fileService.readDocumentPropertyFromDocument(currentDocument, "IS_CLOSABLE");
                    if ("true".Equals(value))
                    {
                        ((Word._Document)currentDocument).Close(refFalse, missing, missing);
                    }
                    CommonUtils.releaseComObject(currentDocument);
                }
            }
            finally
            {
                CommonUtils.releaseComObject(openDocuments);
                WordApp.System.Cursor = oldCursor;
                WordApp.ScreenUpdating = oldScreenUpdateSetting;
            }
        }


Addin works properly if I remove that call closeBlankDocuments. Why is that? Any Idea?
How to avoid that because I cannot remove that call?

I appreciate your help.

Regards,
Pancham
Posted 06 Sep, 2016 09:47:38 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Pancham Kumar writes:
public void closeBlankDocuments(Word._Application WordApp)


Scan the documents in the reverse order.

I assume you have all other COM add-ins disabled.

I would like that you send me blocks of your code so that I could create an add-in project to reproduce the issue. If this is possible, you can find the support email address in {Add-in Express installation folder}\readme.txt. Please make sure your email contains a link to this topic.


Andrei Smolin
Add-in Express Team Leader
Posted 07 Sep, 2016 09:13:40 Top
Pancham Kumar




Posts: 6
Joined: 2016-08-24
Hi Andrei Smolin,

I was using Interop assembly version 14.0 for my word add-in project. After upgrading it to 15.0, I am not facing any issue like "Word stopped working" for now.
Can you please tell me what could be the possible reason that had caused word 2016 to stopped working?
Is there any file or registry or something that was missing in Interop assembly version 14.0?

Thanks for the instant response. I really appreciate it.

One more thing, I am opening a outlook new Mail Item(referring to https://msdn.microsoft.com/en-us/library/office/ff462097.aspx) from my word-addin to send an email. I am able populate its recipient(Including Originator recipient), subject, body and attachments. But How can I make one of the attachments added mandatory i.e. It cannot be removed or changed by the end user? And same for the Originator recipient or From comboBox. How can I make these field be non editable by the end user? Is there any event handler to access outlook mailItem controls such that I can enable or disable some controls like attachment ribbon menu or from comboBox?
Cause this is somewhat new to me.

Regards,
Pancham
Posted 23 Sep, 2016 13:42:48 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Pancham,

Pancham Kumar writes:
Can you please tell me what could be the possible reason that had caused word 2016 to stopped working?


I can't. I would recommend that you find the call that causes the issue.

Pancham Kumar writes:
But How can I make one of the attachments added mandatory i.e. It cannot be removed or changed by the end user? And same for the Originator recipient or From comboBox.


The Outlook object model doesn't provide a way to create such an attachment or recipient. The Outlook UI doesn't provide such a way either.

Pancham Kumar writes:
Is there any event handler to access outlook mailItem controls such that I can enable or disable some controls like attachment ribbon menu or from comboBox?


No.


Andrei Smolin
Add-in Express Team Leader
Posted 26 Sep, 2016 02:37:08 Top
Pancham Kumar




Posts: 6
Joined: 2016-08-24
Hi Andrei Smolin,

Then any other way to accomplish this?

I am creating a MailItem as

                oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                oInspector = oMailItem.GetInspector;
                // Thread.Sleep(10000);

                //Recipient
                oRecips = (Outlook.Recipients)oMailItem.Recipients;

                //Add Originator(i.e. To)
                if (originator.Trim().Equals("") == false)
                {
                    oRecip = (Outlook.Recipient)oRecips.Add(originator);
                    oRecip.Type = (int)Outlook.OlMailRecipientType.olOriginator;
                    oRecip.Resolve();
                }

                //Add To
                foreach (String recipient in mailRecipients)
                {
                    oRecip = (Outlook.Recipient)oRecips.Add(recipient);
                    oRecip.Type = (int)Outlook.OlMailRecipientType.olTo;
                    oRecip.Resolve();
                }    
                // Add AttachMents
                oAttachments = oMailItem.Attachments;
                foreach(MailAttachmentMs attachment in attachments)
                {
                    oAttachment = oAttachments.Add(attachment.filePath,Outlook.OlAttachmentType.olByValue,Type.Missing, attachment.fileName);
                    oMailItem.Save();
                }

                //Display the mailbox
                oMailItem.Display(true);


It is just a sample code by which I am able to populate MailItem fields.
I have to disable one of the attachments me non removable and also user cannot add attachments himself.
Is there any way to do this?
Posted 26 Sep, 2016 10:06:57 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Pancham,

As explained, you can't prevent the user form deleting an attachment or modifying recipients and/or email address(es). You can try to check if the email that the user is sending matches your requirements; see the ItemSend event of the Outlook Events component.


Andrei Smolin
Add-in Express Team Leader
Posted 26 Sep, 2016 10:27:25 Top