General

Customize the Visio toolbar and keep macros handy(ish)… Part II

Hey, I return to the topic of Visio customisation to implement actual buttons for my macros instead of using the time wasting menu drill down through Tools/Macros/Document1/Module1/My_Macro.

I didn’t do this before because there is no functionality to add macro’s as buttons in the  Customise Toolbar dialog. However I have now learned that it is possible to do this programmatically. So, based on my past discoveries, I now have 2 new tools handy on my Standard toolbar: the first sets a fixed grid of 2mm x 2mm; the second fits the drawing page to the contents. FYI this saves me 8 clicks + 2 keystrokes; and 5 clicks respectively.

I added all the code to the BASFLO_M.VSS shape template as I normally only use this.

The following goes in the ThisDocument object to create the buttons as soon as the shape template (or a drawing created using the shape template) is opened:

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
AddGridButton
AddFitPageButton
End Sub

The code to create the buttons goes into a Module along with the procedures (the actual macro’s you want to run):

Public Sub AddGridButton()
On Error Resume Next
' Check for existing custom button by the Tag property to avoid creating duplicates
Set checkControl = Application.CommandBars.FindControl(, , "GridButton")
' If no button with this Tag exists then it's OK to create the button
If checkControl.Tag = "" Then
Dim standardBuiltInBar As CommandBar
Dim newButton As CommandBarButton
Dim picPicture As IPictureDisp
Dim picMask As IPictureDisp
' Add the new control to the Standard toolbar
Set standardBuiltInBar = Application.CommandBars("Standard")
Set newButton = standardBuiltInBar.Controls.Add(msoControlButton)
' Get new button face as custom 16x16 Bitmap consisting of a Picture and a transparency Mask
Set picPicture = stdole.StdFunctions.LoadPicture("C:\Program Files\Microsoft Office\Visio11\1033\grid.bmp")
Set picMask = stdole.StdFunctions.LoadPicture("C:\Program Files\Microsoft Office\Visio11\1033\gridmask.bmp")

With newButton
.Picture = picPicture
.Mask = picMask
' OnAction syntax is PROJECT_NAME!MODULE_NAME.PROCEDURE_NAME
.OnAction = "BASFLO_M!CustomProcedures.SetStandardGrid"
.Tag = "GridButton"
.TooltipText = "2mm Fixed Grid"
.Visible = True
End With
End If
End Sub

Public Sub AddFitPageButton()
On Error Resume Next
' Check for existing custom button by the Tag property to avoid creating duplicates
Set checkControl = Application.CommandBars.FindControl(, , Tag:="FitPageButton")
' If no button with this Tag exists then it's OK to create the button
If checkControl.Tag = "" Then
Dim standardBuiltInBar As CommandBar
Dim newButton As CommandBarButton
Dim picPicture As IPictureDisp
Dim picMask As IPictureDisp

' Add the new control to the Standard toolbar
Set standardBuiltInBar = Application.CommandBars("Standard")
Set newButton = standardBuiltInBar.Controls.Add(msoControlButton)
' Get new button face as custom 16x16 Bitmap consisting of a Picture and a transparency Mask
Set picPicture = stdole.StdFunctions.LoadPicture("C:\Program Files\Microsoft Office\Visio11\1033\fitpage.bmp")
Set picMask = stdole.StdFunctions.LoadPicture("C:\Program Files\Microsoft Office\Visio11\1033\fitpagemask.bmp")

With newButton
.Picture = picPicture
.Mask = picMask
' OnAction syntax is PROJECT_NAME!MODULE_NAME.PROCEDURE_NAME
.OnAction = "BASFLO_M!CustomProcedures.FitPageToDrawing"
.Tag = "FitPageButton"
.TooltipText = "Fit Page to Drawing"
.Visible = True
End With
End If
End Sub

