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 Jul 26, 2012 8:39 pm

I am receiving an exception when I attempt to call Table.Clone().

I have some code that looks roughly like this:
Code: Select all
        public void MacroBlockReplacementHelper(Spire.Doc.Document doc, CorrespondenceType.CorrespondenceTypes corrType, string macro)
        {
            IList<DocumentObject> pars = new List<DocumentObject>();

            Spire.Doc.Document subdoc = _templateLoader.LoadTemplate(TemplatePath);

            RunMacros(subdoc);
            foreach (Section section in subdoc.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                    pars.Add(obj);
            }

            TextSelection ts = doc.FindString(macro, false, true);
            if (ts != null)
            {
                TextRangeLocation trl = new TextRangeLocation(ts.GetAsOneRange());
                if(trl != null)
                    ReplaceMacroWithBlock(trl, pars);
            }
            subdoc.Close();
            subdoc = null;
        }


        private void ReplaceMacroWithBlock(TextRangeLocation location, IList<DocumentObject> pars)
        {
            int replacementIndex = GetReplacementIndex(location);

            foreach (DocumentObject obj in pars)
            {
                if (obj is Table)
                {
                    sectionBody.ChildObjects.Insert(replacementIndex, ((Table)obj).Clone()); // Exception thrown
                }               
               
            }
        }

When Table.Clone() is called, I receive an exception:
Code: Select all
System.NullReferenceException: Object reference not set to an instance of an object.
   at Spire.Doc.Table.CloneImpl()
   at Spire.Doc.Table.Clone()
   at WebCOBRA2.Letters.LetterParser.ReplaceMacroWithBlock(TextRangeLocation location, IList`1 pars) in

...

Can you please help me fix this error or provide a new release which fixes it? This code looks like it should work.

itsupport
 
Posts: 10
Joined: Thu Jun 14, 2012 7:18 pm

Mon Jul 30, 2012 8:22 am

Hi itsupport,

Thanks for your patience and so sorry for the long-time waiting.

We have researched it, but can't reproduce your problem, I provide you my demo in the attachment . Please check it. You can provide us your simple code and the original file, so that we can help you fix it as soon as possible.

Have a great day!


Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Aug 08, 2012 1:40 pm

It turns out that Table.Clone was failing because it was attached to another document and Clone was unable to properly detach it. I was able to work around the issue. However, I now have a couple files that are failing to correctly generate word documents.

I am uploading a sample with documents. Paths will have to be changed in the code to properly locate the documents. This file is compressed with 7-zip.

itsupport
 
Posts: 10
Joined: Thu Jun 14, 2012 7:18 pm

Thu Aug 09, 2012 8:48 am

Hi itsupport,

Thanks for your patience.

Your problem has be solved by us. You only need to change two parts. First, "innerTemplate.Close();" should be after "spireDoc.Close();", because you close innerTemplate before the copy the contents in innerTemplate, so it can produce error. Second, you should insert cloned obj. Besides, I have done mark in your project. You can check the attachment(Change.zip).

If you have any problems, please feel free to contact us.

Have a nice day!

Best wishes.
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc