none
Unhandled exception when code runs for the first time,but works fine the second RRS feed

  • Question

  • Hi All,

    I have the following code snippet

    private void RGL_Click(object sender, RibbonControlEventArgs e)
            {

    Word._Document oDoc;
               oDoc = Globals.ThisAddIn.Application.ActiveDocument;
                object start = oDoc.Content.Start;
                object end = oDoc.Content.End;
                oDoc.Range(ref start, ref end).LanguageID = WdLanguageID.wdEnglishCanadian;
                oDoc.Range(ref start, ref end).NoProofing = 0;
                 Microsoft.Office.Interop.Word.ReadabilityStatistic DocStats;
                    DocStats = oDoc.ReadabilityStatistics[10];------>exception occurs here when code runs first time
                    MessageBox.Show(DocStats.Name + " " + DocStats.Value);

    }

    When I copy paste a document and click on the button it raises a COM exception but if I click continue and click on the button again, the correct grade level score is displayed.Cant figure out why this is happening.


    Thursday, January 15, 2015 6:27 AM

Answers

  • Yes Fei ,I resolved this issue by adding a try catch block like this :

     try
                {

                //the code to get readability stats

                }
                catch // if images or any chart(smart act objs) are found,
                {
                    foreach (Word.InlineShape ishl in inlineShapes)
                    {
                           ishl.Reset();==> using this removes any exception and runs statistics test,the very first time
                     } //then use the code to get readability stats after the for-loop
                   }

    This was the only feasible solution I could come up with , hope it helps. 


    Tuesday, January 27, 2015 5:05 AM

All replies

  • Hello Apporva,

    What exception do you get in the code? Could you please be more specific?

    I'd suggest breaking the chain of calls:

    oDoc.ReadabilityStatistics[10];

    and declaring each property or method call on a separate line of code, for example:

    Word.ReadabilityStatistics stat = oDoc.ReadabilityStatistics;

    Thus, you will find what property or method fires the exception.

    Finally, before calling the indexer with a value (10) I'd recommend iterating over all items in the collection to get it initialized. Let me know if it helps.

    Thursday, January 15, 2015 6:21 PM
  • Thanks for the answer Eugene, this exception never shows up if I type something and click on the rgl button , but if i open a huge document that is already in Canadian english and click on this ribbon it gives me the following exception

    System.Runtime.InteropServices.COMException was unhandled by user code
      HelpLink=wdmain11.chm#36966
      HResult=-2146824090
      Message=Command failed
      Source=Microsoft Word
      ErrorCode=-2146824090
      StackTrace:
           at Microsoft.Office.Interop.Word.ReadabilityStatistics.get_Item(Object& Index)
           at UsabilityMapping_V01.Ribbon1.RGL_Click(Object sender, RibbonControlEventArgs e) in <my path> 1211
           at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
           at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponentImpl component, Object[] args)
           at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.Invoke(RibbonComponentCallback callback, Object[] args)
           at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
           at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
      InnerException: 

    Thursday, January 15, 2015 6:47 PM
  • Did you have a chance to change the code and iterate over all items in the collection before accessing the 10th item?
    Thursday, January 15, 2015 7:12 PM
  • Yes Eugene, in the very first iteration itself the same exception is coming, if i say continue and click on the button , the right score is displayed.
    Friday, January 16, 2015 5:26 AM
  • Also, Eugene, do you think there is something wrong with setting the language as 

     oDoc.Range(ref start, ref end).LanguageID = WdLanguageID.wdEnglishCanadian;

    is not making it as the default language? 

    Because if I don't have this line, I manually change the language to Canadian English I'am getting the score without any exceptions.Please let me know how or what is going wrong with setting the default language via my plugin.If i use a particular document and I say  MessageBox.Show("Language" + oDoc.Content.LanguageID) ; it is giving me a value as 99999 // for the first time, second time I click the button it says .wdEnglishCanadian,  really not able to figure out the error.



    Friday, January 16, 2015 7:42 AM
  • Ok I figured out why there is exception,whenever my document has an image/picture, I' am getting this exception so I must figure out a work around for this 
    Friday, January 16, 2015 6:32 PM
  • Hi Apoorva Sridharan,

    Have you fixed this issue? If yes, would you mind sharing the workaround with us?

    If not, I suggest that you upload a sampe document to help us to troubleshoot this issue since I am not able to reproduce this issue with the image in a document.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, January 26, 2015 10:03 AM
    Moderator
  • Yes Fei ,I resolved this issue by adding a try catch block like this :

     try
                {

                //the code to get readability stats

                }
                catch // if images or any chart(smart act objs) are found,
                {
                    foreach (Word.InlineShape ishl in inlineShapes)
                    {
                           ishl.Reset();==> using this removes any exception and runs statistics test,the very first time
                     } //then use the code to get readability stats after the for-loop
                   }

    This was the only feasible solution I could come up with , hope it helps. 


    Tuesday, January 27, 2015 5:05 AM