Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

replace strings on existing word documnet with C#

751 views
Skip to first unread message

Gidi

unread,
Jun 26, 2007, 7:37:01 AM6/26/07
to
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);


NullQwerty

unread,
Jun 26, 2007, 8:25:21 AM6/26/07
to
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");

Gidi

unread,
Jun 26, 2007, 8:41:10 AM6/26/07
to
Thanks,

It worked !!!

Karunagara Pandi

unread,
Oct 24, 2010, 9:58:06 AM10/24/10
to
Hi,

I too struggle with similar issue. Here's my code which you had given. But I'm getting error..

private void FindAndReplaceText()
{
object missing = System.Reflection.Missing.Value;
object file = "c:\\desc.doc";

Microsoft.Office.Interop.Word.Application WordApp =
new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.Document aDoc =
WordApp.Documents.Open(ref file,ref missing,ref missing,ref missing,ref missing,


ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing);

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

WordApp.Selection.Find.Replacement.Text =
WordApp.Selection.Find.Replacement.Text.Replace("Project", "Pandi"); // Getting error [Exception from HRESULT: 0xC0000005]

aDoc.Activate();
aDoc.Save();
aDoc.Close(ref missing, ref missing, ref missing);
//WordApp.Quit(ref missing, ref missing, ref missing);

Please solve my problem....
}

> On Tuesday, June 26, 2007 7:37 AM shnaps wrote:

> 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);


>> On Tuesday, June 26, 2007 8:25 AM NullQwerty wrote:

>> 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");
>>
>>
>> Gidi wrote:


>>> On Tuesday, June 26, 2007 8:41 AM shnaps wrote:

>>> Thanks,
>>>
>>> It worked !!!
>>>
>>> "NullQwerty" wrote:


>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>> SharePoint WorkFlow Basics
>>> http://www.eggheadcafe.com/tutorials/aspnet/1fa263fb-d7a6-40f5-8875-356f75d9fca9/sharepoint-workflow-basics.aspx

0 new messages