This project has moved and is read-only. For the latest updates, please go here.

DocX InsertDocument() (InvalidOperationException: sequence contains no elements)

Dec 31, 2013 at 7:42 AM
Edited Dec 31, 2013 at 4:20 PM
Hi!

I'm use DocX v1.0.0.13
I have a problem when run the procedure InsertDocument()
I need to create multiple documents in one.

Fragment code:
...
docContainer = DocX.Create(@"d:\11.docx");
foreach(....) 
{
template = DocX.Load(GetTemplate());
template.ReplaceText(searchText, paramsCollection[pair.Key].Value, false, System.Text.RegularExpressions.RegexOptions.IgnoreCase, null, null, MatchFormattingOptions.ExactMatch);
// here is exception!
docContainer.InsertDocument(template);
}

...
trace stack (WebForms):
[InvalidOperationException: sequence contains no elements]
   System.Linq.Enumerable.Last(IEnumerable`1 source) +733104
   Novacode.DocX.merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote) +3062
   Novacode.DocX.InsertDocument(DocX remote_document) +1927
   DocumentOut.Process() in c:\inetpub\wwwroot\Test\App_Code\DocOut.cs:224
   Doc_Search.btnFind_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Test\Doc\Search.aspx.cs:70
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   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) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
How to generate a multiple document ?

Thanks
Jan 1, 2014 at 9:14 AM
Temporarily problem solved so:
instead docContainer = DocX.Create() using docContainer = DocX.Load(), that is, use a non-empty file...
Jan 1, 2014 at 11:09 PM
Hi,
Doing much the same thing and am hitting the same error. I've modified my code to be
            var doc = DocX.Create(file);
            var h1= new Formatting();
            h1.Size = 24;
            h1.Bold = true;
            doc.InsertParagraph("Index of Care", false, h1);
            doc.Save();

            var docContainer = DocX.Load(file);

            string file1 = Server.MapPath(@"~\Cases\Reports\GeneratedReports") + @"\Stage1Report-" + MainRecordID + ".docx";
            var doc1 = Stage1Report(file1);
            docContainer.InsertDocument(doc1);
and it fails with the same error. Which would indicate that I've misunderstood your temporary solution.

Any help gratefully received. :)
Feb 6, 2014 at 2:38 PM
Edited Feb 6, 2014 at 2:46 PM
I have the same error. If someone knows how to fix this I would be very grateful.
    Public Sub Merge(ByVal arrayList As List(Of String))

        Using documentToCreate As DocX = DocX.Load(arrayList(0))
            For Each allItem In arrayList.Skip(1)
                documentToCreate.InsertParagraph().InsertPageBreakAfterSelf()
                Dim documentToMergeIn = DocX.Load(allItem)
                documentToCreate.InsertDocument(documentToMergeIn) /// Exception "An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll"
            Next
            documentToCreate.SaveAs("alldoc.docx")
        End Using

    End Sub
Version DocX.dll : 1.0.1.13
Exception snapshot: StackTrace
   в System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
   в Novacode.DocX.merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote)
   в Novacode.DocX.InsertDocument(DocX remote_document)
   в BarcodeVB.Module1.Merge(List`1 arrayList) в C:\Users\a.verboloz\Documents\WorkSpace\Barcode\BarcodeVB\BarcodeVB\Module1.vb:строка 15
   в BarcodeVB.Module1.Main() в C:\Users\a.verboloz\Documents\WorkSpace\Barcode\BarcodeVB\BarcodeVB\Module1.vb:строка 72
   в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   в System.Threading.ThreadHelper.ThreadStart()