I gave up being a programmer over fifteen years ago. After about fifteen years spent programming, I realized that programmers are the 21st-century bricklayers, and that they will always be underfunded, overscheduled, and uncredited. Rather than building houses, I'd rather be the architect designing them.
Plus I just couldn't wrap my head around object-oriented programming. Or if it was possible, I was by then so finished with programming that I couldn't get up the energy or interest to do it.
But that doesn't mean I don't ever code. It just means I don't code very much.
Whenever I do write code, any accomplishments, however minor, are extra-special since I'm no longer a coder. Back in the mid-Nineties I wrote a piece of C-language code that would import all-capital-letters information from a mainframe, and translate it into appropriately capitalized-and-lower-case information. I remember how pleased I was when it worked.
Likewise, I modified a perl script that I found on the internet to turn it into a fairly robust image slideshow viewer.
So today's accomplishment was minor, but pleasing. Put simply, I've been re-creating the ability to build Visio diagrams with Layer buttons. This allows for parts of a diagram to be displayed and hidden - for example, a diagram of a street could have one button for "Cars", one for "People", and another for "Both," and each button would show what's on its label and hide what isn't.
Building each button takes a block of Visual Basic code that looks like this:
Dim LayerObj As Visio.Layer
Dim LayerName As String
Dim LayerCellObj As Visio.Cell
Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
If LayerObj.Name = "Cars" Then
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
LayerCellObj.Formula = True
Else
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
LayerCellObj.Formula = False
End If
Next
The problem I was running into was that if you have a lot of buttons, you repeat that block of code very time. For instance, if I wanted "Cars" and "People" buttons, I'd have to duplicate that entire block of code, changing the word "Cars' to "People."
Then I started to create additional sheets in Visio, and I discovered that each button on EVERY sheet would have one of these entries in the same code file. Now the code block would go on for pages, with a big different block for each button on every sheet, and all the layer names extended across every sheet. This was going to be terribly clumsy.
Finally, if one wanted to show ALL layers again, or contrariwise show NO layers, then one needs those buttons on each sheet, an each of those buttons on every sheet would have an associated big block of code. This was going to be a mess.
What I needed to do was figure out a way to re-use the same block of code over again, with different label names - what's called a "subroutine" in coding language. Problem was, I had no idea how to specify such a thing in the Visual Basic coding language.
I started fooling around with it, but I didn't have a lot of time. I tried a few intuitive guesses, but when I started to find myself using Google looking for instructions, I figured I ought to give up. I didn't have the time to fool around digging in Google.
So I finished what I was doing, closed my quotations and parentheses, and saved my work. Then, more out of habit than anything else, I clicked the button.
It worked!
I didn't even know what I'd done to get it right. I'd simply closed quotes and parenthesis in a way that seemed reasonable, and amazingly I got the function correctly formatted, almost by accident.
Private Sub ShowLayer(ThisLayer)
Dim LayersObj As Visio.Layers
Dim LayerObj As Visio.Layer
Dim LayerName As String
Dim LayerCellObj As Visio.Cell
Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
LayerName = LayerObj.Name
' Debug.Print LayerName
If LayerName = ThisLayer Then
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
LayerCellObj.Formula = True
Else
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
LayerCellObj.Formula = False
End If
Next
End Sub
Private Sub CommandButton1_Click()
'Example page Layer1 button
Call ShowLayer("Layer1")
End Sub
So now I have a handy little set of buttons and subroutines to let me set up more buttons. Instead of reproducing a big block of code, all I do is call the ShowLayer subroutine with the name of the layer to activate, or the AllLayers subroutine to turn all layers on or off.
If you're a Visio user who wants to learn how to make buttons, take a look at my example file and hopefully it will be helpful for you!
Posted by Albatross at April 8, 2008 11:33 AM | TrackBack