We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Operation is not valid due to the current stat of the object

We recently went from Aspose.words 13.8 to 14.6. Intermittently we are generating the error "Operation is not valid due to the current stat of the object."


Stack Trace (not a lot of help, I know)
at ? .()
at ? .(Boolean , Boolean )
at ? . ? ()
at ? .()
at ? .? ()
at ? .(Field )
at ? .(Field )
at ? . (? )
at ? .(? )
at ? .(? , ? )
at ? . ()
at ? .( , Boolean )
at ? . ()
at ? .(? )
at ? .(? , ? )
at ? . ()
at ? .( , Boolean )
at Aspose.Words.Reporting.MailMerge.( )
at KCMS.DAL.MailMerge.MakeASingleFile(Object FileNumberOrReferralId, DataSet& dsMerge, String rootUncKey, FileInfo templateFileInfo, String dateTimeSuffix, String separateDocID, String documentCategory, String includeInCourtTransfer, String eventCode, Int64 EventIndexOrVCompSequenceNumber) in f:\wwroot\CAContraCosta\App_Code\DAL\MailMerge.cs:line 3160
at KCMS.DAL.MailMerge.CreateCaseDocsFromEvent(String TemplateFile, String FileNumber, DateTime EventDate, Int64 DocketSequence, Int64 EventIndex, Boolean IncludeCodefendants, String documentCategory, String witnesses, Int64 ProgramSequenceNumber, String SubpoenaFlag, String includeInCourtTransfer, String eventCode, Boolean SaveIndividualWitnessDocs, String docketCode, DateTime docketDate, String CriminalHistoryIds, String JuryInstructionIDs, String SelectedChargeUIDs, String SelectedCodefendants, List1& uncList) in f:\wwroot\CAContraCosta\App_Code\DAL\MailMerge.cs:line 958</div><div> at Core_EventEntry.GenerateDocuments(TblCtEventCodes& eventCode, TblCsEvents& newCaseEvent, String& nextDocket, String& strSelectedWitnessIDs, String& strSelectedWitnessIDsForDocument, DateTime& nextDocketDate, Int64& intDocketSequenceNumber, Dictionary2& witnessEmailsForDocument, TblCtSystemValues& objSysValues) in f:\wwroot\CAContraCosta\Core\EventEntry.aspx.cs:line 7579
at Core_EventEntry.AddCaseEvent() in f:\wwroot\CAContraCosta\Core\EventEntry.aspx.cs:line 4604

Unfortunately the line number in MakeASingleFile refers to the closing bracket in the method, so I don’t know where the error is generated.

I made an assumption that it was coming from the doc.Save method, so I put in the doc.UpdatePageLayout() workaround mentioned in a post on the same error in Aspose.pdf. No luck, error still occurs.

I have attached the document template “(DRAFT)Complaint.docx” and a text file with method that is calling the merge.

Hi Joe,


Thanks for your inquiry.

I would suggest you please upgrade to the latest version of Aspose.Words for .NET 15.8.0 and see how it goes on your side. I hope, this helps. You can download it from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

In case the problem still remains, please create a standalone simple console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing. As soon as you get this simple application ready, we will start investigation into your issue and provide you more information.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

I do not see a version 15.8. The latest Aspose.total has version 15.7 of Aspose.words.


Please advise.

I have found that if I put quotation marks in one of my merge field, the error is generated. Remove it, and the error goes away. It does not appear to be a problem in just any merge field, I have modified my data to include double quotes in other fields brought into the document and this does not cause the error.


Regardless, I have take the latest Aspose.words (15.7). I can no longer compile my site. I receive these errors:

f:\wwwroot\PBK54\App_Code\clsMailMergeHandlers.cs(12): error CS0246: The type or
namespace name ‘IFieldMergingCallback’ could not be found (are you missing a us
ing directive or an assembly reference?)
f:\wwwroot\PBK54\App_Code\clsMailMergeHandlers.cs(14): error CS0246: The type or
namespace name ‘FieldMergingArgs’ could not be found (are you missing a using d
irective or an assembly reference?)
f:\wwwroot\PBK54\App_Code\clsMailMergeHandlers.cs(105): error CS0246: The type o
r namespace name ‘ImageFieldMergingArgs’ could not be found (are you missing a u
sing directive or an assembly reference?)

Please advise.

Hi Joe,


Thanks for your inquiry. It is great you were able to resolve first problem on your end. Secondly, please add following statement at the top of clsMailMergeHandlers.cs file:

using Aspose.Words.MailMerging;

I hope, this helps.

Best regards,

Ok, after putting Aspose.Words 15.7 in place, I no longer get the error if I have quote marks in my merge data. But I notice that this particular merge field is embedded in an IF statement in the word doc (see the template I attached originally to the post). It looks like Aspose is not parsing the quote marks out correctly and truncates the data if there are quotes.


The merge field in the template is called Chg_ChargeLanguage.

Hi Joe,


Thanks for your inquiry. I have extracted the IF field containing “Chg_ChargeLanguage” merge field into a new document (see attached in.docx). I was unable to observe any issue when executing the following simple code:

Document doc = new Document(MyDir + @“in.docx”);

doc.MailMerge.Execute(new string[] { "Chg_ChargeLanguage" }, new object[] { "test with long text ..." });

doc.Save(MyDir + @"15.8.0.docx");


Could you please create a standalone simple console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing. As soon as you get this simple application ready, we will start further investigation into your issue and provide you more information.

Best regards,

Using your code, I modified the merge fields and data, and now I am back to where I was before, getting the “Operation is not valid due to the current state of the object”.

Here is my complete code:

using Aspose.Words;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAsposeWords157IfWithQuotes
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document(“in.docx”);

        doc.MailMerge.Execute(new string[] { "Chg_UsageType", "Chg_ChargeLanguage" }, new object[] { "C", "Double Quotes are right here \"quoted\" and this text should not be visible" });

        doc.Save("out.docx", SaveFormat.Docx);

        System.Diagnostics.Process.Start("out.docx");
    }
}

}

I am using your in.docx file and using version 15.7.0.0 of Aspose.words.

Sorry, that didn’t format the way I wanted. Attached is my project.

Hi Joe,


Thanks for the additional information.

While using the latest version of Aspose.Words i.e. 15.8.0, we managed to reproduce this issue on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-12486. Your thread has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

Has there been a fix made for this yet?

Hi Joe,


Thanks for being patient. Regarding WORDSNET-12486, our product team has completed the analysis of this issue and the root cause has been identified. Currently, they are on the final stage of reviewing/testing all code changes that were done in the scope of this issue. We will inform you via this thread as soon as your issue is resolved. We apologize for any inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-12486) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issue is fixed using the simple example app I attached previously, but not in the application where we are actually using it.


The new project I have attached is closer to what it really happening in our application. I think the issue may be with the DocumentBuilder.InsertHTML that we have in our field merging callback. The data in question is HTML data.

By manipulating the content inside ChgLangText.txt file, you can get the issue to show itself by adding double quotes.

Hi Joe,


Thanks for your inquiry. But, I can successfully run your project with Aspose.Words for .NET 15.10.0 on my end. Your project does not throw any exception. Please elaborate your inquiry further. You mentioned that the problem occurs when manipulating “ChgLangText.txt” file, therefore please share updated file which generates this error. If the problem is not exception but the generated output itself, please create screenshot highlighting the problematic areas in the generated out.docx and attach it here for our reference. We will investigate the issue further on our end and provide you more information.

Best regards,

Please see the attached Word document for clarifying screen shots.



You are correct, there is no exception thrown, the issue is that the output is not what is expected. Content following quoted text in HTML data when it is contained in an IF statement. Place the same merge field outside of an IF statement and the text is rendered correctly.

Hi Joe,


Thanks for the additional information. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12717. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Awais, has there been any change in the status of this issue? I have customers waiting on a resolution.

Hi Joe,

Thanks for your inquiry. Regarding WORDSNET-12717, our product team has completed the work on your issue and has come to a conclusion that this issue and the undesired behaviour you're observing is actually not a bug. So, we have closed this issue as 'Not a Bug'.

The problem occurs because you insert HTML data manually inside IF field in the IFieldMergingCallback.FieldMerging method and inserted quotes are treated as part of IF field. You can replace quotation mark (") with double apostrophe ('') before insert as workaround:

...
builder.InsertHtml(htmlString.Trim().Replace(""", "''"), false);
...	

Hope, this helps.

Best regards,