This project has moved. For the latest updates, please go here.

Is there a way to replace a string with a table?

Jul 28, 2009 at 1:14 PM

I want to be able to find a string like this...

<QuoteData>

... and replace it with a table.  It seems that creating a table is pretty straightforward, but is there a way to replace a string with my table?

Coordinator
Jul 28, 2009 at 11:34 PM
Hi krugs525,

I am currently working on a new version of DocX that will contain this functionality. I am not yet ready to upload this new version to codeplex as it requires more testing and I need to update the documentation.chm file with some new functions and examples.

However if you send me an email, I will send you back an early copy of this new version. Below is an example of how to use this new version to accomplish your task.

            // Load a document.
            using (DocX document = DocX.Load(@"Test.docx"))
            {
                // Find all instances of the string '<QuoteData>', I care not about the case.
                List<int> indexList = document.FindAll("<QuoteData>", RegexOptions.IgnoreCase);

                /* 
                 * Reverse this list.
                 * I do this so that when I insert new content (A table in this case)
                 * I do not affect the indexes of earlier instances of the string '<QuoteData>'.
                 */ 
                indexList.Reverse();

                // Loop through each found instance in reverse document order.
                foreach (int index in indexList)
                {
                    // Insert a Table at each found index.
                    Table t = document.InsertTable(index, 2, 2);
                    
                    // Set the Table design (By default the table is invisible).
                    t.Design = TableDesign.ColorfulGrid;
                }

                // Remove all of the marker tags <QuoteData>, they are no longer useful.
                document.ReplaceText("<QuoteData>", string.Empty, false);
                
                /* 
                 * Save this document as Test2.docx. 
                 * I don't want to override my origional document, incase I made a mistake.
                 */
                document.SaveAs(@"Test2.docx");
            } // Release the document from memory.

let me know if this helps.
kind regards and happy coding,
Cathal
Mar 17, 2014 at 11:03 AM
I am sure that after all these years the new version is released.

I am using your code but It's throwing an exception:
Specified argument was out of the range of valid values.
document.InsertTable(index, 2, 2);
only works when index is 0, other value than that throws an exception.
Apr 20, 2014 at 12:40 PM
I've got the same exception with the code, based on the sample above (but replacing a placeholder with a bulleted list):
            var agendaItems = new List<string>{"1", "2", ...};

            using (DocX doc = DocX.Load(stream))
            {
                var agendaList = doc.AddList();
                foreach (var agendaItem in agendaItems)
                {
                    doc.AddListItem(agendaList, agendaItem);
                }

                var indexList = doc.FindAll("{Agenda}", RegexOptions.IgnoreCase);
                indexList.Reverse();

                // Loop through each found instance in reverse document order.
                foreach (int index in indexList)
                {
                    // Insert a List at each found index.
                    //****************************************************************************************************************
                    var list = doc.InsertList(index, agendaList); // EXCEPTION is thrown here
                    //****************************************************************************************************************
                } 
                
                // Remove all of the marker tags {Agenda}, they are no longer useful.
                doc.ReplaceText("{Agenda}", string.Empty, false);

                doc.Save();
            }        
Apr 30, 2014 at 9:47 AM
Specified argument was out of the range of valid values.
I am having the same problem. I have to replicate a table n times within a document and I am doing this by copying the orginal table and inserting it after a specific text block but it crashes with the above error. Is there a fix for it in the works? Or do you know an alternative for achieving the same?