View Single Post
 
Old 03-30-2018, 03:02 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

See the attached document. It contains three checkboxes with the associated pdf filename listed against the tag property for each one. The macro can be run by double clicking the first paragraph above the check boxes.

The PDF files will need to be in the same folder as the Word document.
Code:
Option Explicit

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
 
Public Function PrintPDF(xlHwnd As Long, FileName As String) As Boolean
  Dim X As Long
  
  On Error Resume Next
  X = ShellExecute(xlHwnd, "Print", FileName, 0&, 0&, 3)
  
  If Err.Number > 0 Then
    MsgBox Err.Number & ": " & Err.Description
    PrintPDF = False
  Else
      PrintPDF = True
  End If
  On Error GoTo 0
End Function

Sub PrintDocs()
  Dim aCC As ContentControl, sFile As String, sPath As String
  sPath = ActiveDocument.Path & Application.PathSeparator
  For Each aCC In ActiveDocument.ContentControls
    If aCC.Type = wdContentControlCheckBox Then
      If aCC.Checked Then
        sFile = sPath & aCC.Tag
        If Not PrintPDF(0, sFile) Then MsgBox "Printing failed: " & sFile
      End If
    End If
  Next aCC
End Sub
Attached Files
File Type: docm PrintDocsDemo.docm (60.0 KB, 22 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote