ArgumentOutOfRangeException on Word.Open(string)

Can anyone shed some light on what might cause the following to be thrown on a call to Word.Open(string) ?

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get\_Item(Int32 index) +91
Aspose.Word.File.c.a(z A\_0) +68
Aspose.Word.File.t.b(Int32 A\_0) +40
Aspose.Word.File.z.a(Int32 A\_0) +58
Aspose.Word.File.z.a(s A\_0) +383
Aspose.Word.File.z.a(Int32 A\_0, Int32 A\_1) +480
Aspose.Word.File.z.b() +117
Aspose.Word.File.t.a(bu A\_0) +1377
Aspose.Word.Document.a(bu A\_0) +125
Aspose.Word.Document…ctor(String fileName) +81
Aspose.Word.Word.Open(String fileName) +25
ClientDemo.SiteDocument.UpdateDocFile() in c:\documents and settings\djayne\vswebcache\siteaddr.com\Clientdemo\sitedocument.cs:370

Oddly the function leading to this exception succeeds when called from one page but not when called from another. I’ve double-checked that I’m passing in valid file paths (invalid paths would throw a different exception anyway). All appearances lead me to believe there’s something in the doc file causing Aspose.Word to choke (and the error only occurs when opening a file which has already been alterred and saved previously by Aspose.Word). Anyway, I can’t tell at all what’s going on due to obfuscation.

I was able to resolve this issue by reverting to Aspose.Word ver 1.3.1.

I also verified that once the document had been altered by Aspose.Word 1.4.3 in a certain way, it was no longer openable by Word.Open(String).

If I have time later I will attempt to replicate the error in a short example program. In a nutshell, the document was opened, had a section containing a merge field cloned from another document added to it via Document.Sections.Add(Section), protection set to AllowOnlyFormFields and saved. Another function then opened the document, unprotected it, merged an image into the merge field, set protection to AllowOnlyFormFields and saved it. After this, Aspose.Word was unable to open the doc.

Hi David,

Can you please email the documents you used to word@aspose.com for examination. If those are the steps then I will replicate them in my own test, but if you come up with your program that replicates the error it would be nice.

Roman,

I’ve successfully replicated the error and mailed that code along with doc files to word@aspose.com.

It’s very likely this issue can be resolved by using methods of DocumentBuilder (which was not yet available when development on my site began), but I haven’t tried it yet.

David Jayne

I’ve broken this down even further. From my experiments it seems that adding a section then deleting it creates a document aspose word cannot open. Try the following code. Referencing Aspose.Word 1.4.7 it bombs where the comment says it will. Referencing 1.3.1 it works fine.

using System;
using Aspose.Word;

namespace ReplicateAsposeError
{
    class Test
    {
        // paths to docs (any 2 word docs will work)
        private static string OrigDocPath = @"C:\code\test\Aspose\test.doc";
        private static string AppendDocPath = @"C:\code\test\Aspose\test2.doc";

        // create Section object from append doc
        private static Word AspWord = new Word();
        private static Document AppendDoc = AspWord.Open(AppendDocPath);
        private static Section NewAppendSection = AppendDoc.Sections[0].Clone();

        [STAThread]
        static void Main(string[] args)
        {
            AddSection();
            DeleteSection();

            // now bombs on open
            Document BadDocument = AspWord.Open(OrigDocPath);
        }

        private static void AddSection()
        {
            Document OrigDoc = AspWord.Open(OrigDocPath);
            Console.WriteLine("Document has {0} sections before add", OrigDoc.Sections.Count);
            OrigDoc.Sections.Add(NewAppendSection);
            Console.WriteLine("Document has {0} sections after add\n", OrigDoc.Sections.Count);
            OrigDoc.Save(OrigDocPath);
        }

        private static void DeleteSection()
        {
            Document OrigDoc = AspWord.Open(OrigDocPath);
            Console.WriteLine("Document has {0} sections before delete", OrigDoc.Sections.Count);
            Console.WriteLine("Deleting section {0}", OrigDoc.Sections.Count - 1);
            Section OrigAppendSection = OrigDoc.Sections[OrigDoc.Sections.Count - 1];
            OrigAppendSection.Range.Delete();
            Console.WriteLine("Document has {0} sections after delete\n", OrigDoc.Sections.Count);
            OrigDoc.Save(OrigDocPath);
        }
    }
}

Hi David,

Thanks for your detailed report. I’ve tracked this down to Aspose.Word writing the size of the stylesheet into a wrong field in the binary file. This is now fixed, please download latest Aspose.Word 1.4.8.

By the way, there are some suggestions I can provide looking at your code. Not this example, but the one you emailed to me.

  1. I don’t normally save the output file into the same file name as the original file. It just allows to keep the original template always intact.

  2. You can improve performance by moving the section from one document to another rather than cloning it. In your case the section is small and performance gain will not be big.

  3. You don’t need to unprotect and protect the document to modify it. Aspose.Word will always allow to change the document regardless of whether it is protected or not.

  4. It seems strange that you create a ready document once in the main and then do the same thing again in the worker method.

Thanks for addressing this promptly Roman.

Also thanks for the advice. #'s 2 & 3 are duly noted. 1 & 4, while they do seem odd, in the grand scheme of the app I’m working on make sense.

Thanks again.

David Jayne