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

Replacing text and preserving formatting?

Feb 3, 2011 at 3:53 AM

Is there a way to use the ReplaceText function and preserve formatting?  For instance I have the text "<<_Name_>>" that is bolded and 20 point font.  But when I replace it, the text is back to 12 point and not bolded.

I also tried using a custom property with the AddCustomProperty function but this function seems very fragile.  If I make any formatting changes to the custom property it stops replacing it with the desired text.  In addition if I try to add new CustomProperties to the document after saving my first version it again stops replacing text.

Is there a way to do what I am trying?

Coordinator
Feb 3, 2011 at 8:43 AM
Hello broham,

ReplaceText is suppose to preserve style such as Font, Bold, etc.
Are you using the newest version of DocX, v1.0.0.11?

If so please send me the document and the word to be replaced, I will debug the issue on my side and get back to you with a fix.

Kind regards,
Cathal
Feb 8, 2011 at 2:37 PM

Hi Cathal,
Yes I am using 1.0.0.11.

I sent you an email with the documents attached.  Here is the code that I am using:

                using (DocX doc = DocX.Load(@"C:\Temp\MergDoc.docx"))
                {
                    doc.ReplaceText("<<_name_>>", u.Name);   //these values come from objects of a class i created.  In newDoc0.docx they were replaced with: "John Doe1", "111 first street", "Sacramento", "CA"
                    doc.ReplaceText("<<_address_>>", u.Address);
                    doc.ReplaceText("<<_city_>>", u.City);
                    doc.ReplaceText("<<_state_>>", u.State);
                    doc.ReplaceText("<<_date_>>", DateTime.Now.ToShortDateString());
                    doc.ReplaceText("<<_phoneNumber_>>", "555-555-5555");
                    doc.SaveAs(@"c:\Temp\newDoc" + i + ".docx");
                    i++;
                }

Thanks for the quick response!  I'm liking the project so far.
-Abe

Feb 8, 2011 at 3:53 PM

I'm having the same problem with the latest version. No formatting is preserved.

 

 

Feb 19, 2011 at 10:08 AM

I have the same issue too in 1.0.0.11.

Feb 23, 2011 at 8:32 PM

Has there been a resolution to this issue?

Feb 24, 2011 at 4:48 PM

I ended up commenting out line 1722 in paragraph.cs top get it working for me.

 

//if (formatting == null) formatting = new Formatting();

Mar 2, 2011 at 11:42 PM

@moola, any chance you would upload a copy of the working dll? Thanks

Coordinator
Mar 19, 2011 at 8:59 PM

Hey guys,

I have just uploaded a fix for this awful bug. Please download change set 62851.

Sorry this one took so long, Ive had a crazy few weeks.

Happy coding,
Cathal

Mar 21, 2011 at 10:49 AM

Hi Cathal,

I am having issues trying to compile the source code, any chance you could update the .dll on the codeplex site or alternatively email it me across?

Cheers

James

From: coffeycathal [email removed]
Sent: 19 March 2011 20:59
To: James Monaghan (Monatech)
Subject: Re: Replacing text and preserving formatting? [DocX:244425]

From: coffeycathal

Hey guys,

I have just uploaded a fix for this awful bug. Please download change set 62851.

Sorry this one took so long, Ive had a crazy few weeks.

Happy coding,
Cathal


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Coordinator
Mar 21, 2011 at 11:11 AM

Hi James,

try just downloading the dll from the source it will be in the folder debug/bin/docx.dll if this does not work I will email it to you when I get back to my desk.

Regards,
Cathal

This email was sent from my Android phone.

On 21 Mar 2011 10:52, "MCFC2011" <notifications@codeplex.com> wrote:
> From: MCFC2011
>
> Hi Cathal,I am having issues trying to compile the source code, any chance you could update the .dll on the codeplex site or alternatively email it me across?CheersJamesFrom: coffeycathal [email removed]
> Sent: 19 March 2011 20:59
> To: James Monaghan (Monatech)
> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHey guys,I have just uploaded a fix for this awful bug. Please download change set62851.Sorry this one took so long, Ive had a crazy few weeks.Happy coding,
> Cathal
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
>
Mar 21, 2011 at 11:32 AM

Hi Cathal,

Thanks for getting back to me so quickly. I downloaded the source from CodePlex and followed your advice. The Version of the dll in the latest release is 10.0.0.10 where as the buggy version is 10.0.0.11. Do you think this will make a difference?

Cheers

James

From: coffeycathal [email removed]
Sent: 21 March 2011 11:12
To: James Monaghan (Monatech)
Subject: Re: Replacing text and preserving formatting? [DocX:244425]

From: coffeycathal

Hi James,

try just downloading the dll from the source it will be in the folder debug/bin/docx.dll if this does not work I will email it to you when I get back to my desk.

Regards,
Cathal

This email was sent from my Android phone.

On 21 Mar 2011 10:52, "MCFC2011" <notifications@codeplex.com> wrote:
> From: MCFC2011
>
> Hi Cathal,I am having issues trying to compile the source code, any chance you could update the .dll on the codeplex site or alternatively email it me across?CheersJamesFrom: coffeycathal [email removed]
> Sent: 19 March 2011 20:59
> To: James Monaghan (Monatech)
> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHey guys,I have just uploaded a fix for this awful bug. Please download change set62851.Sorry this one took so long, Ive had a crazy few weeks.Happy coding,
> Cathal
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
>


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Coordinator
Mar 21, 2011 at 11:47 AM

James,

I forgot to update the version number of the dll in the source, it is actually the newest version. Thanks for bringing it to my attention. I will rename it shortly.

Regards,
Cathal

On 21 Mar 2011 11:32, "MCFC2011" <notifications@codeplex.com> wrote:
> From: MCFC2011
>
> Hi Cathal,Thanks for getting back to me so quickly. I downloaded the source from CodePlex and followed your advice. The Version of the dll in the latest release is 10.0.0.10 where as the buggy version is 10.0.0.11. Do you think this will make a difference?CheersJamesFrom: coffeycathal [email removed]
> Sent: 21 March 2011 11:12
> To: James Monaghan (Monatech)
> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHi James, try just downloading the dll from the source it will be in the folder debug/bin/docx.dll if this does not work I will email it to you when I get back to my desk.Regards,
> CathalThis email was sent from my Android phone.On 21 Mar 2011 10:52, "MCFC2011" <notifications@codeplex.com> wrote:
>> From: MCFC2011
>>
>> Hi Cathal,I am having issues trying to compile the source code, any chance you could update the .dll on the codeplex site or alternatively email it me across?CheersJamesFrom: coffeycathal [email removed]
>> Sent: 19 March 2011 20:59
>> To: James Monaghan (Monatech)
>> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHey guys,I have just uploaded a fix for this awful bug. Please download change set62851.Sorry this one took so long, Ive had a crazy few weeks.Happy coding,
>> Cathal
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>
>>
>>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
>
Mar 21, 2011 at 11:54 AM

Great. Thanks for your help.

James

From: coffeycathal [email removed]
Sent: 21 March 2011 11:48
To: James Monaghan (Monatech)
Subject: Re: Replacing text and preserving formatting? [DocX:244425]

From: coffeycathal

James,

I forgot to update the version number of the dll in the source, it is actually the newest version. Thanks for bringing it to my attention. I will rename it shortly.

Regards,
Cathal

On 21 Mar 2011 11:32, "MCFC2011" <notifications@codeplex.com> wrote:
> From: MCFC2011
>
> Hi Cathal,Thanks for getting back to me so quickly. I downloaded the source from CodePlex and followed your advice. The Version of the dll in the latest release is 10.0.0.10 where as the buggy version is 10.0.0.11. Do you think this will make a difference?CheersJamesFrom: coffeycathal [email removed]
> Sent: 21 March 2011 11:12
> To: James Monaghan (Monatech)
> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHi James, try just downloading the dll from the source it will be in the folder debug/bin/docx.dll if this does not work I will email it to you when I get back to my desk.Regards,
> CathalThis email was sent from my Android phone.On 21 Mar 2011 10:52, "MCFC2011" <notifications@codeplex.com> wrote:
>> From: MCFC2011
>>
>> Hi Cathal,I am having issues trying to compile the source code, any chance you could update the .dll on the codeplex site or alternatively email it me across?CheersJamesFrom: coffeycathal [email removed]
>> Sent: 19 March 2011 20:59
>> To: James Monaghan (Monatech)
>> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalHey guys,I have just uploaded a fix for this awful bug. Please download change set62851.Sorry this one took so long, Ive had a crazy few weeks.Happy coding,
>> Cathal
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>
>>
>>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
>


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Mar 21, 2011 at 9:23 PM

Hmmmm.... doesn't work at all for me now.  I have to go back to 62676 to get it to partially work.  Not doing anything special and can confirm in the document.xml that my varibles are not split.

