Finding for lowered or raised character

J

JenFruels

Hi,
I am trying to find a raised character in Word. I have the following code:

Dim MWord As Word.Application

With MWord.Selection.Find
.Text = ""
.Font.Position = 5
End With

This code works fine but if the value I used for ".Font.Position" is a
decimal like below:

..Font.Position = 0.5

, it cannot find what I am looking for.Was this a bug or was I missing
something? If I manually searched for the raised character using the Find and
Repalce dialog box, it can actually find the character whether it is a
decimal or a non-decimal. The info in the dialog box has: "Format: Raised by
0.5 pt." but when done programmatically, it wasn't found. Please enlighten
me. Thanks.
 
K

Klaus Linke

When the macro language changed from WordBasic to VBA, the developpers
messed up and defined the position as a Long (integer), although half points
are allowed.

I don't know a good way to find text that's say raised 0.5 pt ...

If you already have it selected, you can get the position from the dialog:

With Dialogs(wdDialogFormatFont)
.Update
MsgBox .Position
End With

You could tell whether the selection contains any text raised 0.5 pt with
this code, looking at the XML:

If InStr(1, Selection.Range.XML, "<w:position w:val=""1""/>") > 0 Then
MsgBox "Selection contains a character or characters raised half a point"
End If

Note that in the XML format, the value s given in half points. So for text
raised 3.5 points, the value would be "7".

Possibly you could do that for the whole text, and if you find there's
anything, then successively split the range you look at in half to pinpoint
the characters actually formatted that way... Though getting the XML takes
quite a long time, and so the macro might be pretty slow on long texts.

Klaus
 
K

Klaus Linke

I just wrote a macro that formats all characters raised or lowered some
(half-point) amount with Highlight=red, using the XML approach described in
the last post.

Regards,
Klaus


Sub FindRaised(Position As Long, rng As Range)
' Specify Position in half points!!
' (3.5 pt: Position=7)
Dim rng1 As Range
Dim rng2 As Range
If InStr(1, rng.XML, "<w:position w:val=" & Chr(34) & _
Trim(STR(Position)) & Chr(34) & "/>") > 0 Then
If Len(rng.Text) > 1 Then
Set rng1 = rng.Duplicate
Set rng2 = rng.Duplicate
rng1.End = rng.Start + Int(0.5 * (rng.End - rng.Start))
rng2.Start = rng.Start + Int(0.5 * (rng.End - rng.Start))
Call FindRaised(Position, rng1)
Call FindRaised(Position, rng2)
Else
rng.HighlightColorIndex = wdRed
End If
End If
End Sub

Sub TestFindRaised()
' Test it, using the main story of the active document,
' looking for text raised 0.5 pt (that is, Position=1)
Call FindRaised(Position:=1, rng:=ActiveDocument.Content)
End Sub
 
K

Klaus Linke

BTW a word of warning:
If the doc contains a style that has the "position raised/lowered" you're
looking for in its definition, the macro will fail miserably.
Since it'll find the <w:position ...> tag for any range in the XML, it'll
mark up every single character, one at a time (... SLOWLY).

Klaus
 
K

Klaus Linke

Since I'm on a roll posting replies to myself, below a version that fixes
the problem with style definitions.
Seems to work in Word2003 and 2007.

The macro takes much, much longer than some "Find/Replace", on the other
hand it's much faster than looking at each character individually.

Klaus

Sub FindRaised(Position As Long, rng As Range)
' Specify Position in half points!!
' (3.5 pt: Position=7)
Dim rng1 As Range
Dim rng2 As Range
Dim posEndStylesDefs As Long
Dim sXML As String
sXML = rng.XML
posEndStylesDefs = InStr(1, sXML, "</w:styles>")
If InStr(posEndStylesDefs, sXML, "<w:position w:val=" & Chr(34) & _
Trim(STR(Position)) & Chr(34) & "/>") > 0 Then
If Len(rng.Text) > 1 Then
Set rng1 = rng.Duplicate
Set rng2 = rng.Duplicate
rng1.End = rng.Start + Int(0.5 * (rng.End - rng.Start))
rng2.Start = rng.Start + Int(0.5 * (rng.End - rng.Start))
Call FindRaised(Position, rng1)
Call FindRaised(Position, rng2)
Else
rng.HighlightColorIndex = wdRed
End If
End If
End Sub
Sub TestFindRaised()
' Test it, using the main story of the active document,
' looking for text raised 0.5 pt (that is, Position=1)
Call FindRaised(Position:=-7, rng:=ActiveDocument.Content)
End Sub
 
J

JenFruels

Hi Klaus, sorry for the late reply. Thank you for your post. It gives me an
insight of what workaround to do. I will also be making use of this on
finding raised/lowered characters on word documents and just make the
necessary modifications. Thanks!
 
K

Klaus Linke

JenFruels said:
Hi Klaus, sorry for the late reply. Thank you for your post. It gives me
an
insight of what workaround to do. I will also be making use of this on
finding raised/lowered characters on word documents and just make the
necessary modifications. Thanks!

If you just want to get rid of the half-point values, you could replace them
in the XML and re-insert it:
ActiveDocument.Content.InsertXML (sXML)

Getting the XML for any Range (no matter how small) is what takes so long,
so if you only need to do it once, go for it.

Klaus
 

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