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