简体   繁体   中英

c# “this method or property is not available because the current selection partially covers a plain text content control.”

I am having an issue with a method (AddProcedure(Procedure procedure, Word.Table table, int procedureCol, int initialsCol, int dateCol) ) in a Microsoft Word document-level customization that adds rows (ie, procedure) to a table.

Each row contains a description in column 1, a dropdown list content control in column 7, and a date picker content control in column 9. Which procedures that get added should be dependent on the value of the string member "engagementType".

When engagementType is initialized with a sample engagementType (ie, "Audit"), everything works perfectly. But when engagementType is initialized with an empty string and then assigned the value "Audit" in an event handler which then calls the SetupProcedures method then AddProcedure method (method in question, I believe) only one row is added with the description in column 1 and then fails in column 7 when attempting to add the dropdown list content control.

Does anyone know why this is happening?

Here is my code:

public partial class ThisDocument
    {
        #region Fields

        private string engagementType = "Audit";

        #endregion Fields

        #region Events

        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            PartnerDropdownListControl.Validated += PartnerDropdownListControl_Validated;
            UpdateUI();
        }

        private void PartnerDropdownListControl_Validated(object sender, EventArgs e)
        {
            MessageBox.Show("Partner Dropdown list Validated event handler called.");

            if (PartnerDropdownListControl.Text == "JDI")
            {
                engagementType = "Audit";
                SetupProcedures(engagementType);
            }
            else
            {
                engagementType = "Review";
                SetupProcedures(engagementType);
            }
            // throw new NotImplementedException();
        }


        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {

        }


        #endregion Events

        #region Methods

        private void UpdateUI()
        {
            ActiveWindow.View.TableGridlines = false;
            if(PartnerDropdownListControl.DropDownListEntries.Count == 0) PopulatePartnerDropdownList();
            if(PreparerDropdownListControl.DropDownListEntries.Count == 0) PopulatePreparerDropdownList();
            SetupProcedures(engagementType);
        }

        private void PopulatePartnerDropdownList()
        {
            RouteManager routeSheet = new RouteManager();

            foreach (string partner in routeSheet.Partners()) 
            {
                PartnerDropdownListControl.DropDownListEntries.Add(partner, partner);
            }
        }

        private void PopulatePreparerDropdownList()
        {
            RouteManager routeSheet = new RouteManager();

            foreach(string preparer in routeSheet.Preparers())
            {
                PreparerDropdownListControl.DropDownListEntries.Add(preparer, preparer);
            }
        }

        private void SetupProcedures(string engType)
        {
            Procedures procedures = new Procedures();
            Word.Table proceduresTable = this.Tables[2];

            int proceduresColumn = 1;
            int initialsColumn = 7;
            int dateColumn = 9;

            switch(engType)
            {
                case "Audit":
                    foreach(Procedure procedure in procedures.AuditProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                case "Review":

                    foreach (Procedure procedure in procedures.ReviewProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                case "Compilation":

                    foreach (Procedure procedure in procedures.CompilationProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                default:
                    break;
            }

            // Add botton border to each cell in the sign-off initials column & sign-off date column.
            for (int row = proceduresTable.Rows.Count; row > 0; row--)
            {
                proceduresTable.Cell(row, initialsColumn).Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                proceduresTable.Cell(row, dateColumn).Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }

            // Set the table Column widths.
            proceduresTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow);
        }

        private void AddProcedureRow(Procedure procedure, Word.Table table, int procedureCol, int initialsCol, int dateCol)
        {
            table.Rows.Add();
            int row = table.Rows.Count;

            RouteManager routeSheet = new RouteManager();

            DropDownListContentControl signOffDropdownList;
            string signOffDropdownListName = $"signOffDropdownList{row}";
            DatePickerContentControl signOffDatePicker;
            string signOffDatePickerName = $"signOffDatePicker{row}";

            table.Cell(row, procedureCol).Range.Text = procedure.Description;

            try
            {
                MessageBox.Show("Try block called.");
                signOffDropdownList = Controls.AddDropDownListContentControl(table.Cell(row, initialsCol).Range, signOffDropdownListName);
            }
            catch (Exception)
            {
                MessageBox.Show("Catch block called.");
                throw;
            }
            signOffDropdownList.PlaceholderText = "Sign-Off";

            foreach (string employee in routeSheet.Employees())
            {
                signOffDropdownList.DropDownListEntries.Add(employee, employee);
            }

            signOffDatePicker = Controls.AddDatePickerContentControl(table.Cell(row, dateCol).Range, signOffDatePickerName);
            signOffDatePicker.PlaceholderText = "Sign-Off Date";
        }

        #endregion Methods

        #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(this.ThisDocument_Startup);
            this.Shutdown += new System.EventHandler(this.ThisDocument_Shutdown);

        }

        #endregion

    }
}

I got it to work. I needed to select a range outside of the partnerdropdownlist content control before calling the setuprocedures method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM