VBA within Publisher

by Brian Kvalheim

Edited by Ed Bennett

Part 1


Why would someone want to use VBA in Publisher? The most common use is to automate repetitive tasks. Rather than repeating a series of several clicks to create a publication, wouldn’t it be nice to have a way to simply click one button and have all those steps done automatically? You could create a wizard to design a company newsletter set to your preferences for paper size, printer specifications, and color schemes. You could run a mail merge with a simple click of a button.

On the more complex side, VBA gives you a way to automate other Office applications like Word and Excel, and gives those applications a way to manipulate objects within Publisher.

A fundamental principle with VBA is that it runs only within the application that created it. If you write a code module in a publication, you need to be running that publication to execute the code. There is not a way to compile it outside of the application. If you want to do that, use Visual Basic, a more comprehensive programming language.

Before we can do any coding, we need to open the Visual Basic Editor. Go to Tools > Macros > Visual Basic Editor (or tap Alt+F11). Then, in the tree-view on the left, expand "Project (publication filename)", followed by "Microsoft Office Publisher Objects" (in Publisher 2002, this will be "Microsoft Publisher Objects"). Double-click on ThisDocument. This should open a window showing code attached to the current document.

Example 0:

This example does not do anything when run, but shows the bits you need to have present before you can write any code.

Sub ThisDoesNothing

End Sub

Example 0.5:

This example does exactly the same thing as Example 0 when run, but illustrates comments. Comments are text that the computer ignores, but makes the code easier to understand. Adding comments is considered good practise, as although you may understand what your program does when you write it, it becomes a lot harder when you come back 6 months later and try to read it!

Sub ThisAlsoDoesNothing
    ' Anything appearing after a single quote sign is a comment, and is ignored.
    ' Comments can appear at the end of lines containing code, or can occupy entire lines by themselves.
End Sub

Note that the code within the sub has been indented by four spaces. This can also be done using the Tab key in the VBE, and is recommended to make your code readable. More complex structures can use more indentation, but more on that in Part 2.

Example 1:

We want to change the bottom layout guides for the whole document. We identify the object that we want to manipulate. In this case, that would be the Layout Guides. We cannot simply type in Layout Guides. We need to establish the context where Layout Guides resides.

Sub Margins()
    ThisDocument.LayoutGuides.MarginBottom = 150
End Sub

When we run this macro, it moves the bottom margin to approximately two inches (2") from the bottom of the page.

Sidenote: About Points

Points are the default unit of measurement in Publisher VBA. These may be familiar to you from font sizes (which are also measured in points), but you may not be aware of what they mean. There are exactly 72 points in one inch. So size 72 font (usually the last and largest entry in a font size list) is one inch tall (in all caps). Publisher VBA provides plenty of built-in functions to make conversion to and from points easy.

Example 2:

Sub Burgundy()
    ThisDocument.ColorScheme = Application.ColorSchemes("Burgundy")
End Sub

In this example, we can programmatically change the color scheme of the publication on which we are working. In this case, we tell it to use “Burgundy”. Note the difference in ColorScheme and ColorSchemes. ColorSchemes is a collection of all Color Schemes that includes individual members, like Burgundy, or Citrus, or Floral, or Parrot.

Example 3:

Sub Heather()
    ThisDocument.ColorScheme = Application.ColorSchemes("Heather")
End Sub

This third example illustrates the same point with a different member of the collection, “Heather.”

Onwards to Part 2