Logo: TechTrax...brought to you by MouseTrax Computing Solutions

Quick, Get Me Some Graph Paper!

by Bill Coan, MVP
Skill rating level 5.

If Microsoft Word had been around in the 17th century, Rene Descartes might have been an early adopter. After all, he’s the one who came up with the idea of graph paper, and Word would have been a great help to him, especially if he was fussy about the size of the squares.

Figure 1: Samples of graph paper generated by Microsoft Word, with squares ranging in size from one inch down to one-eighth of an inch.

Today, if you’re serious about graphing something, chances are you’ll use a graphing calculator or a personal computer. As a result, on the rare occasions when you need a sheet of graph paper, you won’t have one right at hand. That’s when a little Word macro can save you a trip to the office supply store.

The following macro is a handy tool for anyone who needs an occasional piece of graph paper. It’s also a handy introduction to some intermediate programming concepts, such as how to prompt the user for data, how to perform simple calculations, and how to manipulate “draw” objects such as lines in a Word document.

After prompting you for the size of the squares, the macro calculates how many squares will fit in the available space, given the page size and margins. Then the macro draws a series of horizontal and vertical lines to create the squares.

If you’ve never worked with a macro before and you’d like to test the graph paper macro provided below, the following article will provide valuable tips on how to proceed:

http://word.mvps.org/faqs/macrosvba/CreateAMacro.htm

Whether you’ve worked with macros previously or not, reading through the following code and especially the comments (i.e., lines starting with an apostrophe [shown in red]) should help you understand how the macro works.

Sub CreateGraphPaper()

'Declare variables
Dim sglBeginX As Single
Dim sglEndX As Single
Dim sglBeginY As Single
Dim sglEndY As Single
Dim Interval As Single
Dim LineCount As Integer
Dim VerticalGridLines As Integer
Dim HorizontalGridLines As Integer
Dim GridWidth As Single
Dim GridHeight As Single
Dim UserChoice As String
Dim oLine As Shape

'set the line weight, in points
Const LINE_WEIGHT As Integer = 2

'set whether to delete the old grid,
'(assuming there is one)

Const DELETE_OLD_GRID As Boolean = True

'Delete the old grid
If DELETE_OLD_GRID Then
   On Error Resume Next
   ActiveDocument.Range.ShapeRange.Delete
   On Error GoTo 0
End If

'Prompt the user to specify the size of squares desired
PromptUser:
UserChoice = InputBox _
("Enter size of squares in points:" & vbLf & vbLf & _
   "(Value must be between 1 and 72)" & vbLf & _
   "9 points = 1/8 inch" & vbLf & _
   "12 points = 1/6 inch" & vbLf & _
   "18 points = 1/4 inch" & vbLf & _
   "24 points = 1/3 inch" & vbLf & _
   "36 points = 1/2 inch" & vbLf & _
   "72 points = one inch", "Create graph paper")

'If the size requested is outside the allowed range,
'then go back and prompt the user again,
'but if user has clicked cancel, then quit

If IsNumeric(UserChoice) Then
   Interval = Val(UserChoice)
   If Interval > 72 Or Interval < 2 Then
      MsgBox "Value must be between 1 and 72.", _
        vbOKOnly + vbInformation, "Value out of range"
      GoTo PromptUser
   End If
ElseIf UserChoice = "" Then
   GoTo EndGracefully
Else
   GoTo PromptUser
End If

'Calculate beginning and ending positions
'based on page size and margins

With ActiveDocument.Sections.First.PageSetup
   sglBeginX = .LeftMargin
   sglEndX = .PageWidth - .RightMargin
   sglBeginY = .TopMargin
   sglEndY = .PageHeight - .BottomMargin
End With

'Calculate how many squares of the requested size
'can fit in the available area

HorizontalGridLines = Int((sglEndY - sglBeginY) / Interval)
VerticalGridLines = Int((sglEndX - sglBeginX) / Interval)

'Calculate the size of the finished grid, based on
'size of the number of lines and the size of the squares

GridWidth = VerticalGridLines * Interval
GridHeight = HorizontalGridLines * Interval

'draw the horizontal lines
For LineCount = 0 To HorizontalGridLines
   Set oLine = ActiveDocument.Shapes.AddLine( _
      BeginX:=sglBeginX, _
      BeginY:=sglBeginY + LineCount * Interval, _
      EndX:=sglBeginX + GridWidth, _
      EndY:=sglBeginY + LineCount * Interval)
   oLine.Line.Weight = LINE_WEIGHT
Next LineCount

'draw the vertical lines
For LineCount = 0 To VerticalGridLines
   Set oLine = ActiveDocument.Shapes.AddLine( _
      BeginX:=sglBeginX + LineCount * Interval, _
      BeginY:=sglBeginY, _
      EndX:=sglBeginX + LineCount * Interval, _
      EndY:=sglBeginY + GridHeight)
   oLine.Line.Weight = LINE_WEIGHT
Next LineCount

'release the oLine object from memory
Set oLine = Nothing

'end gracefully
EndGracefully:

End Sub

Click to rate this article.

 

Go up to the top of this page.
This site powered by the Logical Web Publisher™: Content management by Logical Expressions, Inc.