Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Find, replace and StrConv question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vince - 17 Mar 2005 05:50 GMT
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
Helmut Weber - 17 Mar 2005 13:26 GMT
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/
Greg - 17 Mar 2005 15:03 GMT
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.
Helmut Weber - 17 Mar 2005 21:29 GMT
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/
Greg Maxey - 18 Mar 2005 03:37 GMT
> Always a joy discussing things with you.

Hear, hear.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support
http://gregmaxey.mvps.org/word_tips.htm

> Hi Submariner,
>
[quoted text clipped - 15 lines]
> Word XP, Win 98
> http://word.mvps.org/ 
Greg - 17 Mar 2005 13:27 GMT
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
Greg - 17 Mar 2005 14:36 GMT
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
Vince - 18 Mar 2005 03:03 GMT
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

> Vince,
>
[quoted text clipped - 34 lines]
> End With
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.