Consulting

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

Thread: Solved: How to auto print .PDF files

  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,285
    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
    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.

  4. #4
    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.

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,285
    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

  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,285
    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 Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    It should be

    Shell "c:\Program Files\Adobe\Acrobat 8.0\Reader\AcroRd32.exe /p /h " & FileName

    because the variable FileName includes the filename and path. Atmt.FileName won't be found since it's a temporary file attached to your email.

    --JP


    Quote Originally Posted by pitspawn8
    [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
    [/vba]

  15. #15
    I've tried that but still the same error with adobe.

  16. #16
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,285
    Location
    Quote Originally Posted by pitspawn8
    I've tried that but still the same error with adobe.
    You are sure you only save pdf's with this routine.

    Charlize

  17. #17
    Yes.

    I've solved the problem for the most part. The last thing that I want to do with it is set it to pull files from an archive or public folder. I've tried a couple of different thing but I can't figure it out. Any thoughts? Here is the corrected code.

    [VBA]'This macro was written by Cpl Malcolm K. Quick with the help of Sgt Jeremiah G. Mort
    'This code is used to automatically print all *.pdf files from the specified folder.

    Option Explicit

    Sub Printattachments()

    'The next four variable declarations are specific to Microsoft Outlook.
    Dim ns As NameSpace
    Dim smbbox As MAPIFolder
    Dim item As Object
    Dim atmt As Attachment

    'These are standard counters and variables.
    Dim filename, thedate As String
    Dim i As Integer

    'These lines are responsible for the folder that the e-mails will be pulled from.
    Set ns = GetNamespace("MAPI")
    Set smbbox = ns.GetDefaultFolder(olFolderDrafts)

    'This If statement checks to see if there are any e-mails in the folder.
    If smbbox.Items.Count = 0 Then
    MsgBox "There are no messages in the Inbox.", vbInformation, "Nothing Found!"
    Exit Sub
    End If

    'This block is responsible for looking for attachments in an Outlook folder and dealing with them.
    i = 0
    'This looks for e-mails in the specified folder
    For Each item In smbbox.Items
    'This If Statement looks for only *.pdf files.
    For Each atmt In item.Attachments
    If UCase(Right(atmt.filename, 3)) = "PDF" Then
    'This next line creates a string with a specific Date/Time format.
    thedate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & " " & _
    Right("0" & Hour(Now), 2) & "." & Right("0" & Minute(Now), 2)

    ' This ine creates specific file names for each attachment with the Date/time and _
    'counter so the chance of file name duplication is reduced.
    filename = "c:\E-Mail\" & thedate & " - " & i & " - " & atmt.filename

    'This line saves the attachments.
    atmt.SaveAsFile filename

    'This line runs Acrobat Reader in a command prompt format. It opens and prints a _
    'a specified .pdf file.
    Shell """C:\Program Files\Adobe\reader 8.0\Reader\AcroRd32.exe"" /t """ _
    & filename & """"

    End If

    i = i + 1

    Next atmt
    Next item

    'This block checks to see if any attachments were found and informs the user.
    If i > 0 Then
    MsgBox "Attachments have been found and dealt with."
    Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
    "Finished!"
    End If

    'This sets the microsoft Outlook specific variables to nothin.
    Set atmt = Nothing
    Set item = Nothing
    Set ns = Nothing



    End Sub[/VBA]

  18. #18
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,285
    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

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

  20. #20
    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

Posting Permissions

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