Wanna replace a string in header

Z

Zoom

Hi,

I want to replace a text "<test>" with "hello" in the word document header
and footer using C#. Below code doesn't do anything. Can someone suggest an
option for it.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text = "<test>";
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text = "hello";
 
J

Jean-Guy Marcil

Zoom was telling us:
Zoom nous racontait que :
Hi,

I want to replace a text "<test>" with "hello" in the word document
header
and footer using C#. Below code doesn't do anything. Can someone
suggest an
option for it.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello";

You are missing an Execute statement. As is, you are setting the minimum
parameters for executing a find/replace, but you are not actually doing it.
In VBA, the minimum code would be:

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find
.Text = "<test>"
.Replacement.Text = "hello"
.Execute Replace:=wdReplaceOne
End With

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
Z

Zoom

Hi Jean,

Forgot to mention, i do specify execute but it didn't work out. Please check
the below code.

ApplicationClass app = new ApplicationClass();
doc = app.Documents.open(------------------------);
Selection sel = app.Selection;
object replaceone = WdReplace.wdReplaceOne;

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text = "<test>";

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text = "hello";
sel.Find.Execute(ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref replaceone, ref missing, ref missing, ref missing,
ref missing);


Jean-Guy Marcil said:
Zoom was telling us:
Zoom nous racontait que :
Hi,

I want to replace a text "<test>" with "hello" in the word document
header
and footer using C#. Below code doesn't do anything. Can someone
suggest an
option for it.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello";

You are missing an Execute statement. As is, you are setting the minimum
parameters for executing a find/replace, but you are not actually doing it.
In VBA, the minimum code would be:

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find
.Text = "<test>"
.Replacement.Text = "hello"
.Execute Replace:=wdReplaceOne
End With

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Zoom was telling us:
Zoom nous racontait que :
Hi Jean,

Forgot to mention, i do specify execute but it didn't work out.
Please check
the below code.

ApplicationClass app = new ApplicationClass();
doc = app.Documents.open(------------------------);
Selection sel = app.Selection;
object replaceone = WdReplace.wdReplaceOne;

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello"; sel.Find.Execute(ref missing, ref
missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref
missing, ref missing, ref replaceone, ref missing, ref missing, ref
missing,
ref missing);


Jean-Guy Marcil said:
Zoom was telling us:
Zoom nous racontait que :
Hi,

I want to replace a text "<test>" with "hello" in the word document
header
and footer using C#. Below code doesn't do anything. Can someone
suggest an
option for it.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello";

You are defining find/replace parameters on a range object, but are using
the Execute method on a selection object.

Try with a "With (...) End With" block (or its equivalent in C#) as I
suggested in my previous post.

Or replace,
sel
by
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
Z

Zoom

Hi Jean,

Thanks for your help, i'm suppose to use execute on selection object. But
even after that i don't find any change with the header. I'm able to add new
header text but i'm not able to replace an existing text. Below the execute
line which i tried.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Execute(ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref replaceone, ref missing,
ref missing, ref missing, ref missing);

Thanks


Jean-Guy Marcil said:
Zoom was telling us:
Zoom nous racontait que :
Hi Jean,

Forgot to mention, i do specify execute but it didn't work out.
Please check
the below code.

ApplicationClass app = new ApplicationClass();
doc = app.Documents.open(------------------------);
Selection sel = app.Selection;
object replaceone = WdReplace.wdReplaceOne;

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello"; sel.Find.Execute(ref missing, ref
missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref
missing, ref missing, ref replaceone, ref missing, ref missing, ref
missing,
ref missing);


Jean-Guy Marcil said:
Zoom was telling us:
Zoom nous racontait que :

Hi,

I want to replace a text "<test>" with "hello" in the word document
header
and footer using C#. Below code doesn't do anything. Can someone
suggest an
option for it.

app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Text
= "<test>";
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Replacement.Text
= "hello";

You are defining find/replace parameters on a range object, but are using
the Execute method on a selection object.

Try with a "With (...) End With" block (or its equivalent in C#) as I
suggested in my previous post.

Or replace,
sel
by
app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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