Public Sub SetStandardGrid()
Dim UndoScopeID2 As Long
UndoScopeID2 = Application.BeginUndoScope("Set Grid")
Dim vsoShape1 As Shape
Set vsoShape1 = Application.ActiveWindow.Page.PageSheet
vsoShape1.CellsSRC(visSectionObject, visRowRulerGrid, visXGridDensity).FormulaU = "0"
vsoShape1.CellsSRC(visSectionObject, visRowRulerGrid, visYGridDensity).FormulaU = "0"
vsoShape1.CellsSRC(visSectionObject, visRowRulerGrid, visXGridSpacing).FormulaU = "2 mm"
vsoShape1.CellsSRC(visSectionObject, visRowRulerGrid, visYGridSpacing).FormulaU = "2 mm"
Application.EndUndoScope UndoScopeID2, True
End Sub

Public Sub FitPageToDrawing()
Dim vsoShape As Visio.Shape
Dim UndoScopeID1 As Long
If Application.ActivePage.PageSheet.Shapes.Count = 0 Then Exit Sub
UndoScopeID1 = Application.BeginUndoScope("Fit Page")
Application.ActiveWindow.SelectAll
Set vsoShape = ActiveWindow.Selection.Group
h = vsoShape.Cells("Height")
w = vsoShape.Cells("Width")
Application.ActivePage.Background = False
Application.ActivePage.BackPage = ""
Application.ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageWidth).FormulaU = Str(w)
Application.ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = Str(h)
Application.ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageDrawSizeType).FormulaU = "1"
Application.ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPageOrientation).FormulaU = "2"
Application.ActiveWindow.Page.CenterDrawing
Application.EndUndoScope UndoScopeID1, True
vsoShape.Ungroup
End Sub

You will need to download and save the following custom button face Bitmaps into C:\Program Files\Microsoft Office\Visio**\1033 if you want to try out my example.
gridmask.bmp
grid.bmp
fitpagemask.bmp
fitpage.bmp

Proper Titlecase for MS Word

[Code sample updated to fix bugs on 21/06/2009]

I use Titlecase a lot in business documents so I was never quite satisfied with the “dumb” version available by default in MS Word which just capitalises the first letter of every word.

After researching some of the conventional rules for Titlecase I started building a Visual Basic procedure to apply these to the Selection in Word. It was a struggle to get it to a state where the rules were correctly applied and then after running some tests another major obstacle reared it’s ugly head: paragraph and character formatting were being lost.

It eventually dawned on me that I should be using the Word Object Model. After some more research lo and behold the awesome power of the Selection.Words.Item object which breaks down the selection to “words” where punctuation and symbols are nicely treated as separate “words” themselves.

To speed things up and for a more visually appealing implementation I turned ScreenUpdating off. It runs at approximately 1 second for 100 words consisting of 500 characters. You really shouldn’t be Titlecasing very large blocks of text so this is fine for most cases.

Sub TitleCase()
Application.ScreenUpdating = False
Dim a, b, c, d, e, f, g, h, p1, p2, pc, wc, x As Integer
Dim Exceptions, Pre1, Pre2, Suf1, Suf2 As Variant
Dim WorkStr As String
Dim myDup() As Object
Dim myRange As Object
Dim mySelection As Object
Dim myIndex()
'
' RULES APPLIED:
' =============
' Rule 1: Always Titlecase the first and last word of the title.
' Rule 2: Lowercase all prepositions of 4 or fewer letters.
' Rule 3: Lowercase all articles (a, an, the).
' Rule 4: Lowercase all coordinate conjunctions (and, but, for, nor, or,
' so, yet).
' Rule 5: Lowercase the second word in compound words if it is a preposition.
'
' RULES NOT APPLIED:
' =================
' Rule 6: Never use periods or exclamation marks.
' Rule 7: Lowercase the "to" in an infinitive, e.g. "to Be" (requires
' grammar check which is beyond the scope of this Subroutine).
'

