Microsoft KB Archive/93999

{|
 * width="100%"|

WD: VAL Returns Illegal Function Call - WordBasic Err=5

 * }

Q93999

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SYMPTOMS
In Word for Windows, you receive the following error message

Word 6.x, 7.x
WordBasic err=100

Syntax error

Word 2.x
Illegal function call - WordBASIC Err=5

if the argument for the Val function begins with a number and ends with one of the following characters:

D

d

E

e For example, the following WordBasic macro statement causes the above error message.:

Print Val(&quot;123e&quot;)

Further, if any of the above characters appears in the middle of a numeric string, such as &quot;123e4&quot;, the return value is incorrect. In the following example, Word incorrectly prints 1230000 on the status bar. Word should print 123 on the status bar, ignoring everything from the letter &quot;e&quot; and beyond:

Print Val(&quot;123e4&quot;)

WORKAROUND
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; 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.

Use the macro function NewVal instead of Val to correctly convert a numeric string into a number. The following macro demonstrates the correct operation of NewVal.

NOTE: The Sub MAIN subroutine is included here to demonstrate that NewVal correctly works on the example given previously.

  Sub MAIN Print NewVal(&quot;123e4&quot;) End Sub

Function NewVal(a$) src$ = a$     result = 0 For i = 1 To Len(src$) sNum$ = Left$(src$, 1) If(Asc(sNum$) > 47) And(Asc(sNum$) < 58) Then result =(result * 10) + Val(sNum$) Else Goto ByeNewVal End If        src$ = Right$(src$, Len(src$) - 1) Next i  ByeNewVal: ' NOTE: This line must be left aligned. NewVal = result End Function

STATUS
Microsoft has confirmed this to be a problem in the Word for Windows version's listed above. This problem was corrected in Word for Windows 97.

MORE INFORMATION
The Val function converts a text string into a numeric variable.

In Visual Basic for Applications (which is available with the Word 97 and later) the Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.

The following returns the value 123:

"Debug.Print Val(&quot;123e&quot;)"