Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-08-2016, 01:11 PM
drcz drcz is offline propercase function Windows 10 propercase function Office 2016
Novice
propercase function
 
Join Date: May 2016
Posts: 4
drcz is on a distinguished road
Default propercase function

HI,



I saw a function created by Paul Edstein / macropod few years ago which I cannot make it to work in my case.
What I need: if an word is containing any capital letter then that word to start with a Capital letter. If the word is written in lowercases had to be left as it is

ThIs TeXT has to bE transformed wITh CaPITAL LETTERS
into
This Text has to Be transformed With Capital Letters


I spent all day long but I could not find proper solution
Code:
Function ProperCase(StrTxt As String, Optional Caps As Long, Optional Excl As Long) As String
'Convert an input string to proper-case.
'Surnames like O', Mc and hyphenated names are converted to proper case also.
'If Caps = 0, then upper-case strings like ABC are preserved; otherwise they're converted.
'If Excl = 0, selected words are retained as lower-case, except when they follow specified punctuation marks.
Dim i As Long, j As Long, k As Long, l As Long, bChngFlg As Boolean
Dim StrTmpA As String, StrTmpB As String, StrExcl As String, StrPunct As String, StrChr As String
StrExcl = " a , an , and , as , at , but , by , for , from , if , in , is , of , on , or , the , this , to , with "
StrPunct = "!,:,.,?,"""
If Excl <> 0 Then
  StrExcl = ""
  StrPunct = ""
End If
If Len(Trim(StrTxt)) = 0 Then
  ProperCase = StrTxt
  Exit Function
End If
If Caps <> 0 Then StrTxt = LCase(StrTxt)
StrTxt = " " & StrTxt & " "
For i = 1 To UBound(Split(StrTxt, " "))
  StrTmpA = " " & Split(StrTxt, " ")(i) & " "
  StrTmpB = UCase(Left(StrTmpA, 2)) & Right(StrTmpA, Len(StrTmpA) - 2)
  StrTxt = Replace(StrTxt, StrTmpA, StrTmpB)
Next i
StrTxt = Trim(StrTxt)
'Code for handling O' names
For i = 1 To UBound(Split(StrTxt, "'"))
  ' If InStr(Right(Split(StrTxt, "'")(i - 1), 2), " ") = 1 Then
  If InStr(Right(Split(StrTxt, "'")(i - 1), 2), " ") = 1 Or _
    Right(Split(StrTxt, "'")(i - 1), 2) = Right(Split(StrTxt, "'")(i - 1), 1) Then
    StrTmpA = Split(StrTxt, "'")(i)
    StrTmpB = UCase(Left(StrTmpA, 1)) & Right(StrTmpA, Len(StrTmpA) - 1)
    StrTxt = Replace(StrTxt, StrTmpA, StrTmpB)
  End If
Next
'Code for handling hyphenated names
For i = 1 To UBound(Split(StrTxt, "-"))
  StrTmpA = Split(StrTxt, "-")(i)
  StrTmpB = UCase(Left(StrTmpA, 1)) & Right(StrTmpA, Len(StrTmpA) - 1)
  StrTxt = Replace(StrTxt, StrTmpA, StrTmpB)
Next
'Code for handling names starting with Mc
If Left(StrTxt, 2) = "Mc" Then
  Mid(StrTxt, 3, 1) = UCase(Mid(StrTxt, 3, 1))
End If
i = InStr(StrTxt, " Mc")
If i > 0 Then
  Mid(StrTxt, i + 3, 1) = UCase(Mid(StrTxt, i + 3, 1))
End If
'Code for handling names starting with Mac
If Left(StrTxt, 3) = "Mac" Then
  If Len(Split(Trim(StrTxt), " ")(0)) > 5 Then
    Mid(StrTxt, 4, 1) = UCase(Mid(StrTxt, 4, 1))
  End If
End If
i = InStr(StrTxt, " Mac")
If i > 0 Then
  If Len(StrTxt) > i + 5 Then
    Mid(StrTxt, i + 4, 1) = UCase(Mid(StrTxt, i + 4, 1))
  End If
End If
'Code to restore excluded words to lower case
For i = 0 To UBound(Split(StrExcl, ","))
  StrTmpA = Split(StrExcl, ",")(i)
  StrTmpB = UCase(Left(StrTmpA, 2)) & Right(StrTmpA, Len(StrTmpA) - 2)
  If InStr(StrTxt, StrTmpB) > 0 Then
    StrTxt = Replace(StrTxt, StrTmpB, StrTmpA)
    'Make sure an excluded words following punctution marks are given proper case anyway
    For j = 0 To UBound(Split(StrPunct, ","))
      StrChr = Split(StrPunct, ",")(j)
      StrTxt = Replace(StrTxt, StrChr & StrTmpA, StrChr & StrTmpB)
    Next
  End If
