replace strings on existing word documnet with C#

G

Gidi

Hi,

I've a template word document, and using my C# application, i open
it(allwoing to modify) and want to modify it.
the template has const string (i.e. "CONST STRING"), and i want to send a
new string with my application and replace the const string.

How can i do it?

I tried using:

Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref
fileName,ref missing,ref readOnly, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref
isVisible,ref missing,ref missing,ref missing);
WordApp.Selection.Find.Replacement.Text.Replace("CONST STRING","NEW STRING");
aDoc.Activate();
aDoc.Save();
aDoc.Close(ref saveChanges,ref originalFormat,ref routeDocument);
 
N

NullQwerty

WordApp.Selection.Find.Replacement.Text returns a string. By you
calling replace on it, you are just replacing text in the returned
string. Try this:

WordApp.Selection.Find.Replacement.Text =
WordApp.Selection.Find.Replacement.Text.Replace("CONST STRING","NEW
STRING");
 
G

Gidi

Thanks,

It worked !!!

NullQwerty said:
WordApp.Selection.Find.Replacement.Text returns a string. By you
calling replace on it, you are just replacing text in the returned
string. Try this:

WordApp.Selection.Find.Replacement.Text =
WordApp.Selection.Find.Replacement.Text.Replace("CONST STRING","NEW
STRING");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top