Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30013

Outlook VBA-Script: Check for pdf or zip in attachment, save and print it automatically

$
0
0

I use an Outlook VBA-Script to print attachments if the attachment is a pdf. If the attachment is a zip-file then the script saves it to a specified folder on my machine. However, the content of the ZIP-File has to be unzipped first and then printed manually which gets very annoying with a high volume of e-mails.

Private 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 Sub PrintSelectedAttachments_Modul1()
  Dim Exp As Outlook.Explorer
  Dim Sel As Outlook.Selection
  Dim obj As Object

  Set Exp = Application.ActiveExplorer
  Set Sel = Exp.Selection

  For Each obj In Sel

    If TypeOf obj Is Outlook.MailItem Then
      PrintAttachments_Modul1 obj

    End If
  Next
End Sub
_________________________________________________________________________________
Private Sub PrintAttachments_Modul1(oMail As Outlook.MailItem)
  On Error Resume Next
  Dim colAtts As Outlook.Attachments
  Dim oAtt As Outlook.Attachment
  Dim sFile As String
  Dim sDirectory As String
  Dim sFileType As String

  sDirectory = "C:\Temp\Rechnungen\Outlook\"

  Set colAtts = oMail.Attachments

  If colAtts.Count Then
    For Each oAtt In colAtts

      sFileType = LCase$(Right$(oAtt.FileName, 4))

      Select Case sFileType
      Case ".xls", ".doc", ".pdf"
        sFile = ATTACHMENT_DIRECTORY & oAtt.FileName
        oAtt.SaveAsFile sFile
        ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
      End Select
    Next
  End If
End Sub
________________________________________________________________________________
Public Sub SaveAttachments_ZIP()

Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Objekt
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String

' Specify path
   strFolderpath = "C:\Temp\Rechnungen\Outlook\"
On Error Resume Next

   Set objOL = CreateObject("Outlook.Application")

   Set objSelection = objOL.ActiveExplorer.Selection

For Each objMsg In objSelection
   Set objAttachments = objMsg.Attachments
   lngCount = objAttachments.Count

If lngCount > 0 Then
  For i = lngCount To 1 Step -1

' Read filename
   strFile = objAttachments.Item(i).FileName

' Merge
   strFile = strFolderpath & strFile

' Save file
   objAttachments.Item(i).SaveAsFile strFile

  Next i
End If

Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

Is it possible modify this script as following?:

  1. Check for attachment in mail: if PDF then save on local machine

else

1.2. Check for ZIP-file in mail: if ZIP-file then unzip it and save the unzipped files on local machine

  1. Afterwards print all the saved files with the standard printer

Viewing all articles
Browse latest Browse all 30013

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>