Find, replace and StrConv question

V

Vince

This is a little confusing.

Example input:
Old Mcdonald had a farm. <Text> This text cannot be guessed </Text> Eaeieio

What's desired:
I should be able to turn the text within the "<Text>" tag to (a) Proper
Case (b) Only the first initial capital (c) Lower Case (d) Upper Case (e)
Small capitals.

What I've done:

(d) and (e) are very simple:

With ActiveDocument.Range.Find
.ClearFormatting
.MatchWildcards = True
.text = "\<Text\>" & "[!^l^13]@" & "\</Text\>"

with .replacement
.text ="^&"
.font.allcaps=True ' For (d)
.font.Smallcaps=True ' For (e)
End With

.Execute Replace:=wdReplaceAll
End With

This works fine for (d) and (e). But, what can I do about (a), (b) and (c)?
I know that VB has a function VbStrConv(String,VbLowerCase) and so forth but
I cannot use them because I do not know the content of the Find string.
Therefore, if I do a:

.text =strconv("^&",vbuppercase) ' This is meaningless as ^& is
considered to be the text

What should I do?

Thank you for your time.

Vince
 
H

Helmut Weber

Hi Vince,

have a look at this demo:

Sub test55555()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Dim sTmp As String
With rDcm.Find
.MatchWildcards = True
.Text = "\<Text\>" & "[!^l^13]@" & "\</Text\>"
.Execute
rDcm.start = rDcm.start + 7
rDcm.End = rDcm.End - 8
sTmp = rDcm.Text
rDcm.Text = StrConv(sTmp, vbProperCase)
rDcm.Text = sTmp
rDcm.Text = StrConv(sTmp, vbUpperCase)
rDcm.Text = sTmp
rDcm.Text = StrConv(sTmp, vbLowerCase)
rDcm.Text = sTmp
rDcm.Font.AllCaps = True
rDcm.Font.Reset
rDcm.Font.SmallCaps = True
rDcm.Font.Reset
End With
End Sub

For use in a while .execute wend loop you'd have
to collapse rDcm to the end at the end of the loop.

rdcm.collapse direction:=wdcollapseend
wend

HTH

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Vince

Try:
Sub Test()
Dim myRange As Range
Dim myString As String

Set myRange = ActiveDocument.Range
With myRange.Find
.ClearFormatting
.MatchWildcards = True
.Text = "\<*\>" & "[!^l^13]@" & "\</*\>"
While .Execute
With myRange
.Case = wdTitleWord ' for a
' .Case = wdTitleSentence ' for b
' .Case = wdLowerCase ' for c
' .Case = wdUpperCase ' for d
' .Font.SmallCaps = True 'for e
End With
myRange.Collapse direction:=wdCollapseEnd
Wend
End With
End Sub
 
G

Greg

Vince,

I like how Helmut excluded the Tags from the formatting. I tried that
with my original code and noticed that it didnt' work for "b" or
Sentence case. I found that even using the menu Format>Change Case you
couldn't apply sentence case formating if the selection was not
structured as a sentence. To get around this I did a little string
manipulation.

Try:

Sub Test()
Dim myRange As Range
Dim myString As String
Dim myString2 As String
Set myRange = ActiveDocument.Range
With myRange.Find
.ClearFormatting
.MatchWildcards = True
.Text = "\<*\>" & "[!^l^13]@" & "\</*\>"
While .Execute
myRange.Start = myRange.Start + 7
myRange.End = myRange.End - 8
With myRange
.Case = wdTitleWord ' for a
'Use the following three lines for b (Sentence Case)
'myString = myRange.Text
'myString = Format(Left(myString, 1), ">") & Right(myString,
Len(myString) - 1)
'myRange.Text = myString
'.Case = wdLowerCase ' for c
'.Case = wdUpperCase ' for d
'.Font.SmallCaps = True 'for e
End With
myRange.Collapse direction:=wdCollapseEnd
Wend
End With
End Sub
 
G

Greg

Helmut,

I like how you stripped out the tags and format only the bound text.
Looking at your code you don't address Vince's option "b" which would
be sentence case. I posted what appears to be a workable solution if
you want to have a look.
 
H

Helmut Weber

Hi Submariner,

in fact, I never came across range.case before.
Seems, I didn't care about wdTitleSentence, because
the case of the range containing "This text cannot be guessed"
is wdTitleSentence anyway. But could easily be integrated.
The point seems to be to differentiate between changing
the characters in the range like rDcm.Case = wdTitleWord
and applying formatting. Formatting can be reset easily,
rDcm.Font.Reset,
whereas changing range.case makes it necessary,
to remember (sTmp) what the original text was.

Always a joy discussing things with you.

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
V

Vince

Hey Helmut / Greg,

I can't thank you both enough for your help. I also had no idea about
Range.Case. Needless to say, this code works great and so did Helmut's code.

Once again, thank you very much.

Vince
 

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