fumei
Fri Feb 29 10:39:08 PST 2008
1. Totally, completely, utterly agree with Jean-Guy regarding using Option
Explicit. Start now. It wil save you lost of headaches.
2. You are getting an Object required because you do not have an object...
one is required.
3. Functions are instructions used for returning something. One thing. They
are used to return ONE thing. Yes, they can be used to execute multiple
instructions (and often do). However, they are normally used to return ONE
thing, and that thing is usually declared. They are usually called from a
Sub.
Function yadda (strIn As String, whatever As Long) As Long
yadda = Len(strIn) / whatever
End Function
Sub Try()
Msgbox yadda(Selection.Text, 2)
End Sub
Would display a messages with the length of the selection text, divided by 2.
Silly example I suppose, but the point is your code does not DO anything
really. You name the Function Spaces....but never set Spaces.
Sub DisplayEachChar()
Dim aChar
For Each aChar In Selection.Range.Characters
MsgBox aChar
Next
End Sub
This would display a messagebox for each character in the Selection. You
could replace MsgBox aChar with whatever you are trying to do with each
character. Asssuming you are using whatever is in the Selection. The point
being is that by passing in parameters to a FUNCTION you get do further
instructions in a SUB.
A Function is a series of instructions that, normally, is used to return a
single value.
A Sub is a series of instructions.
You could do a search through the Selection with something like:
Function Yeah(strIn As String, _
strCheckFor As String) As Boolean
If InStr(1, strIn, strCheckFor) > 1 Then
Yeah = True
Else
Yeah = False
End If
End Function
Sub CheckIt()
Dim strSearch As String
strSearch = InputBox("Enter search string.", "Look for....")
If Yeah(Selection.Text, strSearch) = True Then
MsgBox "Yup, it has that string."
Else
MsgBox "Nope, it does not have that string."
End If
End Sub
Say the Selected text is: "The selection has some text."
Executing CheckIt and entering "some" into the Inputbox will get a messagebox
"Yup, it has that string."
Say the Selected text is: "The selection has yadda yadda text."
Executing CheckIt and entering "some" into the Inputbox will get a messagebox
"Nope, it does not have that string."
You can do pretty much whatever you want regarding string manipulation, as
long as you clearly define what it is you want to do.
Start off with using Option Explicit though.
SueP wrote:
>I want a VB function (called from a word macro) that takes a string and I
>want to manipulate each character at a time. I must be doing something stupid:
>
>function Spaces(str As String, len As Integer)
>for I = 1 to len
> thisChar = str.Chars(I)
>next I
>end function
>
>The message I get is Error 424 Object Required. It does not appear to
>recognize str as a string object. HELP?
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200802/1