Exceptions = Array("A", "An", "The", "And", "But", "Or", "Nor", _
"Amid", "As", "At", "Atop", "But", "By", "Down", "For", "From", "In", _
"Into", "Like", "Mid", "Near", "Next", "Of", "Off", "On", "Onto", "Out", _
"Over", "Pace", "Past", "Per", "Plus", "Save", "So", "Than", "Till", _
"To", "Up", "Upon", "Via", "With", "Yet")
e = UBound(Exceptions)
Pre1 = Array(" ", "(", "[", "{ ", "-", " ", " ", " ", Chr(34), Chr(145), _
Chr(147))
Suf1 = Array(" ", ", ", ")", "]", ":", ";", "/", "}", Chr(34), Chr(146), _
Chr(148))
p1 = UBound(Pre1)
Pre2 = Array(". ", "! ", "? ", ". ")
Suf2 = Array(" ", ", ", "; ", ": ")
p2 = UBound(Pre2)
pc = Selection.Paragraphs.Count
wc = Selection.Words.Count
ReDim myDup(pc - 1)
ReDim myIndex(wc - 1)
Set myRange = Selection.Range
Set mySelection = Selection

For x = 0 To pc - 1
Set myDup(x) = Selection.Paragraphs(x + 1).Format.Duplicate
Next x
'
' Convert each Word in the Selection Object to Titlecase to begin with
'

For x = 1 To wc
myRange.Select ' This is needed - after each iteration the Selection is dropped
d = mySelection.Words.Item(x).Text
myIndex(x - 1) = Len(d)
WorkStr = WorkStr & StrConv(d, vbProperCase)
Next x

If wc = 2 Then GoTo SkipRules
'
' Scan and replace according to the above Rules
'

For a = 0 To e
For b = 0 To p1
For c = 0 To p1
'
' Lowercase prepositions, articles, conjunctions and the second word
' in compound words if it is a preposition
'

WorkStr = Replace(WorkStr, Pre1(b) & Exceptions(a) & Suf1(c), Pre1(b) _
& LCase(Exceptions(a)) & Suf1(c))
Next c
Next b

For b = 0 To p2
For c = 0 To p2
'
' Fix captitalisation of prepositions, articles and conjunctions found
' at the beginning of sentences
'

WorkStr = Replace(WorkStr, Pre2(b) & LCase(Exceptions(a)) & Suf2(c), _
Pre2(b) & Exceptions(a) & Suf2(c))

Next c
Next b
Next a
SkipRules:
'
' Write the correctly cased Words back to the Selection. By writing text
' values back to the Text Property of the Words Item Object ensures
' that all character formatting is retained
'

For x = 1 To wc
f = myIndex(x - 1)
g = Left(WorkStr, f)
h = Len(WorkStr)
WorkStr = Right(WorkStr, h - f)
myRange.Select
mySelection.Words.Item(x).Text = g
Next x

For x = 0 To pc - 1
Selection.Paragraphs(x + 1).Format = myDup(x)
Next x

Application.ScreenUpdating = True
End Sub

iTunes a Misnomer

Let me get this straight – the iTunes Store for South Africa doesn’t sell tunes? Duh! Wasn’t that the whole point of iTunes? What a rotten apple this is!

A visit to this “so-called” music store revceals that all we miserable bastards in South Africa can download is useless utilities and other crap that won’t even work on a “normal” iPod. This App Store has so far only offered me shit for an iPod Touch or iPhone – neither of which I own or will ever own anytime soon. Not even a single game is available for my iPod.

Someone said the lack of music was due to local laws – so yet another fine thing to be grateful for from those arseholes in Pretoria! I love you guys so, so much!

Does anyone know of an alternative to iTunes for purchasing cheap music online?

WELL DONE SHARKS!!!!!!

Blood, sweat and VICTORY!

Customize the Visio toolbar and keep macros handy(ish)…