below is my code... no rocket science involved.

        protected void exeProcessTheDoc(object sender, EventArgs e)
        {
            string doc1 = Server.MapPath("~/Templates/ExternalAssessmentT.docx");
            string doc2 = Server.MapPath("~/Templates/againagainagain.docx");

            GetPropertyFromDocument(doc1, doc2);
        }

        public static void GetPropertyFromDocument(string document, string outdoc)
        {
            File.Delete(outdoc);
            File.Copy(document, outdoc);
            using (DocX doc = DocX.Load(outdoc))
            {
                doc.ReplaceText("{Company Name}", "Penn Inc.", false);
                doc.ReplaceText("{Project No}", "CLE-OP55555", false);
                doc.ReplaceText("{Primary Contact}", "John Smith", false);
                doc.SaveAs(outdoc);
            }
        }
Coordinator
Mar 21, 2011 at 10:52 PM
petronius,

Can you send me the document in question?

The only thing I can think off is that sometimes Word stores
properties such as {Company Name} internally with a caps tag.
As of a recent change, DocX now understands the caps tag, are you sure
you don't need to do

doc.ReplaceText("{COMPANY NAME}", "Penn Inc.", false);

I won't know for sure until you send me a document that ReplaceText fails on.

Cathal
Mar 21, 2011 at 11:43 PM

Here’s the doc I’ve been working from. Thanks for taking a look. Simplified code is included below. Simply reads the one doc, saves it as another after the changes.

namespace DocumentCatalyst

{

public partial class TestReplace : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void exeProcessTheDoc(object sender, EventArgs e)

{

string doc1 = Server.MapPath("~/Templates/ExternalAssessmentT.docx");

string doc2 = Server.MapPath("~/Templates/againagainagain.docx");

GetPropertyFromDocument(doc1, doc2);

}

public static void GetPropertyFromDocument(string document, string outdoc)

{

File.Delete(outdoc);

File.Copy(document, outdoc);

using (DocX doc = DocX.Load(outdoc))

{

doc.ReplaceText("{Company Name}", "Penn Inc.", false);

doc.ReplaceText("{Project No}", "CLE-OP55555", false);

doc.ReplaceText("{Primary Contact}", "John Smith", false);

doc.SaveAs(outdoc);

}

}

}

}

Toby Penn

Solutions Director

North Central, Ohio Valley, North East
Cell: 216.978-5298
tpenn@accuvant.com
http://www.accuvant.com
www.twitter.com/accuvant

From: coffeycathal [email removed]
Sent: Monday, March 21, 2011 6:52 PM
To: Toby Penn
Subject: Re: Replacing text and preserving formatting? [DocX:244425]

From: coffeycathal

petronius,

Can you send me the document in question?

The only thing I can think off is that sometimes Word stores
properties such as {Company Name} internally with a caps tag.
As of a recent change, DocX now understands the caps tag, are you sure
you don't need to do

doc.ReplaceText("{COMPANY NAME}", "Penn Inc.", false);

I won't know for sure until you send me a document that ReplaceText fails on.

Cathal

Coordinator
Mar 21, 2011 at 11:47 PM
petronius,

you didn't attach the .docx.
Codeplex doesn't let you upload documents so just attached it to an
email and send it to coffey.cathal@gmail.com

Cheers,
Cathal
Coordinator
Mar 22, 2011 at 6:46 PM

Petronius,

theres was two problems at play here.

The first problem was that your Paragraphs are all defined inside <snd></snd> tags and a recent change made to support PageNumbers caused these Paragraphs to be ignored.
Please download changeset 63004 and your Paragraphs that are inside <snd></snd> tags will no longer be ignored :-)

The second problem is that the value {Project No} in your document is not text, its bound to a core property you have defined in the document.

doc.ReplaceText("{Project No}", "CLE-OP55555", false); is working correctly, however when you open the document Word.exe fetches the value stored in the Core Property which is {Project No} and replaces the hard work done by DocX.

The following is the correct way to achieve what you are trying to do.

using (DocX doc = DocX.Load("In.docx"))
{
    // Replace text.
    doc.ReplaceText("{Company Name}", "Penn Inc.", false);
    doc.ReplaceText("{Primary Contact}", "John Smith", false);

    // Chance the value of the core property that has the value {Project No}
    doc.AddCoreProperty("dc:subject", "CLE-OP55555");
    doc.SaveAs("Out.docx"); 
}

Happy coding,
Cathal

Mar 23, 2011 at 2:11 AM

Are you recompiling the dll in each of these updates? The following doesn’t work at all unless I’m using 62676.

using (DocX doc = DocX.Load("In.docx"))
{

doc.ReplaceText("{Primary Contact}", "John Smith", false);

doc.SaveAs("Out.docx");
}

