Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-17-2021, 11:59 PM
Peterson Peterson is offline Trouble converting a sub to function, incl. passing argument and nixing Selection Windows 10 Trouble converting a sub to function, incl. passing argument and nixing Selection Office 2019
Competent Performer
Trouble converting a sub to function, incl. passing argument and nixing Selection
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default Trouble converting a sub to function, incl. passing argument and nixing Selection

I'm trying to convert a macro to a function. The macro converts user-selected text to title case. I want the function to convert a string variable instead.

I'm struggling to figure out how passing a string - as a range - to the function, and how to convert the function's Selection elements to Range elements.

First, should I even be passing a range? And if so, how do I first convert a string variable to a range? The super-crude approach below works, but is ugly -- it's because I haven't been able to figure out how to instantiate the range and assign it text at the same time:
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 is actually a variable coming from another macro.)
    
    fcnTitleCase (rngTitle)
    
End Sub
Meanwhile, I want the function to convert the string without Selection. Ample searching and multiple attempts have only yielded as many error messages. Here's the code:
Code:
Function fcnTitleCase(rngTitle)
' From Allen Wyatt
' https://word.tips.net/T000215_Intelligent_Title_Case.html

    Dim lclist As String
    Dim wrd As Integer
    Dim sTest As String

    ' list of lowercase words, surrounded by spaces
    lclist = " of the by to this is from a "

    Selection.Range.Case = wdTitleWord

    For wrd = 2 To Selection.Range.Words.Count
        sTest = Trim(Selection.Range.Words(wrd))
        sTest = " " & LCase(sTest) & " "
        If InStr(lclist, sTest) Then
            Selection.Range.Words(wrd).Case = wdLowerCase
        End If
    Next wrd
 End Function
Thank you
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble creating a multiple selection drop down list, even after checking other threads sbelinski Word VBA 1 12-14-2020 03:33 PM
Trouble converting a sub to function, incl. passing argument and nixing Selection Help wanted passing data to Form kirkm Word VBA 4 09-19-2016 07:03 PM
Trouble converting a sub to function, incl. passing argument and nixing Selection trouble with pdf doc after converting to word.. tom356 Word 3 09-11-2016 08:23 AM
Trouble converting a sub to function, incl. passing argument and nixing Selection passing variable from one sub to another gbrew584 Excel Programming 10 03-21-2016 12:06 AM
Trouble converting a sub to function, incl. passing argument and nixing Selection userform help (having trouble passing info from form to macro) cyraxote Word VBA 16 09-08-2015 04:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:48 AM.


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