Two Lines of Code that Can Drastically Improve Performance Reporting Progress
Dec 10

One key to becoming a more effective programmer is to develop an understanding of procedure arguments and how to use them. Once you develop an understanding of arguments, you will see programming tasks in a whole new light and dramatically increase your productivity as well as the quality of your code.

As an example, consider Listing 1. This is a macro I recorded in Microsoft Word to insert a picture and add an orange border to it. If you always add the same picture with the same color borders, perhaps this macro would be useful. However, it would be much more useful and efficient if you modified this macro so that it could be used in more situations. For example, maybe you want to specify which file to add and what color the borders should be.

Listing 1: A simple macro to insert a picture in Word

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2/21/2007 by Steve Hansen
'
' Note the reference to a hard-coded file name – you should update this
Selection.InlineShapes.AddPicture _
FileName:= "C:\DSC00007.JPG", _
LinkToFile:=False, _
SaveWithDocument:=True

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

With Selection.InlineShapes(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorOrange
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorOrange
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorOrange
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorOrange
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorOrange
End With
End Sub

You can think of arguments as variables that you define in the declaration of a procedure (the Sub or Function statement) with an added twist — other procedures that call the procedure can specify the value of these variables.

Listing 2 shows a modified version of Listing 1 that uses arguments to specify the filename of the image to add as well as the color of the borders. I also included a procedure named AddTestImage that demonstrates how to call another procedure that uses arguments. This procedure displays a dialog box that prompts the user to select an image to add.

Listing 2: A simple macro to insert a picture in Word with arguments

Sub AddTestImage()
Dim dlgOpen As FileDialog

' Initialize the file open dialog box
Set dlgOpen = Application.FileDialog( _
FileDialogType:=msoFileDialogOpen)
dlgOpen.Filters.Clear
dlgOpen.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
dlgOpen.AllowMultiSelect = False
dlgOpen.Show

' If a file was selected, try and insert it.
If dlgOpen.SelectedItems.Count > 0 Then
AddImage dlgOpen.SelectedItems(1), wdColorRed
End If
End Sub

Sub AddImage(sFileName As String, clrColor As WdColor)
Selection.InlineShapes.AddPicture _
FileName:=sFileName, _
LinkToFile:=False, SaveWithDocument:=True

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

With Selection.InlineShapes(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = clrColor
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = clrColor
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = clrColor
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = clrColor
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = clrColor
End With
End Sub

Using arguments is an essential concept to learn in order to start building more elaborate solutions instead of what you create simply by recording macros. As you can see here, it is relatively simple to add arguments to a recorded macro so that you can use it in more situations.

I’ll leave it as an exercise for you to figure out how to simplify the AddImage procedure even more. Here are a couple of clues: you can break it up into multiple procedures that use arguments and the AddImage has extra “baggage” that you can get rid of.

Leave a Reply