Vba Foundations, Part 12: Paperspace
Vba Foundations, Part 12: Paperspace
My vision for the future is that PC systems will evolve to the point where the user is not even
aware that different applications are being invoked to produce a document. One essential element in this
vision is a common macro language. A common macro language will have several advantages for users.
First, it will be easy to use. Second, it will be the same in a variety of applications. Finally, by using
agentsa graphical interface "operative" that can cross applications boundariesusers can work in
one application and call parts of other applications into play as needed, or they can start from outside
any application and tie them together in various ways...
Bill Gates, Chief Software Architect, Microsoft, Inc., Guest Editorial, BasicPro Magazine, 1991
Ponder on that quote while I reassemble the pieces of our VBA puzzle. I began this series with the above quote as
a means to demonstrate both the philosophy and enthusiasm behind the visual basic phenomenon. This
introductory article paved the way for our further learning by reviewing the history of Visual Basic and
introducing and explaining some common terms. The second article continued our exploration of this graphically
rich and easy to learn software development environment by introducing the key components of the editor. The
third article jumped back into the editor and continued our exploration while further explaining some of the key
concepts of the interface. The fourth and fifth articles actually contained some example code utilizing the
command line interface and intrinsic objects. It was in the fourth article that our first macro was written. The fifth
article explored common programming constructs such as loops, variables, functions, etc. By the midpoint of this
series we were deeply exploring the built in functions provided in the VBA language itself and preparing for the
transition to AugiWorld magazine. Part seven began the exploration of the Visual portion of our studies by
looking at Userforms, objects, controls, and demonstrated the usage of some of the implied objects like Me
which make programming in VBA so much easier. Parts eight and nine were devoted to error trapping and
debugging and demonstrating how to best make use of the editor for these purposes. In part 10, we returned to the
core concepts of the VBA language, explored the Object Model and the tools used to expose it, and broached the
concept of events. The eleventh article explored the very heart and soul of VBA, events, which hold the true
power behind VBA.
Of course this series is merely an introduction to VBA and as such cannot be considered a complete and thorough
treatment of the subject, but to be thorough in this introduction I must honor my promise and finish by returning
to that original Bill Gates quote. The stated goal of a common macro language has been realized in the VBA
language. This commonality promotes code that can work both inside of and outside of AutoCAD as well as
many other VBA enabled applications. Note: there are now hundreds of applications that can be programmed in
this way. The key to controlling this type of automation application is a firm understanding of the applications
object model. Be aware that each applications object model is different, and is programmable to varying degrees.
Where some object models are well developed and allow access to virtually every interface and function, others
only allow minimal interaction. Remember too that once you become familiar with an object model or two, the
rest will fall into place quite easily and rapidly with but a little exploration.
Sept/Oct 2003
PaperSpace
p. 1
Sept/Oct 2003
PaperSpace
p. 2
Now it is your turn to explore, instead of just clicking the run button, try stepping through this code a line at a
time. Once you have made the connection, open the Locals window and explore the oApp object. You will see
a plethora of objects and properties you can access, modify and use to your hearts content. Take a look at Figure
12-3 for information on where to look for objects and properties available to you, the Excel master, while working
with this spread sheet application in a programmatic manner. (See Figure 12-3)
Sept/Oct 2003
PaperSpace
p. 3
Note: one of the idiosyncrasies of working with Excel is you must always remember to remove objects in
the reverse order of their creation. Otherwise you may leave a session of Excel running invisibly in the
background.
Once you get the hang of working with Excel, you will find that the other VBA enabled applications that are part
of Microsoft Office will work in a very similar manner.
Lets take a look at some routines written in one of AutoCADs vertical applications. In this example we will
access AutoCAD Architectural Desktop and create a routine that will search for a particular door number. If
found, then the routine will perform a Zoom Center command to display the door in the center of your work
screen.
To get started with this example: We must first
add some references. Start a new project and call
it Zoom2Door. In this project we will have a
module and a userform. Within your project open
the Tools menu and choose references. Make sure
that your list of references matches the reference
dialog box shown in Figure 12-4. In this project
we are going to make use of the VBA
Extensibility library as well as some of the
Architectural Desktop libraries. In addition, we
will utilize an undocumented custom control
provided for your use by the programmers at
Autodesk. This control may or may not be already
registered on your system. When you go to add it
to the toolbox, if it is not listed you should browse
to the following location and select it. C:\Program
Files\Common Files\Autodesk
Shared\AcFocusCtrl.dll. Add this control by
dragging it onto your form and giving it the
Sept/Oct 2003
PaperSpace
p. 4
Once you have the references added to your project, we can add the following routines. Select your UserForm
and switch to the code window. Ensure that we are working in Option Explicit mode. We will also set Option
Compare Text so that we can work without regard to upper versus lower case text in our searches. Since we are
using the AutoCAD focus Control for VBA, we must also program some events to control it. This control allows
AutoCAD users to interact with the AutoCAD menu, drawing window, and objects while the form is still being
displayed on top of AutoCADs window. Because of this we will want to be able to set focus, keep focus, and
relinquish focus. To that end you must also add a userform to your project and name it Zoom2Door, but dont
include the quotation marks. Add a textbox to this form and a command button. You should name the textbox,
txtDoorNum, again without the quotes. The Command buttons name is cmd_Find. See Figure 12-5 for the
layout I used. Once we have the form in place we will write code to handle our form during the following events:
txtDoorNum_MouseDown, cmd_Find_Click, and UserForm_MouseMove. (See Figure 12-6) We will
wish to be able to search for our door objects both within the current drawing and within any external references
that may be attached to the current drawing. For this we will add a subroutine to our form to do the current
Sept/Oct 2003
PaperSpace
p. 5
(Figure 12-6)
Sept/Oct 2003
PaperSpace
p. 6
(Figure 12-7)
Sept/Oct 2003
PaperSpace
p. 7
(Figure 12-8)
(Figure 12-9)
Note: The VBA extensibility library has many uses. Take some time to explore this library and see what it
offers.
Sept/Oct 2003
PaperSpace
p. 8
(Figure 12-10A)
Sept/Oct 2003
PaperSpace
p. 9
(Figure 12-10B)
This routine will be available in its entirety for your use by download at the Augi Exchange site by the time you
read this. Please do a search for Zoom2Door. Also take the time to explore other routines, symbols, and papers
available for your use at the Augi Exchange site.
As always, use the on-line help and the additional resources presented in this article to further explore these
concepts as necessary.
Sept/Oct 2003
PaperSpace
p. 10
Sept/Oct 2003
PaperSpace
p. 11