Hi, I am trying to search for a string in Word Document using C#. My problem is the string I'm trying to find is not a series of characters one after the other. It has a start condition that I need to check and end condition. Anything in between is just read as it is. eg: I will accept the following string: (Check note: see section below) the startwith condition is (Check note: endwith condition is ) there can be any no of characters in between. it depends on the word doc under consideration. I'm pasting my code, please let me know what I should do. The code below works well if I have a particular search say just the starting string. But for both matches, I can't come up with anything. I have also tried foreach(Word.range rng in doc4.storyranges) but i know this is wrong Thanks May
object File1 = SourceFile3;
object File2 = TargetFile1;
object objTrue =true;
object objFalse=false;
Word. ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//WordApp.Selection.Find.ClearFormatting();
Word. Document doc4 = WordApp.Documents.Open(ref File1, ref objMissing, ref objTrue, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
rng.Find.Text = "(Programming Note:";
rng.Find.Forward = true;
rng.Find.Replacement.Text = "got it";
rng.Find.Forward = true;
rng.Find.Wrap = Microsoft.Office.Interop.Word. WdFindWrap.wdFindContinue;
rng.Find.Execute( ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing);
doc4.SaveAs( ref File2, ref objSaveFormat, ref objMissing, ref objMissing, ref objMissing,
ref objMissing,ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing,ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//doc4.AcceptAllRevisions();
}
}
}
Answers
-
Here's a code snippet that demonstrates using a wildcard search that specifies and beginning and end string, with any number of characters between. For information about the use of special characters, please consult the sources I've mentioned previously.
//Wildcard search |
Word.Document oWordDoc = Globals.ThisAddIn.app.ActiveDocument; |
Word.Range rng = oWordDoc.Content; |
rng.Find.ClearFormatting(); |
object findText="<Start of*end of>"; |
object oTrue = true; |
object oFalse = false; |
object oFindStop = Word.WdFindWrap.wdFindStop; |
rng.Find.Execute(ref findText, ref oTrue, ref oFalse, ref oTrue, |
ref oFalse, ref oFalse, ref oTrue, ref oFindStop, ref oFalse, |
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); |
rng.Select(); |
|
Cindy Meister, VSTO/Word MVP
- Marked As Answer byTim LiMSFT, ModeratorFriday, March 20, 2009 6:15 AM
- Unmarked As Answer byMay1006 Friday, March 20, 2009 7:38 PM
- Marked As Answer byMay1006 Friday, March 20, 2009 7:38 PM
- Unmarked As Answer byMay1006 Friday, March 20, 2009 7:39 PM
- Marked As Answer byCindy MeisterMVP, ModeratorTuesday, March 24, 2009 3:09 PM
-
All Replies
-
-
Hi,
Thanks for your reply. But what I'm looking for is something that can be done through a program and without clicking the ctrl+F button. Also by using the Find property from Range, you can set for what words you are looking for is what I think. I want to check only the first few words and last character in one search. Since I need to replace that with an image. I was thinking tht counting the string and then checking occurrence would be an option though it sounds tedious. Anyways let me know if there is something you think easier than this.
Thanks once again
-
Hi,
Thanks for your reply. But what I'm looking for is something that can be done through a program and without clicking the ctrl+F button. Also by using the Find property from Range, you can set for what words you are looking for is what I think. I want to check only the first few words and last character in one search. Since I need to replace that with an image. I was thinking tht counting the string and then checking occurrence would be an option though it sounds tedious. Anyways let me know if there is something you think easier than this.
Thanks once again
Please re-read my response - carefully. In order to figure out the wildcard search string you should first work in the end-user dialog box. Once you've figured it out, you can carry the search string over to your code...
Cindy Meister, VSTO/Word MVP
-
Hi,
Thanks again. Could you please give some sample code that I can go through?
-
Here's a code snippet that demonstrates using a wildcard search that specifies and beginning and end string, with any number of characters between. For information about the use of special characters, please consult the sources I've mentioned previously.
//Wildcard search |
Word.Document oWordDoc = Globals.ThisAddIn.app.ActiveDocument; |
Word.Range rng = oWordDoc.Content; |
rng.Find.ClearFormatting(); |
object findText="<Start of*end of>"; |
object oTrue = true; |
object oFalse = false; |
object oFindStop = Word.WdFindWrap.wdFindStop; |
rng.Find.Execute(ref findText, ref oTrue, ref oFalse, ref oTrue, |
ref oFalse, ref oFalse, ref oTrue, ref oFindStop, ref oFalse, |
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); |
rng.Select(); |
|
Cindy Meister, VSTO/Word MVP
- Marked As Answer byTim LiMSFT, ModeratorFriday, March 20, 2009 6:15 AM
- Unmarked As Answer byMay1006 Friday, March 20, 2009 7:38 PM
- Marked As Answer byMay1006 Friday, March 20, 2009 7:38 PM
- Unmarked As Answer byMay1006 Friday, March 20, 2009 7:39 PM
- Marked As Answer byCindy MeisterMVP, ModeratorTuesday, March 24, 2009 3:09 PM
-
-
Hi Cindi, thanks for the information. I tried the code snippet you suggested but its replacing the part with just (Prg Note even if it doesn't consist of a closing bracket. Also it is finding just the first occurrence. Even though the property of find.forward is set true, its not working. Could you suggest me something?
Here is the code I've developed...
Word. ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//WordApp.Selection.Find.ClearFormatting();
Word. Document doc4 = WordApp.Documents.Open(ref File1, ref objMissing, ref objTrue, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
Word. Range rng = doc4.Content;
rng.Find.ClearFormatting();
object findtext = "<(Programming Note:*)>";
object ofindstop = Word.WdFindWrap.wdFindStop;
object oReplace = "got it";
rng.Application.Selection.Find.Forward = true; //even if i keep this n the next 2 lines commented, there is no change
rng.Application.Selection.Find.MatchWildcards = true;
rng.Application.Selection.Find.Wrap = Microsoft.Office.Interop.Word. WdFindWrap.wdFindContinue;
rng.Application.Selection.Find.Execute( ref findtext, ref objTrue, ref objFalse,
ref objTrue, ref objFalse, ref objFalse, ref objTrue, ref ofindstop,
ref objFalse, ref oReplace, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing);
rng.Select();
doc4.SaveAs( ref File2, ref objSaveFormat, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
-
Hi ,
rng.Application.Selection.Find.Execute returns a boolean value,which you could use it to control a While loop to search over again.
Thanks
We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
-
Thank you all for the help.
I'm sorry Cindi to unmark that answer. I wanted some more information. So just to keep it in loop, I thought would be better.
I found the solution to find the 2 searches. But I did that using the indexing method. I did 2 searches and then I'm finally replacing it with a tag. But now my problem is I don't know how to find multiple occurrences of the search though. When I try to use foreach for any for loop, it doesn't search for anything. Only the first search is highlighted. In short, the code is of no use. So can anyone help me out with it?
Thanks!!
-
Hi May
this is why I suggested you test in the Word UI, as an end-user and ask for assitance in the end-user oriented newsgroups. Figuring out the exact wildcard combination required is often a task for a specialist.
As far as continuing a search after performing an action on each "hit": You need to collapse the Range variable being used for the search. When rng.Find.Execute is successful, rng is set to the found text. Thus, the search will either keep finding itself, or not proceed any further because the range has already been searched. So, you need to reset the range before looping. Exactly how to do this depends on a number of factors, but one possibility would be:
Word.Range rng = doc4.Content; //perform all the Find stuff //assuming a successful search, we need to reset the Range object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd; rng.Collapse(ref collapseEnd); rng.End = doc4.Content.End;
This collapse the range to its end-point so that rng.Start immediately follows the range that was found. The end-point of the range is then set to the end of the document. Note that this approach will not work reliably in tables. If your range is in a table, then you need to loop the cells of the table until you reach the end of the table.
Cindy Meister, VSTO/Word MVP
|