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