Microsoft KB Archive/290969

= &quot;Run-time error 5&quot; when you use Mid, Left, or Right function =

Article ID: 290969

Article Last Modified on 8/15/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q290969





For a Microsoft Word 97 version of this article, see 181102.



For a Microsoft Word 98 version of this article, see 181103.



For a Microsoft Word 2000 version of this article, see 201979.



SYMPTOMS
In Microsoft Visual Basic for Applications (VBA), when you use the Mid, Right, or Left function, you may receive the following error message:

Run-time error '5': &quot;Invalid procedure call or argument&quot;



CAUSE
This behavior occurs when the length argument for the statement is negative.

The Mid function returns a portion of a text string starting at a given character position. The syntax for the command is as follows Mid(, [, ]) where is the text string to search, is the character position from which to start, and is the number of characters to return. If no argument is specified, or if there are fewer than characters in the text, the function returns all the characters from the position to the end of the string.



RESOLUTION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

290140 How to run the sample code for the Office XP programs from Knowledge Base articles

To resolve this issue, be sure to test the length of the string, as in the following example: Sub ErrorDoesNotOccur

Dim Test As String Dim ReturnText As String

' Empty string, could be passed as an empty dialog ' variable, for example. Test = &quot;&quot;

' 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

Additional query words: vb parameter error 5 Run time Invalid procedure call or argument prb WD2002 WD2003

Keywords: kberrmsg kbmacroexample kbprb kbdtacode KB290969

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.