Académique Documents
Professionnel Documents
Culture Documents
VBA at a glance
g
6
Lecture 1
Lecture 6
VBA
Sub main()
F
Function
ti
D
Declaration
l
ti
Di A
Dim
A, B
B, C,
C D
D, E A
As Double
D bl
Dim Message, Title, Default
Message = "A
" : " ' Set
S prompt.
Title = "InputBox" ' Set title.
Default = "0.0" ' Set default.
End Sub
VBA statement
Data Declaration
1 Numeric:
1.
N
i Integer,
I t
L
Long, Si
Single,
l Double,
D bl
Currency
Di A
Dim
A, B,
B C As
A Integer
I t
2. String
Dim M
Di
MyStr
St As
A String
St i
MyStr = SolidWorks
Lecture 6
InputBox
InputBox(prompt[,
p
(p
p [, title]
] [, default]
] [, xpos]
p ] [, yp
ypos]
])
Title
Message
Default
Lecture 6
MsgBox
MsgBox(prompt[, buttons] [, title] )
Dim Msg, Style, Help, Ctxt, Response, MyString
Msg
g = "Do y
you want to continue ?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "MsgBox Demonstration"
Response = MsgBox(Msg
MsgBox(Msg, Style
Style, Title)
Title
Msg
vbCritical
vbDefaultButton2
vbYesNo
(highlighted)
Lecture 6
MSgBox: Style
vbOKOnly
vbOKCancel
vbAbortRetryIgnore
vbYesNoCancel
vbYesNo
vbRetryCancel
vbCritical
vbDefaultButton1
vbDefaultButton2
vbDefaultButton3
vbDefaultButton4
Lecture 6
MsgBox
Display numeric value
Dim A As Double
A = 10
10.5
5
Dim Msg, Style, Title, Response
Msg = A
Style
Sty
e = vbOKOnly
bO O y
Title = Output "
Response = MsgBox(Msg, Style, Title)
Lecture 6
Arithmetic
Numeric
+
/
\
*
^
Mod
Add
Subtract
Divide
Integer Division
Multiply
Exponent (power of)
Remainder of division
5+5
10-5
25/5
20\3
5*4
3^3
20 Mod 6
String
&
+
Lecture 6
10
5
5
6
20
27
2
G B
AB
Mathematical Examples
Converting radian to degree
180
deg = rad
pi
pi = 4 * atn(1.0)
deg = rad
ad * 180.0
80 0 / pi
p
Sub main()
DIM deg, rad, pi AS DOUBLE
* input the rad
pi = 4 * atn(1.0)
deg = rad * 180.0 / pi
* display the result
End Sub
Lecture 6
10
Mathematical Examples
Converting degree to rad
pi
rad = deg
180
pi = 4 * atn(1.0)
ad = deg * p
pi / 180
80
rad
Length
g calculation
l=
x2 + y2
Lecture 6
11
Mathematical Examples
Quadratic Equation
b b 4ac
x1, x 2 =
2a
2
Note:
Equations can written using one
equation or using multiple
l l equations
(depends on individual)
Lecture 6
12
Mathematical Operators : +
+ : can be applied to string and number.
number
Dim A, B As DOUBLE
A=2
B=3
C = A + B C =23
23 (C = 2 + 3)
Therefore,
o , Val
a is used
u d to
o convert
o
to
o numerical
u
a
value
C = Val(A) + Val(B) C = 5
Lecture 6
13
Lecture 6
14
Function subr1()
Dim Msg, Style, Title, Response
Msg = A
Style = vbOKOnly
Title = "First subr"
Response = MsgBox(Msg, Style, Title)
End Function
Lecture 6
15
End Sub
Function func1()
End Function
Lecture 6
16
Hypotenuse = Sqr(A ^ 2 + B ^ 2)
End Function
Lecture 6
17
End If
End Sub
Lecture 6
18
Lecture 6
19
Task
C
Calculate
l l t th
the coordinates
di t off a
inscribed hexagon based on the
radius of the circle.
circle
Lecture 6
20
Procedure
1.
2.
Lecture 6
21
Programming: Step 1
Dim
i Message, Title
i l
Dim Default, Rad As Double
Message = Radius of the circle "
Titl = "I
Title
"InputBox"
tB "
Default = 10.0"
Rad = InputBox(Message
InputBox(Message, Title
Title, Default)
Lecture 6
22
Programming: Step 2
Note: ang is radian, therefore the ang in cos function is radian.
Dim pi, p1x, p1y, p2x, p2y As Double
pi
i = 4 * At
Atn(1)
(1)
p1x = Rad
p1y = 0
p2x = Rad * Cos(pi / 3)
p2y = Rad * Sin(pi / 3)
Lecture 6
23
Programming: Step 3
Verifying the program,
program by displaying and checking
the answer with known radis
Lecture 6
24
Lecture 6
25
Sett the
S
th sketch
k t h plane
l
Set the macro to record
Draw the line (single line)
Stop the recording
Save the macro
View the macro
Lecture 6
26
' *****************************************************
' C:\DOCUME~1\use\LOCALS~1\Temp\swx2956\Macro1.swb - macro
recorded on 02/22/09 by use
' *****************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Part.CreateLine2 -0.0445163235549, 0.08583865080093, 0,
0.07046754356449, -0.05908893171413,
0.05908893171413, 0
End Sub
Lecture 6
27
Createline2 Function
retval = ModelDoc2
ModelDoc2.CreateLine2
CreateLine2 ( p1x
p1x, p1y,
p1y p1z,
p1z p2x,
p2x p2y,
p2y
p2z)
Input:
(double )p1x
X value of the line start point
(double) p1y
Y value of the line start point
((double)) p
p1z
Z value of the line start p
point
(double) p2x
X value of the line end point
(double) p2y
Y value of the line end point
(double) p2z
Z value of the line end point
Return:
(LPDISPATCH) retval
Pointer to a Dispatch object, if the
operation fails, then NULL is returned
Lecture 6
28
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Conversion from the macro created
p1x =
p1y =
Part.CreateLine2 p1x, p1y, 0, p2x, p2y, 0
Or
Dim line1 As Object
Set line1 = Part.CreateLine2 (p1x, p1y, 0, p2x, p2y, 0)
End Sub
Lecture 6
29
Task 2
Write
W
it a program to
t create
t the
th hexagon
h
based on user input radius?
Lecture 6
30