The consultants I work for are always needing process flow diagrams and so I use Visio a lot. However my efforts to boost productivity by customizing the interface have always been frustrated because, try as I might, I couldn’t get Visio to retain any of my changes to the toolbar from one drawing to the next. It appeared at the time that these changes (as well as any macros I might have recorded) could only be saved in the actual drawing file currently open and would thus vanish as soon as I started a new drawing. Unlike Word and Excel which have the Normal document and Personal workbook templates respectively, Visio has no such thing. Visio also cannot create custom buttons so you can only assign shortcut keys to run your macro.

By chance I made an amazing discovery (second best this week). Open Visio without a drawing page, change your toolbar buttons as desired and close Visio. Then when you open it again all your tool changes are still there, and they stay there no matter how many different drawings you work on. Super!

One trick I’ve always wanted to automate was resizing the printed page to fit the drawing contents so that drawings embedded as objects in Word documents would be correctly framed within the boundary of the object and not zoomed to a portion of the drawing only. This usually requires navigating in Visio through File/Page Setup/Page Size tab and checking the Size to fit drawing contents option. The best thing I’ve learned this week was that macros can be stored in Stencil files too. I nearly always use the Basic Flowchart (Metric) stencil (a.k.a. BASFLO_M.VSS) so I created my “fix my page” macro in this stencil so it will be available for 99% of my work. Of course the stencils are opened as Read Only so you need to save it under another name and then, after closing Visio, rename the original stencil as a .bkp file and rename your new stencil to replace the original one i.e. BASFLO_M.VSS.

B.t.w. the stencils are found in C:\Program Files\Microsoft Office\Visio**\1033

Bad First Impression of Vista!

I’ve only caught rare glimpses of Vista in action but thanks to my employer dropping his laptop and getting a new one with Vista Ultimute pre-installed I’ve had the dubious pleasure of making it’s acquaintance. Wow! It really is breathtakingly beautiful, an absolute visual feast of ultra-cool flavours.

I was secretly glad to spend some quality time with it transferring all his old data, but then of course Micro$oft threw “shit” at the this “would-be” fan.

  1. Vista doesn’t open NTBackup files
  2. Vista doesn’t play nice with XP computers on your simple home network
  3. Five days later and sharing our previously shared printer still isn’t happening

Who would have expected that trying to get a Micro$oft product to actually work with a previous version of itself could prove so problematic. I needed to spend almost a day combing through numerous forums to learn that:

  1. I need to find, download, and install a small application to open NTBackup files in Vista called the “Windows NT Backup – Restore” utility. I found this out here. Isn’t this such an obvious need that it should have been part of the Vista package to begin with?
  2. I need to find, download an obscure gizmo / protocol called the Link Layer Topology Discovery (LLTD), previously unheard of by the common people, on all the XP machines otherwise I can’t see them in the network map. Again, wasn’t this obviously something that could have been accommodated in Vitsa itself instead of having me fix the problem myself?
  3. No one seems to have a solution to my shared printer dilemma.

The upshot of all this is that my employer now want’s XP back on his machine but, joy of joys, he no longer has his CD Key.

Virus Killer Killer Killer

There once was a Virus Killer named AVG who lived quietly in his little corner of the screen. In another place there lived Billy*, a spotty faced teenager with a personality disorder. Billy* wrote a virus to kill virus killers called killav. This was Billy’s* great gift to the world. AVG wrote all about Billy* in their database now we all have a virus killer killer killer in the corner of our screens.

Now in their enthusiasm to Kill Billy*, AVG have attacked my bat file used to configure my desktop for the “Ultimate Gaming Experience”. I run this bat file to kill all my “supporting apps” who don’t feature in my offline games, thus saving a few nanoseconds of valuable processor power. Included for temporary termination of course was AVG since the games are offline and no protection was needed. Obviously the virus scanner found the characters “kill” and “avg” in close proximity as in taskkill /f /im avg*xyz*.exe. No “killav” virus / trojan was truly present as there is no killav.exe process running – thank goodness.

