VBA Publisher

Publisher is a Microsoft application used for desktop publishing. VBA is embedded in Publisher and can be used to work with Publisher programmatically. The Publisher Object Library contains classes that make working with Publisher in VBA possible. The classes in the Publisher Object Library are collectively referred to as the Publisher Object Model. When using VBA within Publisher, a reference is automatically included to the Publisher Object Library.

Publisher Object Model

The Publisher Object Model contains classes that make it possible to work with Publisher in VBA. The Publisher Object Model contains many classes but the general overview of the model is Application → Document → Page → Shape.

  1. Application
    1. Documents
      1. Document
        1. Pages
          1. Page
            1. Shapes
              1. Shape
                1. Table
                2. TextFrame

Context

The place where VBA code is written determines the implied context when referring to classes. For example, when using VBA in Publisher and referring to the Application object, it is implied that Application and Publisher.Application are the same object. However, when using VBA in another application to work with Publisher, it is necessary to qualify references to avoid naming collisions with classes in the other application. Using qualified references requires that a reference is set to the library where the classes are defined. If a reference cannot be set, late-binding can be used instead.

Public Sub Example()
    
    '''In Publisher

    Debug.Print Application Is Publisher.Application 'Prints: True

End Sub
Public Sub Example()
    
    '''In Excel

    'Reference to Publisher Object Library is required

    Debug.Print Application Is Publisher.Application 'Prints: False
    
    Debug.Print Application Is Excel.Application 'Prints: True

End Sub

To retrieve the Publisher Application object from another application, attempt to retrieve the running application and if that fails start a new instance of the application.

Public Function GetPublisherApp() As Object

    On Error Resume Next
    Set GetPublisherApp = GetObject(, "Publisher.Application")
    On Error GoTo 0

    If GetPublisherApp Is Nothing Then
        Set GetPublisherApp = CreateObject("Publisher.Application")
    End If

End Function

Application

The Application object represents the Publisher application itself. The Application object is the top-level object and provides access to the rest of the Publisher Object Model. The Application object can be used to access functionality and properties of the Publisher application.

Options

The Application.Options property can be used to set options for the Publisher application. In the example below, the MeasurementUnit property can be read or set as a member of the PbUnitType enum.

Public Sub Example()

    Debug.Print Publisher.Application.Options.MeasurementUnit

End Sub

Conversion Methods

The Application object has methods for converting measurement units to and from points:

Any measurement unit can be converted to another using one of the ToPoints methods with one of the PointsTo methods.

Public Sub Example()

    'Convert centimeters to inches

    Dim Centimeters As Single
    Centimeters = 1

    Dim Points As Single
    Points = Publisher.Application.CentimetersToPoints(Centimeters)

    Dim Inches As Single
    Inches = Publisher.Application.PointsToInches(Points)

    Debug.Print Inches

End Sub

Documents

The Document object represents a document in Publisher. The Documents collection object contains a collection of Document objects. Use the Application.Documents property to get a Documents collection containing all open Documents.

Iterate Documents

To loop over all open Publisher documents use a For Each loop with the Application.Documents property.

Public Sub Example()

    Dim D As Publisher.Document

    For Each D In Publisher.Application.Documents
        Debug.Print D.Name
    Next D

End Sub

Add Document

To add a new document use the Documents.Add method. Specify the type of publication to create using a member of the PbWizard enum.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents.Add(pbWizardBusinessCards)

End Sub

Save Document

To save a Publisher document use the SaveAs and Save methods. Use a member of the PbFileFormat enum to determine the file format.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents.Add(pbWizardBusinessCards)

    D.SaveAs Environ$("USERPROFILE") & "\Desktop\ExampleBusCards.pub", pbFilePublication

    Dim S As Publisher.Shape
    Set S = D.Pages(1).Shapes.AddTextbox(pbTextOrientationHorizontal, 0, 0, 100, 100)
    S.TextFrame.TextRange.Text = "Hello, World!"

    D.Save

End Sub

Convert Document

To convert a Publisher document to PDF or XPS format use the ExportAsFixedFormat method and pass a member of the PbFixedFormatType enum.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim FilePath As String
    FilePath = Environ$("USERPROFILE") & "\Desktop\ExamplePublication.pdf"

    D.ExportAsFixedFormat pbFixedFormatTypePDF, FilePath

End Sub

Open Document

To open a document use the Application.Open method.

Public Sub Example()

    Dim FilePath As String
    FilePath = Environ$("USERPROFILE") & "\Desktop\ExampleBusCards.pub"

    Dim D As Publisher.Document
    Set D = Publisher.Application.Open(FilePath)

End Sub

Close Document

To close a Publisher document use the Close method.

Public Sub Example()

    Dim FilePath As String
    FilePath = Environ$("USERPROFILE") & "\Desktop\ExampleBusCards.pub"

    Dim D As Publisher.Document
    Set D = Publisher.Application.Open(FilePath)

    D.Close

End Sub

Pages

The Page object represents a page within a Publication. The Pages collection object contains a collection of Page objects. Use the Document.Pages property to get a Pages collection containing all Page objects in a Document.

Add Page

To add a page use the Pages.Add method.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages.Add(1, 0)

End Sub

Add Wizard Page

To add a page of a particular type use the Pages.AddWizardPage method. Select the type of page using a member of the PbWizardPageType enum. Wizard pages can only be added to publications of a similar type otherwise an error occurs.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents.Add(pbWizardCatalogs)

    D.Pages.AddWizardPage 1, pbWizardPageTypeCatalogCalendar

End Sub

Delete Page

To delete a page use the Delete method.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    P.Delete

End Sub

Duplicate Page

To duplicate a page use the Duplicate method.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    Dim P1 As Publisher.Page
    Set P1 = P.Duplicate

End Sub

Move Page

To move a page use the Move method.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    P.Move 2

End Sub

Shapes

The Shape object represents a shape in Publisher. The Shapes collection object contains a collection of Shape objects. Use the Page.Shapes property to get a Shapes collection containing all Shapes on a Page. Shapes can contain various objects and serve different purposes.

Tables

To create a table use the Shapes.AddTable method. Retrieve the shape's Table object using the Table property of the Shape object.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    Dim S As Publisher.Shape
    Set S = P.Shapes.AddTable(10, 3, 0, 0, 200, 300)

    Dim T As Publisher.Table
    Set T = S.Table

End Sub

Text Boxes

To add a text box use the Shapes.AddTextbox method.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    Dim S As Publisher.Shape
    Set S = P.Shapes.AddTextbox(pbTextOrientationHorizontal, 0, 0, 150, 100)
    S.TextFrame.TextRange.Text = "Hello, World!"

End Sub

WizardTag

A Shape object's WizardTag property can be used to set or return a PbWizardTag enum member representing the function of the Shape. The FindShapeByWizardTag method can be used to get a ShapeRange object containing shapes with a given WizardTag.

Public Sub Example()

    Dim D As Publisher.Document
    Set D = Publisher.Application.Documents(1)

    Dim P As Publisher.Page
    Set P = D.Pages(1)

    Dim S As Publisher.Shape
    Set S = P.Shapes.AddTextbox(pbTextOrientationHorizontal, 0, 0, 150, 100)
    S.WizardTag = pbWizardTagMainTitle
    S.TextFrame.TextRange.Text = "Hello, World!"

    Dim SR As Publisher.ShapeRange
    Set SR = P.Shapes.FindShapeByWizardTag(pbWizardTagMainTitle)

    Debug.Print SR(1).TextFrame.TextRange.Text

End Sub