WD: ErrMsg Using Mid$(), Left$() or Right$() FunctionLast reviewed: February 12, 1998Article ID: Q110164 |
The information in this article applies to:
SYMPTOMSWhen you use the Mid$(), Right$() or Left$() functions, you may receive the following error message:
WordBasic error #24 "Bad parameter" CAUSEWARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code 'as is' without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This problem occurs when the count or length parameter for the statement is negative. NOTE: In Word 2.0 for Windows, a negative value in the count parameter of the Mid$(), Right$() or Left$() functions is ignored and does not generate an error. The Mid$() statement returns a portion of a text string starting at a given character position. The syntax for the command is
Mid$(<source$>, <start> [, <count>])where <source$> is the text string, <start> is the character position from which to start, and <count> is the number of characters to return. If no <count> parameter is specified, or if there are fewer than <count> characters in the text, the function returns all the characters from the <start> position to the end of the string. The following sample macro illustrates the error message described in the "Symptoms" section of this article:
Sub MAIN ReturnText$ = Mid$("hello", 1, -1) MsgBox ReturnText$ End SubThe following sample macro returns the error because the length of Test$ minus one is a negative number (0 - 1 = -1):
Sub MAIN Test$ = "" Rem Empty string, could be passed as an empty dialog variable for Rem example. ReturnText$ = Right$(Test$, Len(Test$) - 1) End SubNOTE: Word version 2.0 for Windows ignores a -1 value for the <count> parameter. So in Word 2.0 all the characters from the first character position to the end of the string are displayed in the message box. In the first example, the string "hello" is displayed.
RESOLUTIONTo keep this error from ocurring, first test the length of the string. For example:
Sub Main ' Empty string, could be passed as an empty dialog ' variable, for example. Test$ = "" ' Test the length of the string to ensure ' that subtracting 1 from the length leaves ' at least 1 character. If Len(Test$) - 1 > 0 then ReturnText$ = Mid$(Test$, Len(Test$) - 1) End If End Sub REFERENCESWord Developer's Kit, version 6.0, page 594 Word Developer's Kit, Third Edition, page 660
|
Additional query words:
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |