Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2021, 01:16 AM
Guessed's Avatar
Guessed Guessed 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 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,185
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
  #2  
Old 08-18-2021, 09:16 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

Thanks for your help and explanation, Andrew. I'm afraid I've failed to describe just what I'm trying to achieve.

The overall objective is to:
  1. Extract capitalized text from the beginning of a document
  2. Pass the text to a function that converts it to camel case
  3. Pass the camel-case text back and do other stuff
In my code yesterday, I included the following line to set the range:
Code:
Set rngTitle = ActiveDocument.Words(1)
I actually don't want the first word in the document to be the range. I only used this code because I can't otherwise figure out how to convert the all-cap'd text to a range, which I assume is necessary in order to run the text through the MakeTitleCase code, as there's no user-selected text, so Selection can't be used. Among other attempts, I tried the following, which doesn't work:
Code:
Set rngTitle.Text = "SECTION 00 00 00, ETC."
I think I'm misunderstanding the range concept; that is, perhaps a range can apply only to some portion of a document?? I'm stuck on it because I don't know how else to get around the Selection issue. Or maybe I need to do this differently, by parsing the entire string, evaluating each word on its own, then re-assembling?? In other words, Allen's macro really only works for user-selected text and trying to convert it to work on a range won't work, in this case (sorry, bad pun).

Thanks again.
Reply With Quote
Reply



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 02:27 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