none
ContentControlOnEnter Does Not Trigger RRS feed

  • Question

  • I cannot get the ConentControlOnEnter event to fire.  It works fine with the ContentControlOnExit.

    I have done a lot of poking around without success.  If someone has any idea as to why this isn't working I would be really grateful if you could share the solution.

    Thank you.

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Linq; using Microsoft.Office.Tools.Word; using Microsoft.VisualStudio.Tools.Applications.Runtime; using Office = Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; using Microsoft.Office.Tools.Word.Extensions; using WordTools = Microsoft.Office.Tools.Word; using System.DirectoryServices; using System.Diagnostics; namespace CCF { public partial class ThisDocument { private string getControlNumber(DateTime dateValue) { string millisecond = (dateValue.Millisecond.ToString().Length == 3 ? dateValue.Millisecond.ToString() : (dateValue.Millisecond.ToString().Length == 2 ? "0" + dateValue.Millisecond.ToString().Length : "00" + dateValue.Millisecond.ToString().Length)); string second = ((dateValue.Second.ToString().Length == 2) ? dateValue.Second.ToString() : "0" + dateValue.Second.ToString()); string minute = (dateValue.Minute.ToString().Length == 2 ? dateValue.Minute.ToString() : "0" + dateValue.Minute.ToString()); string hour = (dateValue.Hour.ToString().Length == 2 ? dateValue.Hour.ToString() : "0" + dateValue.Hour.ToString()); string day = (dateValue.Day.ToString().Length == 2 ? dateValue.Day.ToString() : "0" + dateValue.Day.ToString()); string month = (dateValue.Month.ToString().Length == 2 ? dateValue.Month.ToString() : "0" + dateValue.Month.ToString()); string year = dateValue.Year.ToString(); return year + month + day + "-" + hour + minute + second + millisecond; } private void setControlNumber(string ControlNumber, string ControlNumberTag, WordTools.Bookmark BookmarkName) { Word.ContentControls DocCCs; // Find the primary Control Number content control DocCCs = this.SelectContentControlsByTag(ControlNumberTag); if (DocCCs.Count > 0) { foreach (Word.ContentControl cc in DocCCs) { // Make sure the bookmark exists before writing to it. if (this.Bookmarks.Exists(BookmarkName.Name.ToString())) { cc.LockContents = false; BookmarkName.Range.Text = ControlNumber; cc.LockContents = true; } } } } private void setDate(DateTime dateValue) { Word.ContentControls DocCCs; String DateTag = "dtDate"; // Find the primary date content control DocCCs = this.SelectContentControlsByTag(DateTag); if (DocCCs.Count > 0) { foreach (Word.ContentControl cc in DocCCs) { cc.Range.Text = dateValue.ToString(); } } } private SearchResult adQuery() { string UserName = Environment.UserName; string LDAPAddress = "LDAP://DC=domain,DC=com"; string SearchFilter = "(&(sAMAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(sAMAccountName=" + UserName + "))"; SearchResult searchResult; DirectoryEntry entry = new DirectoryEntry(LDAPAddress); DirectorySearcher searcher = new DirectorySearcher(entry); searcher.Filter = SearchFilter; searcher.PropertiesToLoad.Add("displayName"); searcher.PropertiesToLoad.Add("telephoneNumber"); searcher.PropertiesToLoad.Add("mobile"); searcher.PropertiesToLoad.Add("department"); searcher.PropertiesToLoad.Add("sAMAccountName"); searchResult = null; try { searchResult = searcher.FindOne(); } catch (Exception e) { MessageBox.Show(e.Message.ToString()); } return searchResult; } private void setChangeRequesterCreatorParameters() { SearchResult searchResult; searchResult = adQuery(); if (searchResult != null) { this.Requester.Range.Text = searchResult.Properties["DisplayName"][0].ToString(); this.PhoneNumber.Range.Text = searchResult.Properties["TelephoneNumber"][0].ToString(); this.ISUnit.Range.Text = searchResult.Properties["Department"][0].ToString(); } else { MessageBox.Show("Unable to obtain information from AD. Please make sure you are connected to the company domain."); } searchResult = null; } private void ThisDocument_Startup(object sender, System.EventArgs e) { // Calculate the next Control Number. DateTime dateValue = DateTime.Now; string ControlNumber = getControlNumber(dateValue); // Set the date of the Change Request setDate(dateValue); // Set the Control Number for each of the fields in the document. setControlNumber(ControlNumber, "txtControlNumber", this.ControlNumber); setControlNumber(ControlNumber, "txtControlNumberImplementation", this.ControlNumberImplementation); setControlNumber(ControlNumber, "txtControlNumberRollback", this.ControlNumberRollback); setControlNumber(ControlNumber, "txtControlNumberPostTest", this.ControlNumberPostTest); // Populate the document with the creators name, phone number and department. setChangeRequesterCreatorParameters(); } private void ThisDocument_Shutdown(object sender, System.EventArgs e) { } #region VSTO Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisDocument_Startup); this.Shutdown += new System.EventHandler(ThisDocument_Shutdown); this.ContentControlOnEnter += new Word.DocumentEvents2_ContentControlOnEnterEventHandler(this.ThisDocument_ContentControlOnEnter); this.ContentControlOnExit += new Word.DocumentEvents2_ContentControlOnExitEventHandler(this.ThisDocument_ContentControlOnExit); } private void ThisDocument_ContentControlOnEnter(Word.ContentControl ContentControl) { MessageBox.Show("ThisDocument_ContentControlOnEnter: " + ContentControl.Title); //Debug.Print("ThisDocument_ContentControlOnEnter: " + ContentControl.Title); //throw new NotImplementedException(); } private void ThisDocument_ContentControlOnExit(Word.ContentControl ContentControl, ref bool Cancel) { MessageBox.Show("ThisDocument_ContentControlOnExit: " + ContentControl.Title); //Debug.Print("ThisDocument_ContentControlOnExit: " + ContentControl.Title); //throw new NotImplementedException(); } #endregion } }


    Wednesday, April 3, 2013 2:57 AM

Answers

  • There were problems with the file format in Word 2007 that affected content control events. Quite possibly, that's what is interfering in this case.

    Have you tried opening the template in Word and saving to the newer version's file format (meaning "Compatibilty Mode" would disappear in the title bar, but it would still be a *.docx)?

    The other approach would be to compare the internal document.xml file in both "packages" to see if you can spot what the difference is an correct it manually.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 3, 2013 5:50 PM
    Moderator

All replies

  • I did a test with just a blank template and found that my methodology for connecting the event handlers works.  It must be something to do with the way I have things setup in my other template.

    I have the content controls in table cells for layout and then the document is protected as read only but I have selected areas where everyone can edit.  It doesn't matter whether the document is protected or not so I am leaning towards the content controls being in a table being the issue.  I'm going to test that theory next.

    Wednesday, April 3, 2013 5:57 AM
  • Ok, must be something that is messed with my original template.  The original template was born in Office 2007 and has been brought all the way up through the ranks to Office 2010 and then Office 2013.

    The fresh test template made in Office 2013 with a single table, two text content controls (one in each cell), protection and bookmarks for text placement works fine and as expected.

    I really don't want to have to rebuild this template from scratch, but it might be my only choice.

    Wednesday, April 3, 2013 6:13 AM
  • There were problems with the file format in Word 2007 that affected content control events. Quite possibly, that's what is interfering in this case.

    Have you tried opening the template in Word and saving to the newer version's file format (meaning "Compatibilty Mode" would disappear in the title bar, but it would still be a *.docx)?

    The other approach would be to compare the internal document.xml file in both "packages" to see if you can spot what the difference is an correct it manually.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 3, 2013 5:50 PM
    Moderator
  • Hi Cindy,

    Thank you for the response.

    I did save it as a Word 2013 document to get rid of the compatibility mode quite some time ago when I started moving into the VSTO space.  Initially there was VBA running this document.

    I looked at the document.xml's and I think by the time I figured out what difference is that is causing this issues I will have rebuilt the document from scratch.  It probably isn't a bad idea to upgrade the document by rebuilding it.

    I have already got the layout done, now I am just adding the content controls then I just have to wire things up.  Hopefully once I am done the controls the wiring works properly this time.

    It is a 6 page document with a lot of content controls and formatting so the document.xml is quite extensive.

    Monday, April 8, 2013 2:05 AM