Introduction | Example | Tutorial | Applications
Introduction - Dot Grid
Add a custom dot grid to a page in your Word document with this VBA macro. Print it out to make a notebook or use it to design, doodle or play Dots and Boxes.
Disclaimer: Depending on how small a grid you make, this macro may take several minutes to execute, at which time your Word Document may appear to stop responding.
Example
Add Dot Grids to Word Document
Option Explicit
Sub AddDotGrid()
'-----------------------------------------------------------------------------------------
'---Script: AddDotGrid--------------------------------------------------------------------
'---Created by: Ryan Wells (wellsr.com)---------------------------------------------------
'---Date: 01/2016-------------------------------------------------------------------------
'---Description: Add a custom dot grid to a page in your Word document.-------------------
'---Note: This may take a while to execute.-----------------------------------------------
'---Input: Modify the value of dInc to add a dot grid every dInc inches (0.25, 0.5, etc.)-
'-----------------------------------------------------------------------------------------
Dim shp As Shape
Dim dWidth As Double
Dim dHeight As Double
Dim dInc As Double 'Place dots ever dInc inches (assumes 72 DPI)
Dim iIncW As Integer, iIncH As Integer
Dim i As Integer, j As Integer
Dim dDoc As Document
'----INPUT----
dInc = 0.25 'place dots every dInc inches
'-------------
Application.ScreenUpdating = False
Set dDoc = ActiveDocument
dWidth = dDoc.PageSetup.PageWidth
dHeight = dDoc.PageSetup.PageHeight
iIncW = CInt(dWidth / 72 / dInc)
iIncH = CInt(iIncW * dHeight / dWidth)
For i = 1 To iIncW
For j = 1 To iIncH
Set shp = dDoc.Shapes.AddShape(Type:=msoShapeOval, Left:=dWidth / iIncW * i, Top:=dHeight / iIncH * j, _
Width:=2, Height:=2)
shp.Fill.ForeColor = RGB(215, 215, 215)
shp.Line.Visible = msoFalse
Next j
Next i
Set shp = Nothing
Set dDoc = Nothing
Application.ScreenUpdating = True
End Sub
Make powerful macros with our free VBA Developer Kit It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit and wrote the Big Book of Excel VBA Macros full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.
Tutorial
Again, I want to warn you that this can take a long time to execute, but it works! Once complete, the entire page of your Word document will be covered in tiny dots.
Here’s how you run the dot grid macro:
- Open the VBA Editor by pressing Alt+F11.
- Insert a module by pressing Insert > Module on the top menu.
- Paste the
AddDotGrid example in the new Module. - Run the macro by pressing F5.
By default, this macro places grey dots every quarter of an inch (0.25 inches) on your page. Change the value of the variable
If you don’t like grey, you can play around with the color of your dots by changing the integers in RGB(215, 215, 215)
. Don’t like circular dots? Change the Type from
Delete Dot Grid
Sub DeleteDotGrid()
'Delete all shapes on page that look like a dot (i.e., all 2x2 circles)
Dim shp As Shape
Application.ScreenUpdating = False
For Each shp In ActiveDocument.Shapes
If shp.AutoShapeType = msoShapeOval And shp.Width = 2 And shp.Height = 2 Then
shp.Delete
End If
Next shp
Application.ScreenUpdating = True
End Sub
Run the above macro to delete your dot grid. If you created hundreds of dots, you may have to run this macro several times to delete all of them. This macro searches for and deletes all 2x2 ovals in your Word document.
Application Ideas
This is one of my favorite macros. It’s slow as molasses, but I still like it. I run this macro to create full size dot grid paper. I print double-sided copies of the dot grid paper and cut and staple them into small notebooks for myself. They look like the Field Notes brand of notebooks and fit perfectly in my pocket.
In addition to making a notebook, here are some other ideas for you:
- Play Dots and Boxes
- Sketch a floor plan
- Create a ruler
I don’t post Microsoft Word VBA tutorials very often, so I hope you enjoy this rare treat. Share this macro on Facebook or Twitter and subscribe using the form below when you’re ready to really take your VBA skills to the next level.