Petronius

From: coffeycathal [email removed]
Sent: Tuesday, March 22, 2011 2:47 PM
To: Toby Penn
Subject: Re: Replacing text and preserving formatting? [DocX:244425]

From: coffeycathal

Petronius,

theres was two problems at play here.

The first problem was that your Paragraphs are all defined inside <snd></snd> tags and a recent change made to support PageNumbers caused these Paragraphs to be ignored.
Please download changeset 63004 and your Paragraphs that are inside <snd></snd> tags will no longer be ignored :-)

The second problem is that the value {Project No} in your document is not text, its bound to a core property you have defined in the document.

doc.ReplaceText("{Project No}", "CLE-OP55555", false); is working correctly, however when you open the document Word.exe fetches the value stored in the Core Property which is {Project No} and replaces the hard work done by DocX.

The following is the correct way to achieve what you are trying to do.

using (DocX doc = DocX.Load("In.docx"))
{
// Replace text.
doc.ReplaceText("{Company Name}", "Penn Inc.", false);
doc.ReplaceText("{Primary Contact}", "John Smith", false);

// Chance the value of the core property that has the value {Project No}
doc.AddCoreProperty("dc:subject", "CLE-OP55555");
doc.SaveAs("Out.docx");
}

Happy coding,
Cathal

Coordinator
Mar 23, 2011 at 9:33 AM

Yes I build the dll from the latest source. I will email you this dll and when you verify it fixes your problem I will replace the version currently available for download on the homepage

On 23 Mar 2011 02:11, "petronius" <notifications@codeplex.com> wrote:
> From: petronius
>
> Are you recompiling the dll in each of these updates? The following doesn’t work at all unless I’m using 62676.using (DocX doc = DocX.Load("In.docx"))
> {doc.ReplaceText("{Primary Contact}", "John Smith", false);doc.SaveAs("Out.docx");
> }PetroniusFrom: coffeycathal [email removed]
> Sent: Tuesday, March 22, 2011 2:47 PM
> To: Toby Penn
> Subject: Re: Replacing text and preserving formatting? [DocX:244425]From: coffeycathalPetronius,theres was two problems at play here.The first problem was that your Paragraphs are all defined inside <snd></snd> tags and a recent change made to support PageNumbers caused these Paragraphs to be ignored.
> Please download changeset 63004 and your Paragraphs that are inside <snd></snd> tags will no longer be ignored :-)The second problem is that the value{Project No} in your document is not text, its bound to a core property you have defined in the document.doc.ReplaceText("{Project No}", "CLE-OP55555", false); is working correctly, however when you open the document Word.exe fetches the value stored in the Core Property which is {Project No} and replaces the hard work done by DocX.The following is the correct way to achieve what you are trying to do.using (DocX doc = DocX.Load("In.docx"))
> {
> // Replace text.
> doc.ReplaceText("{Company Name}", "Penn Inc.", false);
> doc.ReplaceText("{Primary Contact}", "John Smith", false);// Chance the value of the core property that has the value{Project No}
> doc.AddCoreProperty("dc:subject", "CLE-OP55555");
> doc.SaveAs("Out.docx");
> }Happy coding,
> Cathal
>
>
Mar 23, 2011 at 4:40 PM

Got the new .dll and all is right with the world once more.  Thank you for your dilligence and great work on this project!!!

Mar 30, 2011 at 1:16 PM

I have build the latest source, and it works like charm  :O)  Good work.

Apr 5, 2011 at 3:43 PM

Hi PalleHansen,

May I know how do you manage to build the source code, i have setup my VS to download all the source code. but it requires a password for the strongnamefile.pfx. Thanks.

Apr 5, 2011 at 4:30 PM

Never mind, i found it in the Help folder :)

Coordinator
Apr 5, 2011 at 4:56 PM

Excellent

On 5 Apr 2011 17:30, "afrizal_chen" <notifications@codeplex.com> wrote:
> From: afrizal_chen
>
> Never mind, i found it in the Help folder :)
>
>
Sep 21, 2011 at 2:06 PM
Edited Sep 21, 2011 at 2:06 PM

Hi Coffey,

I still cannot get replace text to conserve formatting.  

I've added a bit of text ("<Merge Item>") and some more pages into the invoiceTemplate.docx example, so it appears multiple times on multiple pages.

I'm then using template.ReplaceText("<Merge Item>", "Some text"); in the code file in order to replace the text.  However, the text is replaced, but so is the formatting.

Anyclue what is going on here?

p.s. I've tried the selected builds as well as the newest source build...