You are on page 1of 7

VB Scripting for CATIA V5: Email Course by Emmett Ross

Lesson #3 - Message and Input Boxes

Message Boxes

The MsgBox function in VBA displays a message box in a window and waits for a user to
click on a button. Message boxes are frequently used to display strings to users while
running a program. Message boxes are helpful debugging tools to insert into code to
display what a variable is currently set to. Be aware that the Msgbox function can only
display up to 1,024 characters and display a limited number of lines. This means arrays
with many values or many characters may get cut off.

Tutorial: Create Your First Custom “Hello World” Macro

It’s time to follow along to create your first CATIA macro. In the programming world, the
term “Hello World” typically refers to the most basic program that can be written in a given
language. Think of it like this, if you can write a Hello World CATIA macro, this means you
are able to setup the necessary tools, write the code, compile the macro, and run the
program. Not a bad start!

1. To begin, create a new macro library using VBA Projects instead of Directories.
2. Create a new macro using the MS VBA language (most examples in this book are
either VBA or CATScript).

3. Rename the macro to Exercise1 then double click or hit edit to begin editing the
macro in the VBA editor.

4. Go to Tools > References and make sure all available CATIA references are selected.

5. By default, CATIA doesn’t force you to declare your variables. However, you should
change that setting immediately. In CATIA (or Excel or other software), press
Alt+F11 to launch the Visual Basic Editor (VBE). Go to Tools>Options.
6. In the Editor tab of the Options dialog, make sure that there’s a check mark in front
of “Require Variable Declaration”

7. After you click OK, insert a new Module. When you do so, you’ll see Option Explicit
as the first line of the module. This line tells CATIA that all variables must be
declared making your code easier to read and follow. Option Explicit will be the first
line in our code.
8. A CATIA VB program or "macro" consists of a "Subroutine" named CATMain().
CATIA only recognizes Sub CATMain as the entry point to any VBA application.
Every Sub must end with an End Sub. Between the two Sub statements, type the text as
shown:

Option Explicit
Sub CATMain()

'declare a variable as a String type


Dim strMessage As String

'define the String variable as the text Hello World


strMessage = "Hello world!"

'have a message box display the text in CATIA


Msgbox strMessage
End Sub
9. Save then run the macro by clicking the play icon, going to the run menu, or by
pressing F5. A message box should pop-up displaying “Hello world!”. Click ok to end
the program. Voila! You have just setup, coded, and ran your first CATIA macro, your
first accomplishment as a programmer.

There are multiple options for the types of buttons displayed on the message box pop-up
window.

Type of MsgBox Buttons


0 vbOKOnly OK button only
1 vbOKCancel OK and Cancel buttons
2 vbAbortRetryIgnore Abort, Retry, and Ignore buttons
3 vbYesNoCancel Yes, No, and Cancel buttons
4 vbYesNo Yes and No buttons
5 vbRetryCancel Retry and Cancel buttons

The values for the constants:

Constant Value Description


vbOK 1 Value signifies that the OK button was pressed
vbCancel 2 Value signifies that the Cancel button was pressed
vbAbort 3 Value signifies that the Abort button was pressed
vbRetry 4 Value signifies that the Retry button was pressed
vbIgnore 5 Value signifies that the Ignore button was pressed
vbYes 6 Value signifies that the Yes button was pressed
vbNo 7 Value signifies that the No button was pressed

To ask a question with a message box use this syntax:


Value = MsgBox (message, [parameter], [Title])
Sub CATMain()
Dim Response As Integer
Response = MsgBox(“Are you sure you want to quit?”, vbYesNo, Confirm to Quit)
If Response = vbYes Then
End 'ends the program
End If
End Sub

Input Boxes

The message box approach only allows for pre-determined responses. To drive a program
event, you may want to ask a user to input data such as text, numbers, or a formula. In this
case, an input box is the answer. Input box syntax is:

value = Inputbox(message, title, value)


dimensionOne=Inputbox(“Please enter the first dimension :”, “Enter the_
Dimension”, 0)

There are a few potential issues when using an input box. When a user hits cancel the
inputbox function returns an empty string. Another possible problem with an input box is
that it might return the wrong type of data, like if the code is expecting a number and a user
enters text string. Use error handling (discussed later) to avoid these potential pitfalls. As a
programmer, you need to try to think of every possible outcome and program against them.
Parameter Input Box

CATIA macros can have input parameters. This means when the macro is run a dialog box
is immediately opened allowing the user to enter input values. The syntax is: Sub
CATMain(Parameter 1, Parameter2,…) where the parameters might be a string, integer,
double, sketch, pad, etc.. Sub CATMain( Length, Width)

Tutorial: Add two integers

Practice creating a VBA program from scratch with Tutorial 2.1.

1. Create a new VBA macro.

2. Enter the following code. Pay close attention to the comments for information about each
chunk of code. This code uses an If..Then statement.
Option Explicit
Sub CATMain()

'declaration of variables
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer

'valuate two of the variables


intA = 2
intB = 3

'set the value of the third as a sum of the first two


intC = intA + intB
'create a string to be displayed as the output use the function Str() to
'convert the integer into a string
Dim strOutput As String
strOutput = "The sum is " & Str(intC)

'call a message box asking the user if they want to display the sum
Dim response As Integer
response = MsgBox("Do you want to display the sum?", vbYesNo, "Display Sum")
If response = vbYes Then
MsgBox strOutput
'do not need an Else here because there are no other options
End If
End Sub

3. Run the macro. A Yes/No message box should pop up. If the user clicks yes then the sum
of 2 + 3 will be shown in a second message box. If the click was No the program ends.

###

Stay tuned for Lesson #4!

Questions or comments? Email me: Emmett@scripting4v5.com

To download pre-written, ready to use macros, check out the Downloads page.

You might also like