none
Automate ms Publisher in vb 2005 RRS feed

  • Question

  • Hi,

    Im a hobby programmer, long since retired.  When I worked, all computers were main frames. In fact, mechanical ledger machines were the order of the day when I started.  When I finished, pc's were still being invented.    So, if you will, be patient.

    I have a folder with manny publisher documents I have created.  These are greeting cards, all have two publisher pages, some have more. My wish is to save theses publisher pages as Jpegs, that is one Jpeg for each page of each file.

    I have been able to access the publisher files and get the pages count.

    But every time I execute a Page.SaveAsPicture the jpeg file that is saved is always page 1 of the publisher file.

    Extract of my code is as follows -

    Dim app As New Publiser.Application, doc As Publisher.Document, pge As Publisher.Page      

    Dim pubfl As String = "some publisher file.pub", savfl As String = "some jpeg file.jpg"

     doc = app.Open(pubfl)  

    pge = doc.Pages(2)

    pge.SaveAsPicture(savfl)

     There are 2 pages in the publisher file doc.

    Can I save the second one as a Jpeg ?

    If I have selected the the wrong forum for this post, please let me know.

    Many Thanks.

    • Moved by Youen Zen Wednesday, November 14, 2012 5:32 AM Appropriate forum for this issue (From:Visual Basic General)
    Tuesday, November 13, 2012 3:16 AM

Answers

  • Hi FredBrownZ,

    Thank you for your last reply.

    I've tried your code on a machine which has Visual Studio 2005 and Office 2003 installed, and your code works fine.

    It based on my experience that Office PIAs are only available for products after Office 2003. From your last reply, I've realized that you are using Publisher 2002 which is not supported in this forum anymore. It is recommended to upgrade your Publisher to a newer version.

    Thank you for your understanding.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by FredBrownZ Thursday, November 15, 2012 8:08 AM
    Thursday, November 15, 2012 6:06 AM
    Moderator

