Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Solved: How to auto print .PDF files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Solved: How to auto print .PDF files

    I'm extremely new to VBA so go easy on me. I constantly receive emails with .pdf files that I have to print. I'm trying to get vba to do it for me without me having to go into each individual email.

    This is what I have so far. It saves the attachments to the specified folder and opens adobe, but it doesn't open the attachment itself and print it.

    [vba]Sub GetAttachments()


    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0

    If Inbox.Items.Count = 0 Then
    MsgBox "There are no messages in the Inbox.", vbInformation, _
    "Nothing Found"
    Exit Sub
    End If

    For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
    FileName = "C:\Email Attachments\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    Shell """c:\program files\adobe\reader 8.0\reader\acrord32.exe"""
    i = 1 + 1



    Next Atmt


    Next Item

    If i > 0 Then
    MsgBox "I found " & i & " attached files." _
    & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
    & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
    Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
    "Finished!"
    End If

    GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub


    Exit Sub



    End Sub[/vba]
    Any ideas?

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Try this command for using acrobat reader :
    AcroRd32.exe /t path printername drivername portname
    - Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader.
    The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).
    printername - The name of your printer.
    drivername - Your printer driver's name. Whatever appears in the Driver Used box when you view your printer's properties.
    portname - The printer's port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.
    If using Acrobat, substitute Acrobat.exe in place of AcroRd32.exe in the command lines.

    Charlize

  3. #3
    Quote Originally Posted by Charlize
    Try this command for using acrobat reader :
    AcroRd32.exe /t path printername drivername portname
    - Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader.
    The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).
    printername - The name of your printer.
    drivername - Your printer driver's name. Whatever appears in the Driver Used box when you view your printer's properties.
    portname - The printer's port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.
    If using Acrobat, substitute Acrobat.exe in place of AcroRd32.exe in the command lines.

    Charlize
    How would I set this as the default printer for my PC. I'm on an extremely secure network and some of this info is eluding me.

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Just use [VBA]shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe /p /h " & path[/VBA]like JP2112 said.

    [VBA]
    For Each Atmt In Item.Attachments
    FileName = "C:\Email Attachments\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    'If using version 7 = Acrobat 7.0
    shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe /p /h " & FileName
    i = 1 + 1
    Next Atmt
    [/VBA]
    You also have to change the way how the variable i gets the first value. You give it 0 but what if there are files with the same name the next time you save a file ? You always start with 0. Maybe use Dir statement to loop through directory and count no of files to determine the next value for i ?

    Charlize

  5. #5
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    After saving the PDF, run this code:

    [VBA]Public Sub print_pdf(xSomeFile as string)
    Shell "C:\Program Files\Adobe\Acrobat 5.0\Reader\AcroRd32.exe /p /h " & xSomeFile, vbHide
    End Sub[/VBA]

    (From http://www.daniweb.com/forums/thread17156-2.html)

    Update the filepath to reflect your local copy of AA.

    HTH,
    JP

    Quote Originally Posted by pitspawn8
    I'm extremely new to VBA so go easy on me. I constantly receive emails with .pdf files that I have to print. I'm trying to get vba to do it for me without me having to go into each individual email.

    This is what I have so far. It saves the attachments to the specified folder and opens adobe, but it doesn't open the attachment itself and print it.

  6. #6
    Okay, I added that stuff and now adobe gives me an error saying that the file can't be found. This is what I have now.

    [VBA]For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
    FileName = "C:\Email Attachments\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & Atmt.FileName
    i = 1 + 1
    Next Atmt
    [/VBA]

    I've also tried it like this but with the same result.

    [VBA]For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
    FileName = "C:\Email Attachments\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & FileName

    i = 1 +1
    Next Atmt
    [/VBA]

    Charlize, The attachments are deleted at the end of the day so I'm not really too worried about the message box.

  7. #7
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Change[vba] Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & FileName [/vba]in
    [vba] Shell "c:\program files\adobe\acrobat 8.0\reader\acrord32.exe /p /h " & FileName [/vba]Charlize

  8. #8
    That file path doesn't have anything but *.bak files. I tried copying the files from the other path but it still gives me the same error.

  9. #9
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    You have to locate the folder containing the Acrobat reader executable.

    Otherwise, check the following for a list of command line switches for use with AA. You might need to reorder the switches/filepath.

    http://partners.adobe.com/public/dev...veloperFAQ.pdf

    --JP

    Quote Originally Posted by pitspawn8
    That file path doesn't have anything but *.bak files. I tried copying the files from the other path but it still gives me the same error.

  10. #10
    I'm pretty sure that the print commands are correct now. It just seems like adobe isn't opening the atmt to print it. Adobe just says the file can't be found.

  11. #11
    Also, I appreciate both of your help. Like I said, I'm extremely new to this and am still learning a great bit. I appreciate your patience.

  12. #12
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Can you post your updated code?

    --JP

    Quote Originally Posted by pitspawn8
    I'm pretty sure that the print commands are correct now. It just seems like adobe isn't opening the atmt to print it. Adobe just says the file can't be found.

  13. #13
    [VBA]Sub GetAttachments()


    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0

    If Inbox.Items.Count = 0 Then
    MsgBox "There are no messages in the Inbox.", vbInformation, _
    "Nothing Found"
    Exit Sub
    End If

    For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
    FileName = "C:\Email Attachments\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    Shell "c:\program files\adobe\acrobat 8.0\reader\acrord32.exe /p /h " & Atmt.FileName

    i = 1 + 1




    Next Atmt


    Next Item

    If i > 0 Then
    MsgBox "I found " & i & " attached files." _
    & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
    & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
    Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
    "Finished!"
    End If

    GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub


    Exit Sub



    End Sub[/VBA]

  14. #14
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    You are processing the drafts folder ? Try this because I don't know which folder you want to handle.[VBA]Set smbbox = Application.GetNamespace("MAPI").PickFolder[/VBA]
    Charlize

  15. #15
    That worked wonderfully. Thanks for all your help. Topic solved.

  16. #16
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    5
    Location
    Incase Adobe is already installed on your machine simply use ShellExecute API.

    Put this on top of all declarations

    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

    and simply use as

    ShellExecute 0&, "open", fileFullPath, 0, "Directory", 0

  17. #17
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Quote Originally Posted by Agni1978
    Incase Adobe is already installed on your machine simply use ShellExecute API.

    Put this on top of all declarations

    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

    and simply use as

    ShellExecute 0&, "open", fileFullPath, 0, "Directory", 0
    The question of the original poster was ... PRINT pdf files. So you need to change the "open" command to a "Print" command.

    More details regarding this subject can be found here : http://www.xcelfiles.com/ShellExecuteA.html

    Charlize

  18. #18

    autoprint

    Charlize[/QUOTE]

    Clicking Print command for each file is really a daunting task when there are hundred of files to be printed. The better alternate is to use FolderMill for automatic Printing PDF Files and Folders. I got it from foldermill.com It can print the entire Folders without any hassle. It really saves a lot of time while i can do other tasks.

  19. #19
    VBAX Newbie
    Joined
    Aug 2015
    Posts
    1
    Location

    Bulk PDF Printing

    Hello,

    I suggest PrintConductor for Bulk PDF Printing if u have to print hundreds of PDFs. It lets you select the entire folder at once and print it all. PrintConductor is a good printing management software. Search PrintConductor more info

  20. #20
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can someone put together a KB Item for this?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •