MS-Word / General Formatting

Using Strings

A string is the most common data type for macros when working with Word documents. You need to know how to manipulate string data if you hope to accomplish anything meaningful in Word VBA.

For starters, you can create string literals, or strings that are simply quoted in your macro. To use a string literal, enclose a string value in quotation marks - full-fledged double quotes, not apostrophes. For example:

MessageText = "Hello there!"

In this example, "Hello there!" is a string literal whose value is assigned to the string variable named MessageText.

You can use an apostrophe within a string literal with no ill effects, as in the following example:

MessageText = "Hi y'all"

But you cannot include quotes within quotes. For example, the following line produces an error:

MessageText = "Say, "Cheeseburger!""

To include a quotation mark within a literal quoted with quotation marks, you must double the quotation marks, like this:

MessageText = "Say, ""Cheeseburger!"""

Concatenation

You can use the concatenation technique to join two or more strings, end to end, to make a single, larger string. For example:

Entre = "Cheese" & "burger"

This line results in the string "Cheeseburger" being assigned to the Entre variable. The ampersand (&) is used for this purpose. The spaces around the ampersand are optional; if you leave them out, Word adds them when you run the macro.

Concatenation becomes quite useful when combined with string variables and, as you see in the following section, string functions. For example, consider this statement:

Message = "Could not deliver message to " & Recipient

In this line, a literal string value is concatenated with a variable string value. If the value of the Recipient variable is "Michael Yard", the Message variable is set to "Could not deliver message to Michael Yard".

The number of concatenations you can string together in a single statement has no limit.

String functions

VBA provides several built-in functions that work on strings. Table-1 summarizes these functions. Most of them come in handy from time to time.

Table-1 VBA Functions for Manipulating Strings
FunctionWhat It Does
Chr(value)Generates the character that corresponds to the numeric (ANSI) code value. Common uses are:
Chr(9): Tab
Chr(11): New line
Chr(13): Carriage return
Chr(30): Nonbreaking hyphen
Chr(32): Space
Chr(160): Nonbreaking space
InStr([n,]string1, string2)Returns the character position within string1 at which string2 begins, or 0 if string2 is not found in string1. If n is used, the first n-1 characters of string1 are ignored.
LCase(string)Returns the leftmost n characters of string.
Left(string,n)Returns the leftmost n characters of string.
Len(string)Returns the length of string.
LTrim(string)Removes leading spaces from string.
Mid(string,x,y)Returns y characters from string starting at character x.
Right(string,n)Returns the rightmost n characters of string.
Right(string,n)Returns the rightmost n characters of string.
RTrim(string)Removes trailing spaces from string.
Str(n)Converts the number n to a string variable. For example, Str(3.15) becomes the string "3.15".
UCase(string)Converts string to uppercase.
Val(string)Returns the numeric value of string. For example, Val("3.15") becomes the numeric value 3.15.

One of the most commonly used string function is Len: It returns the number of characters in a string. For example:

Message = "Hello world!"
LengthOfMessage = Len(Message)

In this example, the LengthOfMessage variable is assigned the value 12, the number of characters in the string Message. Note: The Len function returns a number, not a string. And the quotation marks that indicate the start and end of string literals aren't counted.

A final group of string functions you need are those used to clean up string values that may contain unnecessary leading or trailing spaces or unprintable characters. These functions include LTrim(), RTrim(), and CleanString(). For example, to remove spaces from the beginning and end of a string variable, use this statement:

TrimmedString = Trim(InputText)
[Previous] [Contents] [Next]