Word: Look for a String and a comment it. How ?
-
Wednesday, July 09, 2008 7:00 PM
Hello,
I've been trying to write a function that adds a comment to a particular string:
public void addComment(String toFind, String commentText)
So I would:
- Look for the String
- Mark it as a range
- Add a comment to this range.
I'm having trouble marking my found text as a range. I am using this example from MSDN:
public void addComment(String toFind, String commentText)
{
Word.Range rng = Application.ActiveDocument.Range(ref missing, ref missing);
Word.Find f = rng.Find;
f.Text = toFind;
rng.Find.ClearFormatting();
if (rng.Find.Execute(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))
{
object start = rng.Start;
object end = rng.End;
Word.Range foundRange = Application.ActiveDocument.Range(ref start, ref end);
foundRange.Comments.Add(foundRange, ref commentText); // ERROR HERE
}
else
{
MessageBox.Show("Text not found.");
}
rng.Select();
}
// ****************
But this line does not Work:
foundRange.Comments.Add(foundRange, ref commentText);
Why >? foundRange IS a Range object !
Regards,
All Replies
-
Wednesday, July 09, 2008 8:18 PMAnswerer
The following code appears to do what you want. I think the key is that you can access the found text's range by referencing the current selection, but only if you perform your find operation by referencing the selection object to begin with. I'm not an expert in the Word object model, so I can't explain why the same thing doesn't seem to work when you use the Find object of the range that contains the entire document's contents, but perhaps Cindy or someone else on this forum can.
Code Snippetinternal void AddCommentToString(string toFind, string commentText)
{
object Text = commentText;// Set selection to beginning of document.
object Start = 0;
this.Application.ActiveDocument.Range(ref Start, ref Start).Select();
Word.Selection selection = this.Application.Selection;Word.Find find = selection.Find;
find.ClearFormatting();
find.Text = toFind;find.Execute(
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);while (find.Found)
{
selection.Comments.Add(selection.Range, ref Text);find.Execute(
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);
}
}I hope this helps,
McLean Schofield
-
Wednesday, July 09, 2008 8:49 PMThank you, it works

Initially, I chose the Range Find over the Selection Find because it is transparent. But this works just as well ! -
Thursday, July 10, 2008 11:15 AMModerator
Since McLean invoked me... <g>
TheParadoX wrote: Initially, I chose the Range Find over the Selection Find because it is transparent. But this works just as well ! Your original code using the Range object isn't wrong, it's just that you aren't specifying the foundRange to actually be the range that was found - you're setting it to the entire document. Here's the relevant code snippet from your first message:
if (rng.Find.Execute(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))
{
object start = rng.Start;
object end = rng.End;
Word.Range foundRange = Application.ActiveDocument.Range(ref start, ref end);
foundRange.Comments.Add(foundRange, ref commentText); // ERROR HERE
}Change the line highlighted in red to:
Word.Range foundRange = rng.Duplicate;
and you should be in business...

