Microsoft KB Archive/65707

WordBASIC Val Function Does Not Recognize Commas

PSS ID Number: Q65707 Article last modified on 02-17-1993

1.00 1.10 WINDOWS

Summary: In Microsoft Word for Windows, the WordBASIC Val function is a function that converts a string into its numeric value. This function does not recognize commas as part of a number. To work around this behavior, use the following function, which replaces all commas in a string with spaces: Function EraseCommas$(n$ ) result$ = &quot;&quot; For i = 1 To Len(n$) If Mid$ (n$, i, 1) = "," Then  result$  = result$ + &quot; &quot; Else result$ = result$ + Mid$(n$, i, 1) End If Next i EraseCommas$ = result$ End Function

More Information: Val only recognizes the following characters: - Decimal digits (0-9) - Tabs and spaces (ignored) - ONE decimal point - e, e-, e+, E, E-, or E+ followed by a valid exponent value If Val encounters any other characters, it will assume that the number has ended.

Example
Suppose the following macros are run on a document that has 4,350 words: Sub MAIN Dim dlg As EditSummaryInfo GetCurValues dlg Print Val(dlg.NumWords) ’dlg.NumWords = “4,350” End Sub This macro will print a 4 on the status bar because Val doesn’t read past the comma in “4,350”.

Example
Sub MAIN Dim dlg As EditSummaryInfo GetCurValues dlg ’Assumes EraseCommas$ is Print Val(EraseCommas$(dlg.NumWords)) ’defined in the same macro End Sub This macro will correctly print 4350 on the status bar.

Reference(s): “Microsoft Word for Windows Technical Reference,” pages 57, 279, Microsoft Press, 1990.

Additional reference words: w4wmacro