none
Outlook 2010 : Automatically printing attachments specific file types RRS feed

  • Question

  • Hello,

    I've got a script to automatically print attachments cribbed off google.

    However, it automatically prints signature attachments as well as any attachment as the script just instructs to print the attachment.

    Is there additional code I can slide in which will control the script to ONLY print ".pdf", ".doc", ".xls", "xlsx" and "docx"?

    And will this stop the rule printing anything else?

    Script is below.......be grateful for any help! :-)

    Sub LSPrint(Item As Outlook.MailItem) 

    •             On Error GoTo OError
    •               
    •             'detect Temp
    •             Dim oFS As FileSystemObject
    •             Dim sTempFolder As String
    •             Set oFS = New FileSystemObject
    •             'Temporary Folder Path
    •             sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
    •             
    •             'creates a special temp folder
    •             cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
    •             MkDir (cTmpFld)
    •             
    •             'save & print
    •             Dim oAtt As Attachment
    •             For Each oAtt In Item.Attachments
    •               FileName = oAtt.FileName
    •               FullFile = cTmpFld & "\" & FileName
    •               
    •               'save attachment
    •               oAtt.SaveAsFile (FullFile)
    •               
    •               'prints attachment
    •               Set objShell = CreateObject("Shell.Application")
    •               Set objFolder = objShell.NameSpace(0)
    •               Set objFolderItem = objFolder.ParseName(FullFile)
    •               objFolderItem.InvokeVerbEx ("print")
    •        
    •             Next oAtt
    •             
    •             'Cleanup
    •             If Not oFS Is Nothing Then Set oFS = Nothing
    •             If Not objFolder Is Nothing Then Set objFolder = Nothing
    •             If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
    •             If Not objShell Is Nothing Then Set objShell = Nothing
    •             
    •           OError:
    •             If Err <> 0 Then
    •               MsgBox Err.Number & " - " & Err.Description
    •               Err.Clear
    •             End If
    •             Exit Sub

    Thursday, December 8, 2016 2:07 PM

All replies

  • Hello,

    You can check whether a file name contains or ends with the specified keywords: ".pdf", ".doc", ".xls", "xlsx" and "docx". VBA provides the InStr function which returns an integer specifying the start position of the first occurrence of one string within another. Basically you need to check whether the Filename string contains the keyword and skip the iteration if it does. 

    You may find the Getting Started with VBA in Outlook 2010 article helpful.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 8, 2016 10:55 PM