All replies

  • Hello,

    I'm not aware there is a build in publisher template. 

    However there is an forum for Office tools for Visual Studio

    I guess you have more chance on an answer in that one

    http://social.msdn.microsoft.com/Forums/en-US/vsto


    Success
    Cor

    Tuesday, November 13, 2012 8:11 AM
  • Does the code work, as a VBA macro, directly from Publisher? If not I would post your question to the General Office Development forum.

    I don't seen anything in the code that would indicate a problem, but perhaps SaveAsPicture works differently than I would suspect.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, November 13, 2012 3:36 PM
  • Thanks for you response.

    Two points.

    Firstly - Yes I will post as you suggest. Do I leave this post as is and post a new post, or delete this and post a new one, or is there a transfer option ?

    And second - The code is a Visual Studio 2005 Visual Basic Windows Application. 

    Sub TryIt()
    
            Dim fl, jpgfl As String, i As Integer
    
            For Each fl In IO.Directory.GetFiles(PubFldr)
                If LCase(Me.fExtention(fl)) = ".pub" Then
    
                    Doc = App.Open(fl)
                    For i = 1 To Doc.Pages.Count
                        Pge = Doc.Pages(i)
                        jpgfl = SavFldr & "\" & Me.fFileName(fl) & " page " & i & ".jpg"
                        Pge.SaveAsPicture(jpgfl)
                    Next
                    Doc.Close()
    
                End If            
            Next
    
        End Sub

    For a document that has 3 pages, the statement Pge.SaveAsPicture saves 3 jpegs but each is an image of page 1. Certainly it appears to be the SaveAsPicture that is not working as it should.

    It may have something to do with which page is the active page but I cnat find any way to set that to be a different page.

    The publisher program itself saves each page ".. save as jpeg .." but the page it saves is always the active page.   If you want to save page 2 you must have page 2 showing, then do the save.

    I appreciate being able to ask this question.  Many thanks.


    • Edited by FredBrownZ Tuesday, November 13, 2012 11:44 PM
    Tuesday, November 13, 2012 11:40 PM
  • Hi FredBrownZ,

    I'll move this thread to General Office development forum, which will be appropriated forum for office publisher issue.

    Thanks for understanding.


    Shanks Zen
    MSDN Community Support | Feedback to us

    Wednesday, November 14, 2012 5:31 AM
  • Hi FredBrownZ,

    Thank you for posting in the MSDN Forum.

    I've the code below which has the same logic as yours.

    Imports Publisher = Microsoft.Office.Interop.Publisher
    Module Module1
    
        Sub Main()
            Dim app As New Publisher.Application
            Dim doc As Publisher.Document
            'Dim pge As Publisher.Page
    
            'Suppose you have a pub file named Publication1.pub in D:\
            Dim pubFile As String = "D:\Publication1.pub"
            Dim pgeCount As Integer
            Dim index As Integer
    
            app.ActiveWindow.Visible = True  'Make the publisher visbile,
            doc = app.Open(pubFile)          'Open the pub file.
            pgeCount = doc.Pages.Count       'Count how many pages the pub file contains.
    
            'Loop through the file from 1st page to the end and save each page as picture.
            For index = 1 To pgeCount        
                doc.Pages(index).SaveAsPicture("D:\Publication1_" + index.ToString() + ".jpeg")
            Next
    
        End Sub
    
    End Module

    It works fine.

    So, what if you change your code

     For i = 1 To Doc.Pages.Count
        Pge = Doc.Pages(i)
        jpgfl = SavFldr & "\" & Me.fFileName(fl) & " page " & i & ".jpg"
        Pge.SaveAsPicture(jpgfl)
    Next

    into

     For i = 1 To Doc.Pages.Count
        jpgfl = SavFldr & "\" & Me.fFileName(fl) & " page " & i.ToString() & ".jpg"
        Doc.Pages(i).SaveAsPicture(jpgfl)
    Next

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, November 14, 2012 11:38 AM
    Moderator
  • Hi Quist Zhang.

     Thanks for your time and attention.  I have found a solution to my need but not to why my code does not work properly.

     In my vb code, I could get a page declared, all the details were present such as index, pageID, etc, but when SaveAsPicture was executed the image saved was of page 1, always.

     I noticed page functions for add and delete, tried them and they worked for me.  So I reasoned Page 2 would become page 1 if page 1 was deleted.  I ran the following code, all pages of all files were saved as I wanted.

           Doc = App.Open(fl)
           pgecount = Doc.Pages.Count
           For i = 1 To pgecount
               Pge = Doc.Pages(1)
               jpgfl = SavFldr & "\" & Me.fFileName(fl) & " page " & i & ".jpg"
               Pge.SaveAsPicture(jpgfl)
               If pgecount > 1 Then If i < pgecount Then Doc.Pages(1).Delete()
            Next        
            Doc.Close()
    

    However, it's still a challenge and to me, like a bone to a hungry dog.  The code statements wouldn't be there if they weren't intended to be used.  I have tried the code you suggested - same result.   Following is the complete code  so that you can see everything about what I have done.

    Imports Publisher
    
    Public Class Form1
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Me.Label1.Text = "Starting . . . "
            Select Case e.KeyCode
                Case Keys.Escape : End
                Case Keys.Enter : TryIt()
            End Select
            Me.Label1.Text = "Finished !"
        End Sub
    
        Sub TryIt()
    
            Dim app As New Publisher.Application, doc As Publisher.Document
            Dim pubFldr As String = "D:\other\cards", saveFldr As String = "D:\Test\PublisherJpegs\", saveFile As String
            Dim pgeCount, index, testCount As Integer
    
            For Each fl As String In IO.Directory.GetFiles(pubFldr)
                If LCase(Me.fExtention(fl)) = ".pub" Then
                    doc = app.Open(fl)
                    pgeCount = doc.Pages.Count
                    For index = 1 To pgeCount
                        saveFile = saveFldr & Me.fFileName(fl) & " " & index.ToString & ".jpg"
                        doc.Pages(index).SaveAsPicture(saveFile)
                    Next
                    doc.Close()
                    testCount += 1 : If testCount >= 3 Then Exit Sub
                End If
            Next
    
        End Sub
    
        Function fFileName(ByVal fl As String) As String
            Dim i As Integer = fl.LastIndexOf("\"), j As Integer = fl.LastIndexOf(".")
            Return fl.Substring(i + 1, j - i - 1)
        End Function
        Function fExtention(ByVal fl As String) As String
            Dim j As Integer = fl.LastIndexOf(".")
            If j < 1 Then Return ""
            Return fl.Substring(j)
        End Function
    
    End Class
    

    There's got to be a reason and I bet London to a brick it's something on my computer, or me, or both.  Once again, I'm really grateful for your time and attention.

    I am using MSV 2005 Version 8.0.50727.867

    MS .NET Version 2.0.50727 SP2

    Publisher 2002(10.6867.6870 SP3

    Thursday, November 15, 2012 12:12 AM
  • Hi FredBrownZ,

    Thank you for your last reply.

    I've tried your code on a machine which has Visual Studio 2005 and Office 2003 installed, and your code works fine.

    It based on my experience that Office PIAs are only available for products after Office 2003. From your last reply, I've realized that you are using Publisher 2002 which is not supported in this forum anymore. It is recommended to upgrade your Publisher to a newer version.

    Thank you for your understanding.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by FredBrownZ Thursday, November 15, 2012 8:08 AM
    Thursday, November 15, 2012 6:06 AM
    Moderator
  • This post is to thanks for the opportunity to discuss.  It is nice to know this is available.   

    Thank you Quist Zhang and thank you MSDN.   

    Nice to have interaction with experts in a field which is to me but a retirement hobby.

    I understand why the forum would not support out of date or superseded software and agree that this is the way it must be.

    However, as I mentioned in one of my previous replies on this thread, the code statement is there, and if it was put there then it must have been intended that it would work.

    Until I find the answer -  Have a great day !

     

     

    Friday, November 16, 2012 11:35 PM