Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jan 14, 2021 12:06 am

I need to dynamically set the text for my TextFormField. I keep a field name in FieldText or Name. Based on the FieldText or Name, I would populate correct text for my field. Every time, I set formField.Text, I got an exception.
Code: Select all
...
                foreach (Paragraph paragraph in section.Body.Paragraphs)
                {
                    ProcessParagraph(report, paragraph);
                }
...
        private void ProcessParagraph(DocReport report, Paragraph paragraph)
        {
            foreach (DocumentObject child in paragraph.Items)
            {
                Type t = child.GetType();

                switch (child.DocumentObjectType)
                {
                    case DocumentObjectType.StructureDocumentTagInline: //checkbox
                        StructureDocumentTagInline tag = child as StructureDocumentTagInline;
                        if (tag.SDTProperties.SDTType  == SdtType.CheckBox && tag.SDTProperties.Tag != null)
                        {
                            SdtCheckBox scb = tag.SDTProperties.ControlProperties as SdtCheckBox;
                            var result1 = PopulateText(report, tag.SDTProperties.Tag);
                            bool boolResult1 = false;
                            bool.TryParse(result1, out boolResult1);
                            scb.Checked = boolResult1;
                        }
                        break;
                    case DocumentObjectType.CheckBox:
                        CheckBoxFormField checkBox = child as CheckBoxFormField;
                        var newCheckboxName = LongToShortName(checkBox.Name);
                        var result = PopulateText(report, newCheckboxName);
                        bool boolResult = false;
                        bool.TryParse(result, out boolResult);
                        checkBox.Checked = boolResult;
                        break;
                    case DocumentObjectType.TextFormField:
                        TextFormField formField = child as TextFormField;
                        if (formField.FieldText.Trim().Length > 0)
                        {
                            formField.Text = PopulateText(report, formField.FieldText);
                        }
                        else if (formField.Name.Trim().Length > 0)
                        {
                            var newTextFieldName = LongToShortName(formField.Name);
                            formField.Text = PopulateText(report, newTextFieldName);
                        }
                        //formField.FieldText = formField.Text;
                        break;
                    case DocumentObjectType.TextBox:
                        Debug.WriteLine("TextBox");
                        break;
                    default:
                        Debug.WriteLine("Unknow--" + child.DocumentObjectType.ToString());
                        break;
                }

            }
            paragraph.Text = PopulateText(report, paragraph.Text);
        }


here's the exception:

Collection was modified; enumeration operation may not execute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Source Error:


Line 131: private void ProcessParagraph(DocReport report, Paragraph paragraph)
Line 132: {
Line 133: foreach (DocumentObject child in paragraph.Items)
Line 134: {
Line 135: Type t = child.GetType();

Source File: C:\setworks\SW\SetWorks.Reporting\MergeReport\Doc\Services\DocReportingService.cs Line: 133

Stack Trace:


[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +54
System.Collections.Generic.Enumerator.MoveNextRare() +30
System.Collections.Generic.Enumerator.MoveNext() +65
SetWorks.Reporting.MergeReport.Doc.Services.DocReportingService.ProcessParagraph(DocReport report, Paragraph paragraph) in C:\setworks\SW\SetWorks.Reporting\MergeReport\Doc\Services\DocReportingService.cs:133
SetWorks.Reporting.MergeReport.Doc.Services.DocReportingService.GenerateReport(DocReportConfiguration config) in C:\setworks\SW\SetWorks.Reporting\MergeReport\Doc\Services\DocReportingService.cs:81
SETWorks.Home.Reports.MergeReporting.MergeReport.DoDocReport(DocReportConfiguration config, DocumentViewerControl dvFileViewer) in C:\setworks\SW\SETWorks\Home\Reports\MergeReporting\MergeReport.aspx.cs:185
SETWorks.Home.Reports.MergeReporting.MergeReport.ProcessReport(Object sender, EventArgs eventArgs) in C:\setworks\SW\SETWorks\Home\Reports\MergeReporting\MergeReport.aspx.cs:163
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +109
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +31
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3480




If I traverse section.Body.FormFields to find the TextFormField, I can successfully set the text but it does not appear in the document. Here's the code that I've done that:

Code: Select all
                //foreach(FormField f in section.Body.FormFields)
                //{
                //    if (f.DocumentObjectType == DocumentObjectType.TextFormField)
                //    {
                //        if (f.FieldText.Trim().Length > 0)
                //        {
                //            var result = PopulateText(report, f.FieldText);
                //            f.Text = result;
                //        }
                //        else if (f.Name.Trim().Length > 0)
                //        {
                //            var newTextFieldName = LongToShortName(f.Name);
                //            var result = PopulateText(report, newTextFieldName);
                //            f.Text = result;
                //            //f.Name = result;
                //        }
                //    }
                //}


Please point me to the right direction. How can I set the text and make it appears in the Word Document.

Thank you very much.
Thao

ThaoBond
 
Posts: 2
Joined: Wed Sep 16, 2020 12:02 am

Thu Jan 14, 2021 9:59 am

Hello,

Thanks for your inquiry.
Regarding the error “Collection was modified; enumeration operation may not execute”, it should be caused by your code modifying the collection "paragraph.Items" somewhere. I suggest that you change the “foreach” loop to a “for” loop, as shown in the following code.
Code: Select all
                //foreach (DocumentObject obj in paragraph.Items)
                for (int i = 0; i < paragraph.Items.Count; i++)
                {
                    DocumentObject obj = paragraph.Items[i];
                    //...


As for your another issue, I made some changes based on your code (as shown below), and then simulated a Word file for testing, but did not reproduce your issue. To help us investigate further, please provide your source file and a runnable project that could reproduce your issue. You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Code: Select all
           
      foreach (Section section in doc.Sections)
            {
                foreach (FormField f in section.Body.FormFields)
                {
                    if (f.FieldText.Trim().Length > 0)
                    {
                       
                        f.Text = "new text";
                    }
                    else if (f.Name.Trim().Length > 0)
                    {
                        f.Text = "new text";
                    }
                }
            }

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Sat Jan 16, 2021 7:02 pm

Elena,
I just want to let you know that changing from foreach loop to for loop does get rid of exception. Thanks

ThaoBond
 
Posts: 2
Joined: Wed Sep 16, 2020 12:02 am

Mon Jan 18, 2021 1:35 am

Hello,

Thanks for your feedback.
If you encounter any issues related to our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.Doc