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