String manipulation problem

S

SueP

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?
 
J

Jean-Guy Marcil

SueP said:
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?

Are you using this function within a call to an outside DLL or are you
trying to use that VB function as is within a VBA project?

In any case, I believe there are some problems with your code.

STR and LEN are named Functions within VB or VBA. You cannot/should not use
those as variable names. In my opinion, it is better to use descriptive
names, like "strToCheck as String", "lngStringCheck As Long", etc...
A function normally returns a result, I do not see where/how your function
returns anything... How is it useful? Is "thisChar the return? But it gets
redefined at every pass within the For loop...
As a minor note, the Integer type is not used anymore (from what I
understand, an Integer value is too small for the smallest memory unit that
can be allocated by a compiler). It is actually more trouble for a compiler
to work with Integers as they need to be internally converted to Longs. I
always use Long in VBA projects.
Finally, I am not sure what "str.Chars(I)" does. I do not know of a Chars
function, and if I am not mistaken, you cannot attach a function to a
variable like you did, unless it is an object defined in a class module
elsewhere...

I strongly recommend that you use "Option Explicit" at the top of all your
modules.
This will force you to declare all variables and will make debugging much
easier.

Just my 2 cents...
 
F

fumei via OfficeKB.com

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.
 
J

Jay Freedman

While everything Jean-Guy and fumei have said is true, I'll point out the
immediate misconception in your code:

The variable str (whatever you eventually call it, in view of Jean-Guy's
critique) is declared in the parameter list as a String variable. There is no
such thing as a Chars member of a String (and in fact String is a simple data
type, not an object, so it has no members at all).

To get the I-th character from a string, use the Mid function:

thisChar = Mid(str, I, 1)

Look up this function in the VBA help, along with its friends Left and Right.
 
S

SueP

Thanks all for your posts. Obviously I am a VB newbie. Your suggestions have
helped a LOT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top