Chapter 7: Working With Text
Overview
We work with text every day. This book is composed of text: words, phrases, sentences, paragraphs. The ability to work with text is invaluable in our programming.
Recall that the type of variable that deals with text is a String.
Sub <b class="bold">TextWork01</b>() Dim BookTitle As String BookTitle = "Learning MicroStation VBA" MsgBox UCase(BookTitle) MsgBox LCase(BookTitle) MsgBox Left(BookTitle, 12) MsgBox Right(BookTitle, 12)End Sub
In this example, we have a variable named BookTitle that is declared as a String. It is given a value of "LearningMicroStation VBA". Four different functions are then used with the variableBookTitle as a parameter and the result displays in four MessageBoxes.
VBA String Functions
Let's take a look at each of the VBA functions that deal with text (Strings) one-by-one.
UCase
Function UCase(String)
The UCase function converts the supplied string to upper case.
Sub <b class="bold">TextWork02</b>() Dim strNewLevel As String strNewLevel = InputBox("Enter New Level Name:") strNewLevel = UCase(strNewLevel) Application.ActiveDesignFile.AddNewLevel strNewLevelEnd Sub
In this example we use an InputBox to allow the user to enter the name of a new level. We then convert it to upper case and use it to add a new level ( AddNewLevel) to the active design file.