Free Support Forum - aspose.com

Replace Text in Footer

Hi,

how do you replace text in a word document header and footer? I can replace text in the document ok, but it doesn’t replace the text in the header or footer?

I have placeholders in the document that I want to replace, for example I have the text…
Adviser Full Name
throughout the document which I can replace in the document using…
doc.Range.Replace(“Adviser Full Name”, strAdvisorFullName, false, true);

but it won’t replace the Adviser Full Name text in the Footer. How can I do that?

btw, I originally had the placeholder marked with an angled bracket at the start and end like so,
, but it wouldn’t replace the text, is there a trick to using < and > ?
I’m using asp.net and C#
thanks
PH

Hi Peter,

Thanks for your request. Could you please attach your template and code here for testing? I will check the issue and provide you more information.

Regarding angle brackets, please attach your original template as well.

Also I would advise you to use mail merge feature to fill templates with data instead of using find/replace:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/mail-merge-with-regions-explained.html

This approach is much simpler to implement and so less error prone.

Best regards,

Hi,

I got it to work using…
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
doc.Range.Replace(“Adviser Full Name”, strAdvisorFullName, false, true);
doc.Range.Replace(“Date”, strShortDate, true, true);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
doc.Range.Replace(“Adviser Full Name”, strAdvisorFullName, false, true);
doc.Range.Replace(“Date”, strShortDate, true, true);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
doc.Range.Replace(“Adviser Full Name”, strAdvisorFullName, false, true);
doc.Range.Replace(“Date”, strShortDate, true, true);


Here’s the template I’m using…

Also, can you have a look at the tables in my template and tell me how to control the font styles and colours and cell background colors.


Maybe a couple of examples of decent looking tables.
thanks
Peter

Hi Peter,

Thank you for additional information. I just tested your document on my side and replacement works fine on my side. Here is a simple code I used for testing:

Document doc = new Document(@"Test001\in.doc");

doc.Range.Replace(@"", "This is my value", false, false);

doc.Save(@"Test001\out.doc");

Also, looking at your template, I think that using mail merge would be a better option than using placeholders and find/replace technique.

When you perform replacing, formatting of the placeholders will be kept. If you need to change formatting upon replacing, you can use IReplacingCallback to achieve this. Here you can find a simple code example that demonstrates the technique:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-find-and-highlight-text.html

Best regards,