Guest User

Untitled

a guest
Jan 11th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Declare Public variables so Userform can see them
  2. Public pbApp As Object
  3. Public FullPath As String
  4. Public pbDoc As Object
  5.  
  6. Public Sub PublisherPrint()
  7.  
  8.  
  9.  
  10. ' Saves the workbook, updates links in Publisher, saves as pdf, and (optionally) prints
  11.  
  12. ' Late-Binding
  13.  
  14.  
  15. Dim ReportDate As Range
  16. Dim FileDate As String
  17. Dim i As Integer
  18. Dim PubChkCounter As Long
  19.  
  20. Dim shpShape As Object
  21. Dim PgCnt As Object
  22.  
  23.  
  24. Application.ScreenUpdating = False
  25.  
  26. On Error GoTo ErrHandler
  27.  
  28. ' Save the workbook so Publisher can see current changes
  29. Application.StatusBar = "Saving..."
  30.            
  31. ActiveWorkbook.Save
  32.  
  33.  
  34. ' Make sure Publisher is not running
  35. ' [Because Publisher has a single document interface, the Open method only works when you open a new instance of Publisher]
  36.    
  37.     ' Initialize counter for preventing infitite loops
  38.    PubChkCounter = 0
  39.  
  40.        
  41. setpoint:  ' setpoint for loop
  42.    
  43.     On Error Resume Next
  44.     ' Check for open instance
  45.    Set pbApp = GetObject(, "Publisher.Application")
  46.         PubChkCounter = PubChkCounter + 1 ' Iterate counter
  47.        
  48.         If Err.Number = 0 Then ' Publisher is open
  49.            If PubChkCounter < 6 Then ' Limits loop to 5 iterations
  50.                    If MsgBox("Publisher is running; please close before proceeding." & vbCrLf & "(May have to force close from task manager.)", vbOKCancel, "Warning") = vbOK Then ' User clicked OK
  51.                            GoTo setpoint
  52.                         Else ' User pressed Cancel
  53.                            MsgBox "Procedure cancelled. No export saved."
  54.                             Exit Sub
  55.                         End If
  56.                 Else ' Too many iterations
  57.                    MsgBox "Procedure cancelled. No export saved." & vbCrLf & "Please close Publisher and try again."
  58.                     Exit Sub
  59.             End If
  60.             Else ' Publisher is closed
  61.        
  62.        Set pbApp = CreateObject("Publisher.Application") ' Bind to a new instance of Microsoft Publisher
  63.       End If
  64.  
  65.  
  66. On Error GoTo ErrHandler
  67.  
  68.  
  69. Application.StatusBar = _
  70.             "Updating Publisher tables..."
  71.  
  72.  
  73. ' Open file
  74. Set pbDoc = pbApp.Application.Open("X:\[Path]\[Filemame].pub")
  75.  
  76.  
  77. pbApp.Open FileName:="X:\[Path]\[Filemame].pub"
  78.    pbApp.ActiveWindow.Visible = False
  79.  
  80.  
  81. ' Update linked tables
  82.  
  83. ' Insert pause to allow Publisher to open fully
  84.  If iFirstTimeThrough <> "NO" Then Application.Wait (Now + TimeValue("0:00:02"))
  85.     iFirstTimeThrough = "NO"
  86.        
  87. ' Loop through all pages one at a time
  88.   For Each PgCnt In pbApp.Application.ActiveDocument.Pages
  89.  
  90.           ' Loop through all shapes in each page
  91.        For Each shpShape In PgCnt.Shapes
  92.                 If shpShape.Type = 10 Then
  93.                    shpShape.LinkFormat.Update
  94.                 End If
  95.         Next shpShape
  96.        
  97.     Next PgCnt
  98.        
  99.  ' Do the same for shapes in the Master Page, as it's a separate entity from ActiveDocument.Pages
  100.  
  101.     For Each shpShape In pbApp.Application.ActiveDocument.Pages(1).Master.Shapes
  102.         If shpShape.Type = 10 Then
  103.             shpShape.LinkFormat.Update
  104.         End If
  105.     Next shpShape
  106.  
  107. ' Pause to allow tables to update
  108. If iFirstTimeThrough <> "NOAGAIN" Then Application.Wait (Now + TimeValue("0:00:05"))
  109.         iFirstTimeThrough = "NOAGAIN"
  110.  
  111. Application.StatusBar = _
  112.             "Exporting pdf..."
  113.  
  114. ' Save updated Publisher file
  115. pbApp.ActiveDocument.Save
  116.  
  117. ' Export as .pdf
  118.    Const FileExtension As String = ".pdf"
  119.    
  120.     Dim FSO As Object
  121.     Dim SupplierName As String
  122.     Dim FolderPath As String
  123.     Dim FileName As String
  124.  
  125.     Dim Version As Long
  126.  
  127.    
  128.     Application.DisplayAlerts = False
  129.    
  130.     Set FSO = CreateObject("Scripting.FileSystemObject")
  131.    
  132.     ' Set date variable based on named range from top of spreadsheet
  133.    FileDate = Range("ReportDate")
  134.    
  135.     ' Set path and filename
  136.    FilePath = "X:\[Path]\"
  137.     FileName = "[Filename]_" & Format(FileDate, "MM.DD.YYYY")
  138.     FullPath = FilePath & FileName & FileExtension
  139.    
  140.     ' Check if file already exists, append sequential numbers to filename if it does
  141.    Do While FSO.fileexists(FullPath)
  142.         Version = Version + 1
  143.         FullPath = FilePath & FileName & " (" & Version & ")" & FileExtension
  144.     Loop
  145.    
  146.     ' Save .pdf
  147.    pbApp.ActiveDocument.ExportAsFixedFormat 2, FullPath
  148.    
  149.    Application.StatusBar = _
  150.             "Printing & opening pdf..."
  151.            
  152.    ' Inform of succesful save, ask if user wants to display and/or print
  153.      Set frmUserform = SavePrintDialogue
  154.    
  155.    With frmUserform
  156.     .Label1.Caption = "Your file has been saved as " & FullPath & vbCrLf & vbCrLf & "Print a hardcopy?"
  157.    
  158.    
  159. End With
  160. frmUserform.Show
  161.  
  162.      
  163. Application.DisplayAlerts = True
  164.    
  165.        
  166. ' Close and clear Publisher
  167. pbApp.Quit
  168. Set pbDoc = Nothing
  169. Set pbApp = Nothing
  170. Application.StatusBar = False
  171. Application.ScreenUpdating = True
  172.  
  173. Exit Sub
  174.  
  175. ErrHandler:
  176. MsgBox "There seems to be an error" & vbCrLf & Err.Description
  177.  
  178. End Sub
  179.  
Advertisement
Add Comment
Please, Sign In to add comment