Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Converting Numbers to Strings.
by Allen Wyatt
(last updated November 3, 2016)
You already know that you can use variables in your macros and that there are two very basic types of variables: string variables (containing characters) and numeric variables (containing numeric values). You can quickly and easily convert a number into a string. This is done with the Str() function. The syntax for using this function is:
A = Str(B)
In this syntax, if B is a numeric value equal to 5, then when completed, the string in A will be " 5"; if B is -4, then A would be "-4". Notice the leading space when converting positive numbers. This may not provide satisfactory results for some subroutines. Instead, you should create a function that returns a stripped-down version of the string. The following function does just that:
Function ToNum(X as Variant) as String Dim A as String A = Ltrim(Str(X)) ToNum = A End Function
The reason that the value passed to the function (X) is defined as a Variant is that you can then pass any type of numeric value.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (7066) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Converting Numbers to Strings.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
Macros can be used to read and write all sorts of files. If those files are on a different drive than the current one, you'll ...Discover More
One way to specify word count is to count characters and divide by five. If you still need this old-fashioned way of ...Discover More
When working with macros, you may want to create a variable that will remain constant from one instance of the macro to ...Discover More