0% found this document useful (0 votes)
53 views15 pages

Autocadexcelvba 130727075544 Phpapp01

Uploaded by

cpiconj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views15 pages

Autocadexcelvba 130727075544 Phpapp01

Uploaded by

cpiconj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

This article is the third in a series on integrating the programming power of

AutoCAD VBA with the functionality found in other Windows applications


specifically, Microsoft !cel" #n the first segment we discussed what was possi$le, in
the second segment we discussed how to esta$lish a $asic lin% with !cel through
AutoCAD VBA to open an !cel session and close it down, and in this session we
discuss how to port formatted data from AutoCAD VBA to !cel wor%sheets"
Objects of Our Desire
&ow that you %now how to ma%e an AutoCAD VBA routine spea% '!cel(ese,' and
how to launch a session of !cel, let)s e!amine the Wor%$oo% and the Wor%sheet
o$*ects in !cel"
+ou can study the properties of these and other !cel o$*ects through the AutoCAD
VBA #D ,integrated de-elopment en-ironment. $y/
0" 1unning the #D ,use the VBA#D command."
2" 3sing the Tools41eferences pulldown menu option to acti-ate a Microsoft
!cel 5$*ect 6i$rary reference $y placing a chec% ne!t to this entry in the
1eferences list$o!"
7" 3sing the 5$*ect Browser tool ,use the 82 function %ey in the VBA #D or use
the View45$*ect Browser pull(down menu option. with the primary
Com$o$o! set to !cel ,see 8igure 0."
8igure 0/ The 5$*ect Browser"
View 6arger
8igure 2/ Create a 3ser8orm as shown"
The 5$*ect Browser reports the properties, classes, and -aria$le types associated with
o$*ects" 8or e!ample, if you select a Wor%sheet class, it has a Cells property as a
1ange class" #f you select the 1ange class, it has a Value property, which holds a
-ariant -alue" 6et)s use this information to pump different types of -alues ,integers,
real num$ers, and te!t. into cells in an opened spreadsheet"
0" 3se the process outlined in the last tutorial to create a 3ser8orm as shown in 8igure
2"
2" Add a Microsoft !cel 5$*ect 6i$rary reference to the form"
7" 9tretch the 3ser8orm and CommandButtons so that they can accommodate the
$utton te!t"
:" Apply the following code to the top $utton, CommandButton0, and $ottom $utton,
CommandButton7/
We added an nd command to the code for CommandButton7 so that when this
$utton is clic%ed, not only is the current spreadsheet closed, $ut the VBA routine is
also e!ited, thus returning us to the VBA #D" +ou can run the routine at this point,
$ut remem$er, once the !cel spreadsheet appears on your screen, you must toggle
$ac% to your AutoCAD session ,use A6T;TAB to mo-e $ac% and forth $etween the
current !cel session and AutoCAD software. to see your 3ser8orm" 6et)s e!amine
the code for CommandButton2"
Going Out for the Pass
The code for this CommandButton assumes that !cel is already running" Three
-aria$les are esta$lished to hold an integer, a real num$er, and a te!t -alue" These
-alues are passed to the !cel Wor%sheet o$*ect cell(-alue property using
,row,column. format and notation"
0" Apply the following code to CommandButton2/
2" 1un the application now"
7" Clic% on the top $utton to open a spreadsheet"
:" Clic% on the middle $utton to fill the first column with -alues"
<" Clic% the third $utton to shut down the application and the spreadsheet"
Note: !cel will not completely close down until after you)-e told it not to
sa-e the spreadsheet"
Excel VBA IDE
AutoCAD software isn)t the only application with a VBA #D" Microsoft !cel also
has a VBA #D, which you can access from the !cel main menu $y selecting Tools =
Macro = Visual Basic ditor ,see 8igure 7."
View 6arger
8igure 7/ Accessing the !cel VBA #D"
What if you want to close down the !cel spreadsheet without ha-ing to clic% a
DisplayAlert dialog $o! that as%s you if you want to sa-e the wor%$oo%> ?o to the
!cel VBA #D @elp facility to find this information ,DisplayAlert property of an
Application o$*ect."
0" 1eplace the code for CommandButton7 with the following code, which completely
closes !cel without first calling a DisplayAlert prompt/
2" 1un the application again, clic%ing on the top $utton and then the middle $utton"
Did you notice that the -alue 0"< was not reported properly in cell 2,0> This is
$ecause each cell has default formatting, and we ha-e not instructed !cel to change
its formatting once data has $een passed to its cells" With the 8ormat function you can
set the formatting for any cell $y passing formatted -alues to the cell"
7" Auit the application"
:" 1eplace the code for CommandButton2 with the following code/
<" 1estart the application"
B" 3se the top and middle $uttons to send the new -alues to the spreadsheet"
C" 1emem$er to stretch the column width of the first column to accommodate the
-alues passed to it"
The resulting -alues passed to the spreadsheet should loo% li%e those shown in 8igure
:"
8igure :/ The -alues are passed to the spreadsheet"
Keeping p Appearances
#n addition to cell -alue formatting, you can also control cell siDing, $oldfacing,
italiciDing, and column widths"
0" 1eplace the code in CommandButton2 with the following code/
2" 1un the application again to see how to control the appearance of -alues"
!o"e on the #ange
9ometimes you will want to pass a -alue to many cells or to pass a function to a cell
for a spreadsheet calculation" 9o let)s end this segment $y e!amining the code that
accomplishes this"
2" 1un the application again to see the effect"
&ot much needs to $e said here" The code uses the 1ange class of the o$*ect with a
cell range format -alue and the 8ormula property of the 1ange class to assign a
formula to a cell"
The -alue of : is passed to four cells, and the a-erage, sum, and a su$traction of
-alues is calculated in ad*acent cells"
&e!t time, we)ll discuss pulling -alues from an !cel spreadsheet into AutoCAD
VBA o$*ects such as 6istBo!es and Com$oBo!es, and we)ll also get our first loo% at
code to sur-ey counts of o$*ects within an AutoCAD drawing and report them in a
spreadsheet"
VBA: Integrating with Microsoft Excel - Part 2
By da-e espinosa(aguilar
This article is the second in a series on integrating the programming power of
AutoCAD VBA with the functionality found in other Windows applications
specifically, Microsoft !cel" #n the first article, we discussed what is possi$leE now
it)s time to e!amine how it)s possi$le" And that $egins with a Fuic% o-er-iew of the
AutoCAD VBA #ntegrated De-elopment n-ironment ,or #D."
$he IDE
At the command prompt, enter the command VBA#D" This initialiDes the #D in a
separate window" +ou can use the A6T;TAB %eys to switch $ac% and forth $etween
the #D and your AutoCAD session" #f you close AutoCAD, the #D window closes
with it" The #D is used to de-elop and de$ug your program code and your dialogs
and to -iew the information your program is processing ,see 8igure 0." 5nce you ha-e
finished using the #D ,usually after sa-ing your programming wor%., you can close
the #D window without closing AutoCAD"
View 6arger
8igure 0/ The #D ena$les you to simultaneously -iew your application user forms,
code, the o$*ect $rowser, the tool$o!, o$*ect properties, references and more"
$he VBA %anager
+ou can also $ring up the #D through the use of the VBAMA& command" 3nli%e
VBA#D, the VBAMA& command ena$les you to load ,and unload. e!isting VBA
pro*ect files to4from memory $efore *umping to the #D to de-elop or modify them"
+ou can wor% with se-eral pro*ect files open at once" When you use the VBAMA&
command, the VBA Manager dialog $o! opens" The Visual Basic ditor $utton in this
dialog $o! ta%es you to the #D ,see 8igure 2." The VBA Manager is also where you
create new pro*ect files, de-elop macros to run your pro*ect files, or em$ed your
programs in any of your open drawings"
8igure 2/ The VBA Manager Dialog loads, unloads, and e!ports application code"
Our &irst ser Progra"
1ather than try to e!plain e-ery $utton and function in these interfaces, we)re going to
go straight to de-eloping our -ery first program" #t)s o$*ecti-e is simple enough/ open
and close !cel" Ma%e sure you ha-e !cel already installed on your system, and then
follow along as we $egin to de-elop our first application/
0" 9tart $y ma%ing sure you)re in a $rand new AutoCAD session"
2" 3se the VBAMA& command to $ring up the VBA Manager dialog $o!"
7" Clic% the &ew $utton" This creates a new pro*ect with a default pro*ect name
,ACADGro*ect.
:" Clic% the Visual Basic ditor $utton to go to the #D"
<" nter Control;1 or select Gro*ect !plorer from the View pull(down menu to -iew
the Gro*ect !plorer" #t may $e doc%ed to one side of your window or it may $e a
floating window" #n either case, it should $e reporting your ACADpro*ect"
B" #f the pro*ect name displays a ; ,plus. sign to the left, clic% on the plus sign to see
su$item ThisDrawing ,see 8igure 7."
8igure 7/ The pro*ect window reports pro*ects loaded, and 3ser8orms included in a
pro*ect"
View 6arger
8igure :/ A 3ser8orm is created for the application" The Gro*ect window and
Tool$o! are organiDed for easy o$*ect placement"
C" 8rom the #nsert pull(down menu, select 3ser8orm pull(down to insert a 3ser8orm
into this pro*ect" 9tretch the 3ser8orm dialog $o! and the window the 3ser8orm sits
in so that they ta%e up appro!imately the space shown in 8igure :"
H" 8rom the View pull(down menu select Tool$o!, which opens the Tool$o! dialog
$o!" +ou can also stretch the Tool$o! dialog $o!"
'etting p an Excel #eference
5nce you)-e added the 3ser8orm to the pro*ect, the ne!t step is telling AutoCAD
software how to spea% !cel(ese" 1eferences e!pand AutoCAD VBA to include new
tools and capa$ilities, and for this application we need to $e a$le to spea% in terms of
wor%sheets, cells, ranges, and other concepts associated with spreadsheets" @ere)s
how we do it/
0" 8rom the Tools pull(down menu, select 1eferences, which $rings up the 1eferences
dialog $o!"
2" 9e-eral references at the top of the A-aila$le 1eferences list may already $e
selected" 6ea-e them as they are, and search down through the list of a-aila$le
references until you see one that loo%s li%e Microsoft !cel I"J 5$*ect 6i$rary ,this
assumes Microsoft !cel 2JJJ is already installed." 9elect the $o! to the left of the
6i$rary as shown in 8igure <" When you do this, the 6ocation la$el at the $ottom of
the dialog $o! should report a path similar to c/KGrogram 8ilesKMicrosoft
5fficeK5fficeKLC6I"56B"
8igure </ Adding the Microsoft !cel I"J 5$*ect 6i$rary reference ena$les your
application to spea% '!cel(ese"'
7" Clic% the 5M $utton, which returns you to the #D"
That)s all it ta%es to e!pand our application to spea% in !cel terms"
A((ing Buttons to the ser&or"
&ow that you)-e told the application how to spea% !cel(ese, let)s create some $uttons
on our 3ser8orm dialog $o! that launch !cel, close !cel, and Auit out of our
application" @ere we go/
0" Mo-e your cursor o-er the -arious o$*ects in the Tool$o! dialog $o! and pay close
attention to the tooltips" 8ind the tool with tooltip CommandButton and clic% it" +our
cursor changes to CommandButton draw mode"
View 6arger
8igure B/ +ou can re(organiDe the loo% and layout of your application and
de-elopment interfaces at any time"
2" Mo-e your cursor o-er the 3ser8orm and pic% an upper(left point" This will spot a
$utton on the 3ser8orm" Clic% and drag on the $utton grips to stretch it into any siDe
you li%e" Create two more $uttons and stretch them so that they appear similar to those
shown in 8igure B"
7" 9lowly clic% twice o-er a $utton to mo-e the cursor inside the $utton, ena$ling you
to change the $utton)s la$el"
#f you clic% twice too Fuic%ly, a code window will appear instead" Close out any code
windows that appear and try again if necessary, clic%ing slowly inside each $utton to
mo-e the cursor into the $utton te!t"
:" Change the la$el for each $utton so that CommandButton0 te!t reads 6aunch !cel,
CommandButton2 te!t reads Close !cel, and CommandButton7 te!t reads Auit as
shown in 8igure C"
8igure C/ #t is a helpful practice to include a Auit $utton in your primary 3ser8orm to
pro-ide an easy means to stop your application when it is running"
)our &irst *o(e( Object
&ow that you ha-e finished the dialog $o!, it)s time assign code to each $utton"
Again, we)re going to start -ery simple $y writing the code for the Auit $utton"
0" Dou$le(clic% the Auit $utton to $ring up the $utton)s code window"
The code window shows a com$o $o! with the -alue CommandButton7 in the upper(
left corner so you %now the code you)re writing will pertain to this $utton only" #n the
upper(right corner of the code window is another com$o $o! with -alue Clic%, which
tells you that the code will $e called up when this $utton is clic%ed while the
application is running" #n the main part of the code window are two already typed
statements/
Private Sub CommandButton3_Click()
End Sub
2" Clic% $etween these two statements and enter the word nd so that the code
statement now appears as/
Private Sub CommandButton3_Click()
End
End Sub
ach $utton acts as a routine unto itself" The &D command in Visual Basic
terminates a running application" We)re going to run our little program and use this
$utton to stop it" The indenting of the command isn)t -ital to the program running
correctly, $ut it does help %eep command statements -isually organiDed"
7" Close the code window"
:" 8rom the 1un pull(down menu, select 1un 9u$ = 3ser8orm menu item to launch
our program"
+our dialog $o! should appear o-er your AutoCAD session" Clic%ing on the upper
two $uttons will do nothing, $ut if you clic% the Auit $utton, you should return to the
#D"
<" Clic% the Auit $utton to terminate the program"
+ou *ust ran your first programN +ou can also run a program $y typing the 8< %ey
when you are in the #D"
+aunching an( *losing Excel
This ne!t part is going to ta%e some e!plaining" But first, let)s get the code for the top
two $uttons assigned"
0" Dou$le(clic% the 6aunch !cel $utton to $ring up its code window and type the
following code into it/
2" 6i%ewise, dou$le(clic% the Close !cel $utton to $ring up its code window and type
the following code into it/
6et)s ta%e a closer loo% at the code for the 6aunch !cel $utton" The code for this
o$*ect $egins $y dimensioning -aria$le e!celApp as an !cel application o$*ect type,
-aria$le w$%5$* as an !cel Wor%$oo% o$*ect type, and -aria$le sht5$* as an !cel
Wor%sheet o$*ect type ,all three of which are types now a-aila$le to us with the e!cel
I"J 5$*ect 6i$rary referenced." #f any error occurs, the code is instructed to continue
on instead of stopping at the error" The 3ser8orm is hidden from -iew and the error
state -aria$le is cleared ,set to a -alue of Dero." @ere)s the rele-ant code/
&e!t, the application -aria$le is set to a currently running session of !cel" This may
seem a little weird at first, $ut the code does not start $y launching !cel" &ow, if
!cel isn)t running, this statement generates an error ,which has a nonDero -alue." #f
an error is generated, then we %now that !cel isn)t running yet" 9o we clear the error
state and use the Create5$*ect function to launch an !cel session" 9o we)re
essentially saying '#f !cel is already running, set the -aria$le to what is running"
5therwise, start up !cel"' #f launching !cel generates an error, we %now that !cel
isn)t e-en installed" We report this through a message $o! e!clamation function and
end the program" @ere)s that code/
5nce the application -aria$le is set to either the currently running session of !cel or
a new session of !cel, the 3ser8orm is made -isi$le again, and the wor%$oo% and
wor%sheet -aria$les are set to the first wor%$oo% and the first wor%sheet in that
session" This done, the 3ser8orm is made -isi$le again so that we can clic% the
$uttons to close !cel down or Fuit our VBA program" @ere)s that code/
The code for the Close !cel $utton is fairly imitati-e of that for the 6aunch !cel
$utton" Varia$le e!celApp is dimensioned from scratch again in this procedure as an
!cel application o$*ect type" The code is instructed again to continue if an error is
encountered, the 3ser8orm is hidden, and the error state is cleared" Varia$le e!celAGG
is set to an assumed already running session of !cel" #f !cel isn)t running ,someone
may clic% Close !cel firstafter all, you ha-e to consider e-ery possi$ility., an error
is generated, and we are told !cel isn)t running" #f no error is generated, then the
application Fuits" The 3ser8orm is made -isi$le again so that we can clic% our three
$uttons/
When running this VBA routine, it helps to use A6T;TAB to switch $etween !cel
and AutoCAD after !cel has $een launched" Viewing the Windows Tas% Manager
,CT6;A6T;D6. is also helpful to chec% if you)-e left any !cel application open"
The code in the Close !cel $utton does shut down the currently running session, $ut
if you chec% the Tas% Manager after using Close !cel, you should see an icon for the
currently running !cel session" +ou can always shut down any !cel sessions
through the Tas% Manager if needed" What happens if you press 6aunch !cel twice
in a row> What happens if you then press Close !cel>
In *onclusion
#n future segments we)ll discuss how to pass information to !cel from our VBA
routine, how to format that passed information, and how to channel it to a single cell
or multiple cells and ranges" We)ll also loo% at how to pull information from a
spreadsheet into a 3ser8orm list $o!, edit $o!, com$o $o!, and so on" 8or now, ma%e
sure you understand clearly how to ma%e the !cel connection"

You might also like