![]() |
![]() ![]() |
Pearls from the auld Scotsman |
Association: Glue Wi'oot Gluin' in VisioPart ICeuid Mile Failte!! Grand tae see ye a back again. In this article we immerse ourselves deeper into VBA code and demonstrate how two shapes can be positionally related to each other without having to be glued to each other. But
first a word from our sponsors
Now these are not applied equally. Any 1-D shape
can glue to any of the above selected items. A 2-D shape can only glue to Guides and
Guide-points. A control handle on a 2-D shape can glue to any of the above selected items
as long as it is positionally neither locked in the X or Y direction or obviously both.
When glue is established, moving the shape that has been GLUED TO (i.e., the target shape)
will also move the shape that is GLUING (i.e. the source shape). Moving the shape that is
doing the gluing breaks the glue and freely moves only that particular shape. Another mechanism for establishing relationships between
shapes is GROUPING. This operation, created by selecting Shape > Grouping > Group,
wraps a "shell" around the selected shapes and allows them to function and move
as one object. The price that you pay for this convenience is the loss of access to the
individual shapes custom properties as well as greater difficulty in accessing the
individual shapes text and formatting. Now back to our
programme
So the first step in this process is to create a wee dialogue in VBA to accept the user input and display the parameters that need to be passed to the programme. The form you will generate will look like the one shown below:
In order to create this dialogue, you will need to launch a new session of Visio, and begin a new drawing based on Blank Drawing.VST. Now go ahead and launch VBA by selecting the Alt+F11 keys. This will bring up the VBA IDE. In the VBA IDE, select Insert > UserForm from the menus. A new blank form will be created and a tool palette will appear allowing you to add controls to the form. You will need to use the Label control (thats the one that looks like a capital letter A), the Text Box control (thats the one that looks like a lower case ab with a vertical bar), a Frame control (thats the one that looks like a wee box with an xyz at its top), and a CommandButton control (thats the one that looks like a wee blank button). Go ahead and lay out your dialogue (form) so that it resembles the one shown above. Grand!! Next select the top-most Text Box and then look at the properties window at the left of the VBA IDE. In that Properties window you will see a large listing of all of the properties associated with that Text Box control. Find the Name property (it should be the very first item). Change it from its default value to LeaderShape. Do the same thing with the next Text Box control down, changing its Name property to FollowShape. Aboot now ye might be askin WHY ye are goin through this exercise. Weel you could go on merrily building and using the dialogue and associated controls, making use of their default control names, however giving them recognisable names rather than the default TextBox1 and TextBox2 will GREATLY assist you in understanding just what these controls are for as you work with them. Take my word for it, you will thank me later on. For each of the LeaderShape and FollowShape Text Box controls also change the Enabled property to False. This will ensure that you can display a value in them, store a value as weel, but the user can not change the value you put there. All right. We need to change the names of just a few more controls and we can begin coding. Change the Name property of the Text Box control to the right of the Label control "In X" to be a value of OffsetX and the Name property of the Text Box control to the right of the Label control "In Y" to be a value of OffsetY. Next change the Name property and the Caption property of each of the 4 buttons as follows: From Top to Bottom they should be:
Finally select just the overall form and change its Caption property to Associated Offset. I will presume that you can resize all of the elements to get the best fit and look for your dialogue. Visual design done, we will now turn our attentions to writing the code behind these controls to actually run the application. The first thing we need to do is create a procedure, known as a Sub-Routine, to launch the User Form. To do this, in the VBA IDE go to the Insert Menu and select Insert > Module. Note that in your Project window in the upper-left of the VBA IDE your project now shows an item in the project explorer tree called Modules and beneath that, Module 1. Double-Click on the Module 1 item. This will launch the Code Window for this module. By default you will be looking at the General Declarations for this module. Enter the following code exactly as I have listed it below:
Notice that as soon as you finish entering the first line of code and hit enter, VBA fills in the last line for you. This is VBAs automatically generating a procedure framework. Each Sub-Routine must end with an End Sub. It begins with the declaration of its scope, in this case Public, lists its procedure type, in this case Sub-Routine (Sub) and then the passed arguments within parentheses, in this case void. As stated before, it ends with an End keyword and declaration of what is ending, in this case the Sub. Now between this procedure framework, we will enter 3 lines of code. The line On Error GoTo Bail is the beginning of a small error handler. It is designed to trap for the user doing something foolish like attempting to terminate the routine prior to the completion of the initialisation of the form. The word Bail is simply a label, it could have been GoTo JumpOver or any name. The next line UserForm1.Show is the Object and Method for actually displaying the User Form. Show is the Method acting on the UserForm1 Object. The next line Bail: is the line label for the code to jump to should the code encounter an error condition. In this case the code simply bypasses Show method and delivers the code sequence to the end of the procedure. Take a spell now and absorb your new-found skills. I know ye'll not rest fer long and soon be back to get yer teeth into Part II. "Haste ye back." |
|
|
Copyright © 1998-2007 DBM & others | Disclaimer | Privacy | Re-publication | Trademarks | Webmaster | Home |