none
Strange bugs in Word Add-in RRS feed

  • Question

  • I have 3 strange bugs in my Word add-in. 
    1. I got exception "This property cannot be set during the object's event handler", when
    I set Custom Task Pane Visible = true. 
    It appears only for 2 users this far and not always (one time after Word crash). 
    Visible=true is set in ribbon button Pressed event handler.
    2. SEHException in Shading.SetBackgroundPatternColor
    4 users have this mistake appearing randomly.
    I catch exception -2146823683 (Proofing dialog is active), and try to set backcolor again, pumping Application DoEvents. Maybe this is the problem, but I don't see other way to avoid it.
    3. SEHException in setting Range.Text or Range.Start properties
    Appears only for 1 user.
    I can't reproduce both of errors and don't know what to do. Please help!

    Беликов Сергей Программист-фрилансер MCTS
    Wednesday, August 31, 2011 8:17 AM

Answers

  • Hello Nightblade,

    Throughout this thread you have cited random situations happening with some of your end-users, but situations you can’t repeat.

    Problem # 1 happens to only 2 users and not always. Do you know what those two users have in common in the way of system environment, Service Pack levels, and similar contextual characteristics that you and other users do not have in common?

    Problem #2 happens to 4 users, randomly. Is it frequent for each of those users, or such a random problem that some of the 4 users have reported it only once? What do the users do when they get the problem? (Re-start your program, restart Word, re-boot the system?) What do the users say happened before the problem?

    You mentioned event handlers for tick events and timers. Could you learn anything by adding logging to the event handlers so that you see what happens in a normal event, and whether there is a timing issue?

    Calvin asked you for a repro to share with the Forum viewers. This would be worthwhile and valuable if you can build one that does repro the problems – any one of them. Since you don’t know how to reproduce the problems perhaps a simple demonstration application for others to try would suggest to them arenas they would identify as possible suspects.  

    Short of that, your best troubleshooting will be to identify what is common to the users with the problems that is distinct from the environment of the users who don’t have (or report) the problems.

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    Wednesday, September 7, 2011 2:58 PM
    Moderator
  • In case anyone have similar problem, I found out that using 

    Range.Font.Shading.BackgroundPatternColor

    instead of 

    Range.Shading.BackgroundPatternColor

    solves the problem.

    Monday, January 23, 2012 1:21 PM

All replies

  • I have 3 strange bugs in my Word add-in. 
    1. I got exception "This property cannot be set during the object's event handler", when
    I set Custom Task Pane Visible = true. 
    It appears only for 2 users this far and not always (one time after Word crash). 
    Visible=true is set in ribbon button Pressed event handler.
    2. SEHException in Shading.SetBackgroundPatternColor
    4 users have this mistake appearing randomly.
    I catch exception -2146823683 (Proofing dialog is active), and try to set backcolor again, pumping Application DoEvents. Maybe this is the problem, but I don't see other way to avoid it.
    3. SEHException in setting Range.Text or Range.Start properties
    Appears only for 1 user.
    I can't reproduce both of errors and don't know what to do. Please help!
    Wednesday, August 31, 2011 8:21 AM
  • I am developing Word add-in and noticed that som of functions (for example, setting underline for Range) cause something like WinForms Application.DoEvents. My other waiting WinForms event handlers (timer Ticks, clicks e.t.c.) execute Before current event handler (containing setting underline) is finshed. This breaks whole logic of application. :( 

    I ended up with WinForms timer, queue of commands processed in Tick event and flag variable - while command is processed, all other commands stays in queue. But there are commands that I must process immediatly (not leaving event handler).

    Is there better way? I tried to move app in separate thread (other than Word main thread), but found out that CustomTaskPane always created in main thread, and this makes things too complicated and brings many possibilities for deadlocks between waiting CustomTaskPane handler and Word commands...

    Wednesday, August 31, 2011 8:40 AM
  • Hi all

    Sergei has apparently done some additional detective work in the meantime and posted a follow-up here:

    http://social.msdn.microsoft.com/Forums/en-US/officegeneral/thread/1c5d3b34-73af-4d96-b97d-71407ae2d484


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 31, 2011 9:11 AM
    Moderator
  • Hi Nightblade,

    Thanks for your post.

    For your first issue, based on the limited symptom as you description, I guess that you handle the CustomTaskPane.VisibleChanged event in your add-in and try  to change the visible value in this event, something like this:

    For you second issue, I can't figure out what the Shading.SetBackgroundPatternColor mean...

    Would you like to provide more details? Also I expect that you can post some related codes.

    Have a nice day.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 1, 2011 9:47 AM
    Moderator
  • Thanks for your answer, Calvin!
    I know about VisibleChange event handler, but stack trace of exception shows that it was RibbonButton Pressed event handler.

    But there is a chance that user clicks button when VisibleChange event handler is active. This is possible because of problem I described there
    http://social.msdn.microsoft.com/Forums/en-US/officegeneral/thread/1c5d3b34-73af-4d96-b97d-71407ae2d484
    This also explains why this exception is appearing randomly.
    Will try check this...

    Now about shading issue.
    My addin highlight some text when user click buttons on CustomTaskPane. Something like selection of text but with my colors and independed of standard selection.
    I save text BackgroundPatternColor, use Range.Shading.BackgroundPatternColor to set background color for text to highlight it.
    Most of the time it works ok, but sometimes for some users this exception is thrown.
    Here is the code

            const int ProofingErrorCode = -2146823683;

            public static Word.WdColor GetBackgroundColor(Word.Range r)
            {
                // ugly workaround for word 2007 problem
                // System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because the proofing dialog box is active.
                while (true)
                {
                    try
                    {
                        return r.Shading.BackgroundPatternColor;
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        if (ex.ErrorCode != ProofingErrorCode) throw ex;
                        // if we don't use doevents, main ui thread will hang and don't response to user events
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            }

            public static void SetBackgroundColor(Word.Range r, Word.WdColor c)
            {
                // ugly workaround for word 2007 problem
                // System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because the proofing dialog box is active.
                while (true)
                {
                    try
                    {
                        r.Shading.BackgroundPatternColor = c;
                        return;
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        if (ex.ErrorCode != ProofingErrorCode) throw ex;
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            }

    Problem with intersection of event handlers appeared before I did this workaround with DoEvents, so this is not the problem. And I don't see other ways to avoid Proofreading bug.

    Is there any way to check that word have some dialog window active?
    Can I post some operation in Word message queue async way to avoid this issue?
    I thought about making STA thread and call all Word functions from there, but stuck with custom task pane, that always created in main thread.

    Thursday, September 1, 2011 11:07 AM
  • Hi,

    I have merged your two threads into this one as there are related information. And if you can provide us a simple sample project so that we can reproduce them on our side, which will help us understand your issues more correctly.

    I look forward to hearing of you soon.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 2, 2011 7:53 AM
    Moderator
  • Hello Nightblade,

    Throughout this thread you have cited random situations happening with some of your end-users, but situations you can’t repeat.

    Problem # 1 happens to only 2 users and not always. Do you know what those two users have in common in the way of system environment, Service Pack levels, and similar contextual characteristics that you and other users do not have in common?

    Problem #2 happens to 4 users, randomly. Is it frequent for each of those users, or such a random problem that some of the 4 users have reported it only once? What do the users do when they get the problem? (Re-start your program, restart Word, re-boot the system?) What do the users say happened before the problem?

    You mentioned event handlers for tick events and timers. Could you learn anything by adding logging to the event handlers so that you see what happens in a normal event, and whether there is a timing issue?

    Calvin asked you for a repro to share with the Forum viewers. This would be worthwhile and valuable if you can build one that does repro the problems – any one of them. Since you don’t know how to reproduce the problems perhaps a simple demonstration application for others to try would suggest to them arenas they would identify as possible suspects.  

    Short of that, your best troubleshooting will be to identify what is common to the users with the problems that is distinct from the environment of the users who don’t have (or report) the problems.

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    Wednesday, September 7, 2011 2:58 PM
    Moderator
  • In case anyone have similar problem, I found out that using 

    Range.Font.Shading.BackgroundPatternColor

    instead of 

    Range.Shading.BackgroundPatternColor

    solves the problem.

    Monday, January 23, 2012 1:21 PM
  • Hi Calvin,

      I'm facing the same issue, not able to set pane visibility to true.

    Please give solution to the above mentioned problem.

    Thanks in advance


    Shallet

    Monday, July 22, 2013 7:16 AM
  • Hi Calvin,

      I'm facing the same issue, not able to set pane visibility to true.

    Please give solution to the above mentioned problem.

    Thanks in advance


    Shallet

    I am the threadstarter (just changed account when moved to Win8). I ended up with an ugly solution, but it works. I just added a bool flag - _visibleChangeEventHandlerIsActive that I set in each TaskPane eventhandler and check it before setting _taskPane.Visible = true. If I can't set it visible immediately, I start timer and set is there when event handler is not active. Something like that.

                    if (_taskPane != null)
                    {

                        // can't set pane visible when VisibleChanged eventhandler is active
                        if (!_visibleChangeEventHandlerIsActive)
                        {
                            _taskPane.Visible = value;
                        }
    else

       {    

                           _setVisibleTimer.Start();
                           _setVisible = value;

    }                

    }

    • Proposed as answer by Shallet Tuesday, July 30, 2013 5:52 AM
    Tuesday, July 23, 2013 8:08 AM
  • Hi Clavin,  

         private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                buttonControl = new ButtonUserControl();
                myCustomTaskPane = this.CustomTaskPanes.Add(buttonControl, "Button Task Pane");
                myCustomTaskPane.Visible = true;
                myCustomTaskPane.VisibleChanged += new EventHandler(taskPane_VisibleChanged);

            }

         private void taskPane_VisibleChanged(object sender, System.EventArgs e)
            {            
                if (myCustomTaskPane != null)
                {

                }

        }

    In this event handler i want to make custom task pane visible in full screen, so need to set 

    myCustomTaskPane.Visible = true;

    here i'm not able to add _visibleChangeEventHandlerIsActive.

    Please help me out.

    Thanks in Advance


    Shallet

    Tuesday, July 30, 2013 5:52 AM
  • Hi Clavin,  

         private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                buttonControl = new ButtonUserControl();
                myCustomTaskPane = this.CustomTaskPanes.Add(buttonControl, "Button Task Pane");
                myCustomTaskPane.Visible = true;
                myCustomTaskPane.VisibleChanged += new EventHandler(taskPane_VisibleChanged);

            }

         private void taskPane_VisibleChanged(object sender, System.EventArgs e)
            {            
                if (myCustomTaskPane != null)
                {

                }

        }

    In this event handler i want to make custom task pane visible in full screen, so need to set 

    myCustomTaskPane.Visible = true;

    here i'm not able to add _visibleChangeEventHandlerIsActive.

    Please help me out.

    Thanks in Advance


    Shallet

    You can't set taskPane.Visible inside VisibilityChanged event handler because it will lead to endless recursion. I don't get why you need to do this inside this event handler? If event handler is running this means that taskpane already becoming visible (or invisible). 

    My solution with bool field is for other events that also give error when any taskpane event handler is active.

    • Proposed as answer by Shallet Monday, August 5, 2013 10:18 AM
    Monday, August 5, 2013 8:08 AM