How do I make Outlook 2010 print attachments automatically?

2

I'm trying to get Outlook 2010 to print attachments automatically apon arrival.

I've found this on the internet. The VBA code is

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

  End Sub

I allowed macros to run. I pasted the code into the ThisOutlookSession in the VBA editor and added a reference to Microsoft Scripting Runtime. I created a rule checking whether the new message is from me and if so running the script. I sent a message with a .doc attachment to myself, and I got the error message "424 - Object required" upon receipt.

I don't have a printer at home (I need the code for a different place), so I've set Microsoft XPS Writer as my default printer just to see if it works. Is this the reason for the error? If not, what is and how do I fix it?

And most importantly, how do I get the job done? I need to use a VBA script (not an add-on), and I'm new to VBA.

I'm using Windows XP now, but I need the thing to work on Windows 7.

Michał Masny

Posted 2013-07-02T19:44:18.530

Reputation: 296

If you open the VBA editor, you could set a break point to the beginning of your macro. Then, repeat your test with sending a mail. The editor will pop up und you can execute the macro line by line with F8. This way, we get more details, which line causes the error. – nixda – 2013-07-02T19:49:39.623

Have you tried this VBA code too? Or maybe this rule-based solution?

– nixda – 2013-07-02T19:51:50.977

@nixda As for the second question, yes a couple of times, mostly in this version, and nothing seemed to happen. But I didn't add any rules here -- I just ran the script as Diane Poremsky advises in her post.

– Michał Masny – 2013-07-02T19:58:46.270

@nixda The error message appears after I press F8 with this line highlighted: "MsgBox Err.Number & " - " & Err.Description". – Michał Masny – 2013-07-02T20:00:57.997

@nixda The solution in the second link is to a different problem. Rules alone are not enough to print attachments automatically. They only allow to print messages. – Michał Masny – 2013-07-02T20:09:28.880

The second line On Error GoTo OError is a error handler. Disable it temporarly with a ' in front and repeat the mail test. A different line should now cause the error. – nixda – 2013-07-02T20:14:02.840

@nixda Now there's no error message at all, and nothing seems to happen. Should I list the lines that actually do get highlighted? (Not all of them do.) – Michał Masny – 2013-07-02T20:17:13.557

let us continue this discussion in chat

– nixda – 2013-07-02T20:22:30.687

Answers

1

Paste the following code into ThisOutlookSession.

Edit the code as needed then click in the Application_Startup() macro and press Run button (F8). This starts the macro without the need to restart Outlook.

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

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olNameSpace As Outlook.NameSpace
    Dim Folder As Outlook.MAPIFolder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set Folder = olNameSpace.GetDefaultFolder(olFolderInbox)
    Set Items = Folder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        PrintAttachments Item
    End If
End Sub

Private Sub PrintAttachments(olItem As Outlook.MailItem)
    On Error Resume Next
    Dim colAtts As Outlook.Attachments
    Dim olAtt As Outlook.Attachment
    Dim sFile As String
    Dim sDirectory As String
    Dim sFileType As String

    sDirectory = "C:\Attachments"

    Set colAtts = olItem.Attachments

    If colAtts.Count Then
        For Each olAtt In colAtts
        '// List file types -
        sFileType = LCase$(Right$(olAtt.FileName, 4))

        Select Case sFileType
            Case ".xls", ".doc"
            sFile = ATTACHMENT_DIRECTORY & olAtt.FileName
            olAtt.SaveAsFile sFile
            ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
            End Select
        Next
    End If
End Sub

See Print Attachments Automatically

0m3r

Posted 2013-07-02T19:44:18.530

Reputation: 937