Solved

Using Word Automation - highlight a word + insert a comment

Posted on 2009-04-05
10
681 Views
Last Modified: 2013-12-17
Hi!

Can someone help me out? Im trying to open a word document and search for certain words in the document, and then highlight (make the background yellow) them, and add a comment. However, I don't know which functions I need to use for highlighting or adding a comment. I've only gotten so far as to calling Range.Find.Execute(), but I don't know what to do next.

Thanks
0
Comment
Question by:muskad202
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24070955
You can first set the default highlight colour to yellow.
WrdApplication.Options.DefaultHighlightColorIndex = wdYellow

In the Find, set the .Format property of the Find object to True, and the .Replacement.Highlight to True.



0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24070972
I don't know C#, but this is VBA for adding a Comment if the Find is successful.
Dim comm  As Word.Comment
Dim wrdDoc as Word.Document
 
'...
If MyRange.Find.Execute() then
   Set comm = ActiveDocument.Comments.Add(MyRange, MyCommentText)
End If

Open in new window

0
 
LVL 2

Author Comment

by:muskad202
ID: 24071565
Hi!
The code for highlight worked great - thanks!
Regarding adding the comments, what happens is, I initially start off with the range spanning the entire .doc (since i dont knoe where the word i want to find lies). If I then pass in the range to Comments.Add(), it ends up adding the comment for the entire document, instead of just the word. How do I work around this?

Thanks!
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24071665
Perhaps you could post your code. I have worked with C and Javascript, so I could probably read it well enough.
0
 
LVL 2

Author Comment

by:muskad202
ID: 24071726
The code is shown below.
The problem is that "range" is initialized to be the entire document, and so when i add a comment, it gets associated with the entire range. I guess what i need to do is - for each successful occurrence for the Find() operation, i need the range corresponding only to that, and then add a comment to that new range, instead of the originall range within which i did the Find().
oWordDoc = OpenDocument(documentPath, false);
range = oWordDoc.Range(ref missing, ref missing);
object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object whichItem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object forward = true;
object matchAllWord = true;
range.Document.GoTo(ref item, ref whichItem, ref missing, ref missing);
bool success = range.Find.Execute(ref oFindText, ref missing, ref matchAllWord, ref missing, ref missing, ref missing, ref forward, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (success)
{
   oWordDoc.Comments.Add(range, ref oComment);
}
oWordDoc.Close(ref oTrue, ref missing, ref missing);

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 75 total points
ID: 24072087
I would have thought that it would work. The operation depends on the Word Object model, and I would be surprised if the Find does not set the Range object to the found text in C#.

I have tried to replicate what you are doing in VBA in order to assure myself on the way that it works in that environment.  The only point I would make, and I don't know if it's relevant to your problem, is that the GoTo probably isn't necessary.
Sub FindAndComment()
    Dim oWordApp As Word.Application
    Dim oWordDoc As Word.Document
    Dim oRange As Word.Range
    Dim success As Boolean
    Dim strFindText As String
    Dim strComment As String
    
    strFindText = "fox"
    strComment = "Animal"
    Set oWordDoc = oWordApp.Documents.Open(documentPath)
    Set oRange = oWordDoc.Range
    success = oRange.Find.Execute(strFindText, , , , , True, True)
    If success Then
       oWordDoc.Comments.Add oRange, strComment
    End If
    oWordDoc.Close wdSaveChanges
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24072181
I have run up VS.Net and tried C# for the first time by pasting your code into a button click event.

It gives 28 build errors because it doesn't know what 'oWordDoc', 'range', 'oFindText' and 'Office' are.

I guess that they have to be 'declared' somehow. Is that easy for you to explain, or would you rather wait for a C# expert to carry on with the question?
0
 
LVL 2

Author Comment

by:muskad202
ID: 24072256
I've attached my entire solution here. It's pretty small, so you can give it a try if you want.
MSWordPrototype.zip
0
 
LVL 2

Author Comment

by:muskad202
ID: 24072261
I just tried it - if I remove the GoTo, it works perfect! Thanks!!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24072432
Thanks. I'll study your solution anyway.Perhaps it's time I got involved with .net
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question