Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Publisher Automation from Access Frustration

BFroeschlKC

Programmer
Jun 20, 2004
10
US
I'm trying to automate creating a publisher page (a binder cover) from a Publisher "template" document, for each person in a list. The template document "BinderCover Test.pub" has one TextFrame object with the text "Name" in it.
BinderCover Test preview.jpg

I have some code to automate this template file.
Code:
Public Sub PublisherAutomationTest()
    Dim PubApp As Object
    Dim oDocument As Publisher.Document
    Dim oTestDoc As Publisher.Document
    Dim oPage As Publisher.page
    Dim oShp As Publisher.Shape
    Dim oName As cmName
    Dim LastPage As Integer
    
    ' Initialize Publisher
    Set PubApp = CreateObject("Publisher.Application")
    
    Set oDocument = PubApp.Open("C:\Users\bfroe\Documents (local)\Software Development\BinderCover.pub")
    oDocument.ActiveWindow.Visible = True
    DoEvents
    
    On Error Resume Next
        Set oTestDoc = PubApp.ActiveDocument
        If Err <> 0 Then
            Debug.Print "Looks like Publisher has a dialog box open!"
            GoTo GenerateBinders_Exit
        End If
    On Error GoTo 0
    
    ' verify the template
    For Each oPage In oDocument.Pages
        Debug.Print "Page: " & oPage.Name
        For Each oShp In oPage.Shapes
            If oShp.Type = pbTextFrame Then
                Debug.Print "Text Frame: " & oShp.TextFrame.TextRange.text
                If Len(oShp.TextFrame.TextRange.text) = 0 Then
                    Debug.Print "Bad document"
                    GoTo GenerateBinders_Exit
                End If
            End If
        Next oShp
    Next oPage
    
    Stop
    
GenerateBinders_Exit:
    On Error Resume Next
        PubApp.Quit
    On Error GoTo 0
    Set PubApp = Nothing
    Set oDocument = Nothing
    Set oTestDoc = Nothing
    Set oPage = Nothing
    Set oShp = Nothing
    Set oName = Nothing
End Sub

When I run the code, I see this in the Debug Window:
Code:
PublisherAutomationTest
Page: Page Title
Text Frame: Name

Text Frame: Retreat title

Text Frame: Binder Cover Date

The problem is, when I hit the "stop" line in the code, about a second later, a dialog box appears in the Publisher window:
Dialog in Publisher Window.png

I can't seem to figure out what is causing this and because this dialog box is open, I can't do anything with Publisher.
Also note that in the Publisher window, the TextFrame has no text in it. If I clear that message BS duplicate the page, the new page has a TextFrame with "####" in it. On the original page, the TextFrame does not have a text area (where you would normally type in some text).

Thus, frustration. Even more frustrating is this code (or a version of it) used to work and would successfully generate a document with many pages with different names filled in.

Does anyone have any idea why this is happening?
 
I copied your code and created a similar Publisher file. It seemed to work some times but not always. I had a hesitation in the code as Publisher was attempting to retrieve printer information. I have a wireless printer as my default. If I waited for the printer dialog to disappear, there was no issue with the code.
 
After playing with it for a long time I figured out it may have something to do with the graphics or complexity of the Publisher Document. I had background images and a couple of other images in my template documents and when I removed them I had better success.

If I reduced the number of people in my list, I also had better success. So, perhaps it is a function of memory or complexity in Publisher.

And never, ever stop the code while it's running or it will fail every time. Debugging the code now consists of Debug.Print lines.

I still don't completely understand it. And occasionally when I run my app and then open the generated document, Publisher wants to open in safe mode saying something to the effect that it could not open last time. I never open in safe mode and the document appears to open just fine.

This is not solved, but I can manage the problem by managing the symptoms.
 

Part and Inventory Search

Sponsor

Back
Top