View Single Post
 
Old 08-18-2021, 01:16 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,980
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Range and String are two different data types. There is no point in passing a string to the function since the .Case won't work on a string but it does work on a range.

Functions differ from Subs in that they usually return a value - that isn't necessary in this case as we are passing a range and the function is changing the value of the text in the range.
Code:
Sub Test_PassToFunction()
  Dim rngTitle As Range
  
  Set rngTitle = ActiveDocument.Words(1)
  rngTitle.Text = "SECTION 00 00 00 - THIS IS THE TITLE OF THE SECTION"    ' (The string actually a variable coming from another macro.)
  MakeTitleCase rngTitle
End Sub

Sub MakeTitleCase (rng As Range)
  ' Modified from Allen Wyatt  https://word.tips.net/T000215_Intelligent_Title_Case.html
  Dim lclist As String, iWord As Integer, sTest As String

  lclist = " of the by to this is from a "    ' List of lowercase words, surrounded by spaces
  rng.Case = wdTitleWord                      ' Make Camel Case

  For iWord = 2 To rng.Words.Count
    sTest = " " & LCase(Trim(rng.Words(iWord))) & " "
    If InStr(lclist, sTest) Then
      rng.Words(iWord).Case = wdLowerCase
    End If
  Next iWord
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote