none
Word 2007 - Event Handling on content controls RRS feed

  • Question

  • I have developed an Word 2007 document level project using Visual Studio 2008.  It has been working successfully until now on all user's pcs. 

    I am using code to open a form for input when a user clicks on a content control.  I am doing this on the content control on enter event.  It is still working ok on my development pc but causes my test pc to crash while the form is displayed (I put a breakpoint to determine that the form is indeed loading) but the program crashes before the user can enter any input.  On another user pc, the form displays but then locks up.

    The error message I have finally determined thru remote debugging is an access violation reading location 0x00000018; 

    It seems that the error is in the event handler - after the event code runs but before exiting -- could there have been some update that caused this -- I'm at a total loss about how to proceed.  Any suggestions??


    Jo Crews

    Saturday, November 24, 2012 2:26 AM

Answers

  • Hi Cindy, the re-locking of the content controls while in the ContentControlOnEvent was the problem.  I am leaving the content control unlocked until the contentcontrol has been exited - so far, the error message has not reappeared.

    Thanks for your help!


    Jo Crews

    Monday, November 26, 2012 4:22 PM

All replies

  • Hi Jo

    Please share the code with us that's causing the crash.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, November 24, 2012 10:46 AM
    Moderator
  • Hi Cindy,  here is my code for the event handler:

           

    private void ThisDocument_ContentControlOnEnter(Word.ContentControl ContentControl)

            {

               

    try


                {

                    Word.

    Application wordApp = Globals.ThisDocument.Application;

                    Word.

    Document wordDoc = wordApp.ActiveDocument;

                    Word.

    Selection sln = wordApp.Selection;

                   

    clsDraft ThisDraft = clsDraft.Instance;

                   

    if (sln.ParentContentControl == null)

                    {

                       

    return;

                    }

                   

    if (ThisDraft.CurrentAction == "AddingSubsections" || ThisDraft.CurrentAction == "In process")

                    {

                       

    return;

                    }

                   

    if (ContentControl.Type == Word.WdContentControlType.wdContentControlGroup)

                    {

                       

    return;

                    }

                   

    if (ContentControl.Title.Length > 7)

                    {

                       

    if (util.StringMethods.Right(ContentControl.Title, 6) == "Header")

                        {

                           

    return;

                        }

                       

    if (ContentControl.Title == "SalaryHeadingYear1" || ContentControl.Title == "SalaryHeadingYear2")

                        {

                            wm.

    clsContentControls.AllowDelete(wordApp, wordDoc, "SalaryHeadingYear1");

                            wm.

    clsContentControls.AllowDelete(wordApp, wordDoc, "SalaryHeadingYear2");

                           

    return;

                        }

                    }

                   

    if (ThisDraft.CurrentAction == "Pending" || ThisDraft.CurrentAction == "Modify")

                    {

                       

    if (ContentControl.Tag == null)

                        {

                           

    return;

                        }

                       

    if (ContentControl.Tag.Length > 0)

                        {

                           

    string strSectionType = util.StringMethods.Left(ContentControl.Tag, 2);

                           

    string strSectionID = ContentControl.Tag;

                           

    switch (strSectionType)

                            {

                               

    case "RS":

                                   

    frmEditRS objfrmEditRS = new frmEditRS();

                                    objfrmEditRS.LoadValues(strSectionID);

                                    objfrmEditRS.ShowDialog();

                                    objfrmEditRS.BringToFront();

                                   

    break;

                               

    case "EH":

                                   

    frmEditEH objfrmEditEH = new frmEditEH();

                                    objfrmEditEH.LoadValues(strSectionID);

                                    objfrmEditEH.ShowDialog();

                                    objfrmEditEH.BringToFront();

                                   

    break;

                               

    case "AP":

                                   

    if (ThisDraft.EditSaved == false && sln.ParentContentControl != null)

                                    {

                                       

                                    

    frmEditAP objfrmEditAP = frmEditAP.OnlyInstance;

                                        objfrmEditAP.LoadValues(strSectionID);

                                        objfrmEditAP.Show();

                                        objfrmEditAP.BringToFront();

                                    }

                                   

    break;

                            }

                        }

                    }

                }

               

    catch (Exception ex)

                {

                   

    MessageBox.Show("Unable to edit the data blocks in this section.;  This error has been logged with Information Systems.", "Process Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    util.

    Notification.LogErrors(ex, "Appro Bill Drafting Program", "ThisDocument - ContentControlOnEnter|Value Passed: " + ContentControl.Title + "; ");

                   

    return;

                }

            }


    Jo Crews

    Sunday, November 25, 2012 1:18 AM
  • Hi Jo

    Very difficult to read code like this - it would help if you would use the "Code" button in the message toolbar...

    As this is a document-level solution, I would not expect the following:

     Word.Application wordApp = Globals.ThisDocument.Application;
     Word.Document wordDoc = wordApp.ActiveDocument;
     Word.Selection sln = wordApp.Selection;
    

    The events in a document-level solution should refer only to controls in that document. You're asking the event to deal with a selection in the currently active document, which could theoretically be something else, entirely.

    What are you trying to accomplish with: if (sln.ParentContentControl ==null)

    I don't know that this is the cause of the problem you're seeing, but I'd try removing these things and see if that has any effect...

     

    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, November 25, 2012 4:33 PM
    Moderator
  • Sorry about the code formatting Cindy -- I haven't ever posted code to a blog before - didn't know how to remove the blank lines... maybe this is a little better...

    As far as the objects - wordApp, wordDoc, and sln - I use these to pass parameters to a DLL that I created that contains many word code methods and procedures.  I've developed several shared VSTO document level apps that use this 'word methods dll'  and have not had encountered any problems in the two years I've been utilizing them.

    The line of code referring to the ParentContentControl == null was placed in code to prevent an error if the selected content control was not contained in a "grouping of content controls" for a particular section of text -

    I suspect that my problems may be caused by the locking and unlocking of the content controls - once the user selects the appropriate 'choice' offered on the input screen, the content control is locked again.  This happens while the ContentControlOnEvent event is still active.

    I will continue working on this today and am going to remark code about in blocks to see if I can locate the problem.

    Thanks for your help - I will keep you posted....

            

    using wm = wordMethods; //dll that contains various word methods

    private void ThisDocument_ContentControlOnEnter(Word.ContentControl ContentControl) { try { Word.Application wordApp = Globals.ThisDocument.Application; Word.Document wordDoc = wordApp.ActiveDocument; Word.Selection sln = wordApp.Selection; clsDraft ThisDraft = clsDraft.Instance; if (sln.ParentContentControl == null) { return; } if (ThisDraft.CurrentAction == "AddingSubsections" || ThisDraft.CurrentAction == "In process") { return; } if (ContentControl.Type == Word.WdContentControlType.wdContentControlGroup) { return; } if (ContentControl.Title.Length > 7) { if (util.StringMethods.Right(ContentControl.Title, 6) == "Header") { return; } if (ContentControl.Title == "SalaryHeadingYear1" || ContentControl.Title == "SalaryHeadingYear2") { wm.clsContentControls.AllowDelete(wordApp, wordDoc, "SalaryHeadingYear1"); wm.clsContentControls.AllowDelete(wordApp, wordDoc, "SalaryHeadingYear2"); return; } } if (ThisDraft.CurrentAction == "Pending" || ThisDraft.CurrentAction == "Modify") { if (ContentControl.Tag == null) { return; } if (ContentControl.Tag.Length > 0) { string strSectionType = util.StringMethods.Left(ContentControl.Tag, 2); string strSectionID = ContentControl.Tag; switch (strSectionType) { case "RS": frmEditRS objfrmEditRS = new frmEditRS(); objfrmEditRS.LoadValues(strSectionID); objfrmEditRS.ShowDialog(); objfrmEditRS.BringToFront(); break; case "EH": frmEditEH objfrmEditEH = new frmEditEH(); objfrmEditEH.LoadValues(strSectionID); objfrmEditEH.Show(); objfrmEditEH.BringToFront(); break; case "AP": if (ThisDraft.EditSaved == false && sln.ParentContentControl != null) { //frmEditAP objfrmEditAP = new frmEditAP(); frmEditAP objfrmEditAP = frmEditAP.OnlyInstance; objfrmEditAP.LoadValues(strSectionID); objfrmEditAP.Show(); objfrmEditAP.BringToFront(); } break; } } } } catch (Exception ex) { MessageBox.Show("Unable to edit the data blocks in this section.; This error has been logged with Information Systems.", "Process Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); util.Notification.LogErrors(ex, "Appro Bill Drafting Program", "ThisDocument - ContentControlOnEnter|Value Passed: " + ContentControl.Title + "; "); return; } } //-------------------------------------------------------------------



    Jo Crews

    Monday, November 26, 2012 3:10 PM
  • Hi Cindy, the re-locking of the content controls while in the ContentControlOnEvent was the problem.  I am leaving the content control unlocked until the contentcontrol has been exited - so far, the error message has not reappeared.

    Thanks for your help!


    Jo Crews

    Monday, November 26, 2012 4:22 PM
  • Hi Jo

    Sorry I wasn't able to look at this yesterday. Yes, there are definitely issues with changing state of content controls while within an event! Glad you managed to find that.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, November 27, 2012 8:27 AM
    Moderator