Professional Documents
Culture Documents
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.
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()
There are multiple options for the types of buttons displayed on the message box pop-up
window.
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:
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)
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
'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.
###
To download pre-written, ready to use macros, check out the Downloads page.