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

ID: 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 95, 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("123e")
Further, if any of the above characters appears in the middle of a numeric string, such as "123e4", 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 "e" and beyond:
Print Val("123e4")


WORKAROUND

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

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("123e4")
   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("123e")


REFERENCES

"Microsoft Word for Windows Technical Reference," pages 39, 101, 111, 121

"Using WordBasic," by WexTech Systems and Microsoft, page 325

Additional query words: word6 macword winword word95 word7 winword2

Keywords : kberrmsg kbmacro kbmacroexample
Version : MACINTOSH:6.0,6.0.1; WINDOWS:2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a
Platform : MACINTOSH WINDOWS
Issue type : kbbug


Last Reviewed: October 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.