Replace all in Word.ApplicationClass

D

Danny op het Veld

I created a solution using the Word.ApplicationClass where I want to open an
existing Word document, find a certain string and replace all found instances
of the string with another string.

It works fine in the sense that it replaces the found instances, except for
the header and footer.
When I use the word GUI application and do a Ctrl-H (replace) it replaces
also the found string in the header and footer section.

Can anyone please tell me what I do wrong?

I'll put my current code below. Thanks in advance!

private void ReplaceVariablesAndSave(string myFile)
{
object objFileName = myFile;
// Open the requested document
Word.Document myWordDocument = myWordApp.Documents.Open(ref objFileName,
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);


// Save the new document
string strWordFileAttachment = strNewDirectory + @"\" +
myWordDocument.Name;
objFileName = strWordFileAttachment;
myWordDocument.SaveAs(ref objFileName, 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);

// Move selection to beginning of document
object unit = Word.WdUnits.wdStory;
object extend = Word.WdMovementType.wdMove;
myWordApp.Selection.HomeKey(ref unit, ref extend);

// Find and replace the Name of the Child
Word.Find fnd = myWordApp.Selection.Find;
fnd.ClearFormatting();
fnd.MatchCase = false;
fnd.Text = strFindNameOfChild;
fnd.Replacement.ClearFormatting();
fnd.Replacement.Text = txtNameOfChild.Text;
ExecuteReplace(fnd);

// Find and replace the Date of the Party
fnd.ClearFormatting();
fnd.Text = strDateOfParty;
fnd.Replacement.ClearFormatting();
fnd.Replacement.Text = dateTimeParty.Text;
ExecuteReplace(fnd);

// Save the document again
myWordDocument.Save();

// Exit word
myWordDocument.Close(ref missing, ref missing, ref missing);

}

/// <summary>
/// Execute and Replace the variables in the document
/// </summary>
/// <param name="find"></param>
/// <returns></returns>
private Boolean ExecuteReplace(Word.Find find)
{
// Simple wrapper around Find.Execute:
Object findText = missing;
Object matchCase = missing;
Object matchWholeWord = missing;
Object matchWildcards = missing;
Object matchSoundsLike = missing;
Object matchAllWordForms = missing;
Object forward = missing;
Object wrap = missing;
Object format = missing;
Object replaceWith = missing;
Object replace = Word.WdReplace.wdReplaceAll;
Object matchKashida = missing;
Object matchDiacritics = missing;
Object matchAlefHamza = missing;
Object matchControl = missing;

return find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
ref matchControl);
}
 
J

Jay Freedman

I created a solution using the Word.ApplicationClass where I want to open an
existing Word document, find a certain string and replace all found instances
of the string with another string.

It works fine in the sense that it replaces the found instances, except for
the header and footer.
When I use the word GUI application and do a Ctrl-H (replace) it replaces
also the found string in the header and footer section.

Can anyone please tell me what I do wrong?

I'll put my current code below. Thanks in advance!
[snip]

Hi Danny,

The Find object in the object model doesn't behave the same way as the
Find command in the user interface, something we've repeatedly kicked
Microsoft for. In VBA, and by extension in any external automation
interface, the Find object works on only one "story" at a time -- and
a document may contain upwards of a dozen stories.

This article shows how to iterate over all stories in a macro. You
should be able to adapt it to your code.
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
 
D

Danny op het Veld

Jay,

Thanks very much.

I changed my code and it works fine now!

Regards,

Danny

Jay Freedman said:
I created a solution using the Word.ApplicationClass where I want to open an
existing Word document, find a certain string and replace all found instances
of the string with another string.

It works fine in the sense that it replaces the found instances, except for
the header and footer.
When I use the word GUI application and do a Ctrl-H (replace) it replaces
also the found string in the header and footer section.

Can anyone please tell me what I do wrong?

I'll put my current code below. Thanks in advance!
[snip]

Hi Danny,

The Find object in the object model doesn't behave the same way as the
Find command in the user interface, something we've repeatedly kicked
Microsoft for. In VBA, and by extension in any external automation
interface, the Find object works on only one "story" at a time -- and
a document may contain upwards of a dozen stories.

This article shows how to iterate over all stories in a macro. You
should be able to adapt it to your code.
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
 

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