Next
ProperCase = StrTxt
End Function

Last edited by macropod; 05-08-2016 at 04:03 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 05-08-2016, 04:23 PM
macropod's Avatar
macropod macropod is offline propercase function Windows 7 64bit propercase function Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The function you posted appears to be an edited variant of code I've posted in threads such as:
https://www.msofficeforums.com/word/24807-how-do-i-convert-line-text-title.html
https://www.msofficeforums.com/word-...html#post55885
It is not deigned to do as you say you want it to and wouldn't even do as it's supposed to with your modifications. For what you say you want, you could use a macro like:
Code:
Sub Reformat()
Dim i As Long, StrChr As String, StrTmp As String
With Selection
  For i = 1 To .Words.Count
    StrChr = Left(.Words(i), 1)
    If StrChr = LCase(StrChr) Then
      StrTmp = StrTmp & LCase(.Words(i))
    Else
      StrTmp = StrTmp & StrChr & LCase(Mid(.Words(i), 2, Len(.Words(i)) - 1))
    End If
  Next
  .Text = StrTmp
End With
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-08-2016, 09:57 PM
drcz drcz is offline propercase function Windows 10 propercase function Office 2016
Novice
propercase function
 
Join Date: May 2016
Posts: 4
drcz is on a distinguished road
Default

thank you.
ok, I will use # ...

now this started to give me some results.
my text: thIs TeXT has to bE transformed wITh CaPITAL LETTERS
became: this Text has to be transformed with Capital Letters

and it should be: This Text has to Be transformed With Capital Letters

This, Be and With are not having the Capital letter.
Reply With Quote
  #4  
Old 05-08-2016, 10:10 PM
macropod's Avatar
macropod macropod is offline propercase function Windows 7 64bit propercase function Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by drcz View Post
my text: thIs TeXT has to bE transformed wITh CaPITAL LETTERS
became: this Text has to be transformed with Capital Letters

and it should be: This Text has to Be transformed With Capital Letters
How do you expect the macro to know when a word that begins with a lower-case letter should actually start with an upper-case letter???
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-08-2016, 10:33 PM
drcz drcz is offline propercase function Windows 10 propercase function Office 2016
Novice
propercase function
 
Join Date: May 2016
Posts: 4
drcz is on a distinguished road
Default

That was the problem I could not solve: one way theoretically could be that it checks first if the word is lowercase and if yes it skips it. If there are some capital letters in it then it should put it with capital letter. This is the rule I need but I do not know how to do it.
thank you for your patience with me
this function I guess it does check if the word is lowercase?

Code:
Function AllLowerCase(stringToCheck As String) As Boolean

    AllLowerCase = StrComp(stringToCheck, LCase(stringToCheck), _
       vbBinaryCompare) = 0
End Function
Reply With Quote
  #6  
Old 05-08-2016, 10:44 PM
macropod's Avatar
macropod macropod is offline propercase function Windows 7 64bit propercase function Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In that case, try:
Code:
Sub Reformat()
Dim i As Long, StrTmp As String
With Selection
  For i = 1 To .Words.Count
    If .Words(i) = LCase(.Words(i)) Then
      StrTmp = StrTmp & .Words(i)
    Else
      StrTmp = StrTmp & UCase(Left(.Words(i), 1)) & LCase(Mid(.Words(i), 2, Len(.Words(i)) - 1))
    End If
  Next
  .Text = StrTmp
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-09-2016, 12:57 AM
drcz drcz is offline propercase function Windows 10 propercase function Office 2016
Novice
propercase function
 
Join Date: May 2016
Posts: 4
drcz is on a distinguished road
Default

That is doing the right thing! Thank you Paul. After spending days trying to figure out I can go further with my project. That text I have to insert in a different program that has to have the words configured like that....
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
propercase function Restrict Editing function disable insert textbox function IanM_01 Word 5 11-21-2015 02:29 AM
IF Function Azleenda Excel 2 11-10-2015 10:06 PM
propercase function #REF! Error in calling VBA function disappears when function is copied lcaretto Excel Programming 2 05-26-2014 07:19 PM
Creating a graph for Future Value function (FV function) bmoody Excel 2 11-06-2013 10:52 AM
if function help jim831 Excel 2 10-29-2010 07:06 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:44 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft