Working with Find/Replace programmatically

  • Thread starter Jens Gyldenkærne Clausen
  • Start date
J

Jens Gyldenkærne Clausen

Hi group. I'm trying to write an AddIn capable of replacing highlighted
text with contentcontrols. For the moment my concern is to find the
code that can find the highligted ranges and return them (as a
collection or list). Here I've found two specific problems:

a) How do I return the found Range from
Microsoft.Office.Interop.Word.Find.Execute? I can't just make an
automated replace, since the user needs to be able to correct or
override every replacement. The Find.Execute method returns a boolean
value to indicate IF the search was successfull (and the Find.Found
property gives the same information for multiple matches) - but I am
looking for the actual Range that the Find operation finds. How do I
retrieve that?

b) How do I search for highligted text (or rather - what integer value
corresponds to True in the property Find.Highlight)? I find it very
strange that the documentation mentions that "This property returns
True if highlight formatting is included in the find criteria. Can
return or be set to True, False, or wdUndefined."
(
http://msdn.microsoft.com/da-dk/library/microsoft.office.interop.word.find.highlight(en-us).aspx
) - but the type of the property is Integer, not a specific enumeration
exposing the three possible values.

Regards
 
H

Helmut Weber

Hi Jens,

I don't know about Interop neither contentcontrols,
but I think the following code returns a list
of range for highlighted text.

Sub Test667()
Dim rDcm As Range
Dim rThis() As String
Dim i As Long
Set rDcm = ActiveDocument.Range
i = -1
With rDcm.Find
.Highlight = -1
While .Execute
i = i + 1
ReDim Preserve rThis(i)
rThis(i) = CStr(rDcm.start) & "," & CStr(rDcm.End)
Wend
End With
For i = LBound(rThis) To UBound(rThis)
Debug.Print rThis(i)
Next
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jens Gyldenkærne Clausen

Helmut said:
I don't know about Interop neither contentcontrols,
but I think the following code returns a list
of range for highlighted text.

Thanks for the help. Indeed -1 was the int value for "True" (not really
surprisingly) and the range object itself changes to reflect the match.

Here is a C# version of the code:


public static IList<Range> GetHighlightedText()
{
// Result variable
IList<Range> resultRanges = new List<Range>();

//Create an object for missing values. This will be passed
when ever we don’t want to pass value
Object missing = System.Reflection.Missing.Value;

// Object values for true and the empty string
Object True = true;
Object EmptyString = "";

// Get the current document/content
Document d = GetActiveDocument();
Range r = d.Content;

// Search for highlighted text (True = -1)
r.Find.Highlight = -1;

// Find loop
while (r.Find.Execute(ref EmptyString, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref True, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing))
{
resultRanges.Add(r);
}

// Result
return resultRanges;

}

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top