Footers and Replacement Counts in Word 2007 Search and Replace
I had a couple of great questions from my post on automated search-and-replace in Word documents:
Hello
I just want to know what I have to change in order to search in the footer of the page only ?
My goal is to update the version of a document which is written in the footer. I don't need to search in the core of the document.
Thanks for your great code !
Longin Benoit
There is an enumeration that determines the type of each Range, Microsoft.Office.Interop.Word.WdStoryType. The values available that have to do with footers are:
- wdPrimaryFooterStory -- main footer story range
- wdFirstPageFooterStory -- override for the first page footer
- wdEvenPagesFooterStory -- override for even numbered pages (odd would inherit wdPrimaryFooterStory)
So to run the code in my previous post for the wdPrimaryFooterStory, you could do something like this (omitting the setup and cleanup code):
// Get the primary footer range Word.Range primaryFooterRange = doc.StoryRanges[Word.WdStoryType.wdPrimaryFooterStory]; // Set the find and replace text (I set a hard-coded token of "Document Version: 2.0" as the starting version, // but this could be initialized to your previous version programmatically) primaryFooterRange.Find.Text = "Document Version: 2.0"; primaryFooterRange.Find.Replacement.Text = "Document Version: 2.1"; // Set the option to wdReplace.wdReplaceOne (assuming you only have the version appearing once in the footer) object replaceAll = Word.WdReplace.wdReplaceOne; primaryFooterRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
Note that f you have differing alternating-page footers, you will have to run this twice, once on the wdPrimaryFooterStory, and a second time on the wdEvenPagesFooterStory.
Second Question:
I've improvised a little into your code as I need to implement an app to find the occurence of a certain word .
However I don't how to find the Occurence number !!
That's my Code
public void Search(Document docObj)
{
int Counter = 0;
foreach (Range tmpRange in docObj.StoryRanges)
{
tmpRange.Find.Text = "Blob";
tmpRange.Find.Wrap = FindWrap.wdFindContinue;
// Here should kind of property or method that should check if "Blob" 's been found then Counter++;
}
System.Windows.Forms.MessageBox.Show(Counter.ToString());
}
Thanks .
Fatla
This one is a little trickier -- I wasn't able to find any property after the "Find" that tells you how many were found. I did find a good hack on this website where they suggest doing a count of characters before and after, divided by the difference in length of the two strings, to get a count of replacements (by Bart Verbeek and Dave Rado):
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm
For situations where the search and replace strings are the same length, they suggest replacing with an additional "#" character, getting the count, and then doing another replace to strip out the "#". This seems like a lot of work, but I wasn't able to find anything in the API to do this, so it's probably a decent option if you abstract it into a method. Anyone who has any other ideas, please let me know.