Now to get the Virus Vault fixed in AVG Free 8 so I can recover my bat.

*This [deleted]‘s real name and gender (if any) is unknown.

Visual Styling problem creates filing mayhem

A funny thing happened today, but wait let me start at the beginning. Yesterday I was playing around with visual styles for Micro$oft Windoze XP and was toying with changing font sizes, trying to get bigger taskbar icons etc, i.e. generally trying to squeeze water out of a bloody stone. Lo and behold, without installing anything “foreign”, just using the plain old Display control panel to select new fonts, font sizes, icon sizes, DPI settings etc, “something” managed to break so that CSS styles wouldn’t render properly. This affected both my browsers FireFox 2 and internet exploiter 6. It was also noticeable in Windoze Help and Support Centre and various html based help files. Dreamweaver also couldn’t render it’s “syntax coloring” at all. Since I needed to spend my time productively working on a website built with CSS this was a bit of a problem as the code was hard to read and I couldn’t preview my work. What could have happened? I don’t know.

The problem sorted itself out with system restore, however that brilliant piece of Micro$oft technology managed to delete documents from two folders I created in root to store all my documents. The folders and sub-folders are still all there but they are vacant. Nobody is home. “Surely this is an optical illusion”, I thought, “maybe I just need to refresh my file browser”. Yeah right!

Micro$oft says that System Restore “enables administrators to restore their PCs, in the event of a problem, to a previous state without losing personal data files (such as Word documents, drawings, or e-mail) “. Perhaps they had fine print there about “oh it has to be in the original My Documents or else, blah blah blah…” , however since I am allowed to move the My Documents special folder to point to a folder of my choice that should have meant my files were safe, shouldn’t it?

I only moved the files there because we are all limited to file names of 256 characters and unfortunately that includes the full path which Micro$oft so cleverly reduced to 208 by wasting 48 characters (in my case) just to get to the root of the My Documents storage location, i.e. “C:/Documents and Settings/*******/My Documents/”. The fun starts when your sub-folders and the eventual file name take up the remaining 208 characters and THEN for example, Micro$oft Word creates a backup and slaps “Backup of ” to the front of the file name. Suddenly your file name becomes longer than 256 and the message “path could not be found” pops up when you try to open it. [mmm, shouldn't we just tell the users "we can't make a backup because the file name would be too long".... naaah, let them suffer]

I was only trying to defer the inevitable by moving everything into a folder named “C:/Documents/” (a mere 13 characters) and pointing to that as the My Documents folder, thereby saving 34 chars to allow for more descriptive folder / file names in my directory structure. Now, apparently I have sinned in doing so and the built-in retribution daemon has zapped my files.

DISCLAIMER: NO TRADEMARKS WERE SERIOUSLY HARMED DURING THE TYPING OF THIS POST

ESKOM Rewards Staff for Performance…

The whopping big bonuses seen in headlines a short while ago were supposedly partly for Awards that Eskom had received. Lets look at some of these shall we.

The KPMG Survey of Sustainability Reporting in South Africa
Eskom was awarded the KPMG Gold award for the best sustainability disclosure in an annual report in the Public Entities Category (2000 Annual Report), and the Gold award for the best Corporate Environmental Report in the South African Category (2000 Environmental Report).

Doesn’t “SUSTAINABILITY” mean that you plan to continue to meet growing demand?

Does “best report” mean they had the least spelling mistakes, grammatical errors and most pretty charts and tables? ;-)

Technology Top 100
Eskom’s Research Division was declared the overall category winner in the Technology Top 100 as the “Best Research and Development Group in South Africa” by Business Day and MTN.

… doesn’t say much for the rest of the technology in this country does it? ;-)

“SHED YOUR LOAD” @ www.blackout.co.za

“Well I’ll be damned! There is “wragtie waar” a place for unhappy citizens to share their “blackout” pains with the world. Who would have thought! If you have a story to tell go to http://www.blackout.co.za. Some interesting reading there.