Consulting

Results 1 to 3 of 3

Thread: Print outlook attachments as they arrive

  1. #1

    Print outlook attachments as they arrive

    Hi,

    I'm new to VBA & had a bit of a break from when I first picked it up a few months ago.

    I want to print certain attachments from a certain sender. Tried to do this via outlook rule orig (pre discovering VBA) and could only print out actual email rather than attachment.

    Dont know if better to achieve as purely VBA or run a script rule I've read about. Tried to amend a code to do this (remember few months ago had to do a registry hack to get "run a script" to show in outlook rules). But then brings up an empty box where I think should be showing my script.

    Any help much appreciated.

  2. #2
    In order to print the attachments you first have to save them. You can do that from a suitable script e.g. PrintAttachments below.
    This saves to the Temp folder then prints using the appropriate application. Test by selecting a message with an attachment and run TestPrint

    Option Explicit
    
    Sub TestPrint()
    Dim olMsg As MailItem
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set olMsg = ActiveInspector.currentItem
            Case olExplorer
                Set olMsg = Application.ActiveExplorer.Selection.Item(1)
        End Select
        PrintAttachments olMsg
    lbl_Exit:
        Exit Sub
    End Sub
    
    Public Sub PrintAttachments(olItem As MailItem)
    'Graham Mayor - http://www.gmayor.com - Last updated - 26 May 2017
    Dim olAttach As Attachment
    Dim sName As String
    Dim j As Long
    Dim sPath As String
        On Error Resume Next
        sPath = Environ("TEMP") & "\"
        If olItem.Attachments.Count > 0 Then
            For j = 1 To olItem.Attachments.Count
                Set olAttach = olItem.Attachments(j)
                If Not olAttach.FileName Like "image*.*" Then
                    sName = olAttach.FileName
                    olAttach.SaveAsFile sPath & sName
                    PrintFile sPath & sName
                End If
            Next j
        End If
    lbl_Exit:
        Set olAttach = Nothing
        Set olItem = Nothing
        Exit Sub
    End Sub
    
    Private Sub PrintFile(ByVal strPath As String)
    Dim FSO As FileSystemObject
    Dim oShell As Object
    Dim Fldr As Object
    Dim FldrItem As Object
        Set FSO = New FileSystemObject
        Set oShell = CreateObject("Shell.Application")
        Set Fldr = oShell.NameSpace(0)
        Set FldrItem = Fldr.ParseName(strPath)
        FldrItem.InvokeVerbEx ("print")
    lbl_Exit:
        If Not FSO Is Nothing Then Set FSO = Nothing
        If Not Fldr Is Nothing Then Set Fldr = Nothing
        If Not FldrItem Is Nothing Then Set FldrItem = Nothing
        If Not oShell Is Nothing Then Set oShell = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Hi Graham,

    Thanks for the reply and script....and cool website .

    I already have a similar script to yours for printing attachments, when I've opened this script I can see similar to yours that it saves the attach's.....never knew this was required so good to know thanks.

    But is there a way I can adapt either script so it automatically prints emails from certain people? This is why I tried to go down the outlook rule run script route. Added the option by some registry hack I read, but then its blank when I tick run script and expect my scripts to be listed for selection.

    Thanks

Posting Permissions

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