#1
|
|||
|
|||
Automatic Attachment Script for Outlook 16 64 bit
Afternoon,
I have struggled to find a suitable script for what i need. So im requiring a script for MS Outlook 2016 64 bit version, I currently have a rule for any invoices that get emailed to go to the Invoice folder. I then want the attachments, which are PDF's, to print automatically when sent to this folder. Any help is much appreciated. Thanks |
#2
|
||||
|
||||
Run the following script (PrintAttachment) from the rule that moves the invoices. It should work in the 64 bit version of Outlook though I cannot test it here.
You may have to extend the sleep period if your PC is slow to save and open the PDFs. I have included a text macro so that you can test the process on a message with a PDF attachment. Code:
Option Explicit Public Declare PtrSafe 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 Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub TestPrint() Dim olMsg As MailItem On Error Resume Next Set olMsg = ActiveExplorer.Selection.Item(1) PrintAttachment olMsg lbl_Exit: Exit Sub End Sub Sub PrintAttachment(olItem As MailItem) 'Graham Mayor - https://www.gmayor.com - Last updated - 10 Jul 2019 Dim olAttach As Attachment Dim strFName As String Dim strExt As String Dim j As Long Dim fso As Object, TmpFolder As Object Dim tmpPath As String 'Get the user's TempFolder to store the temporary file Set fso = CreateObject("Scripting.FileSystemObject") tmpPath = fso.GetSpecialFolder(2) & "\" On Error GoTo lbl_Exit If olItem.Attachments.Count > 0 Then For j = 1 To olItem.Attachments.Count Set olAttach = olItem.Attachments(j) If olAttach.fileName Like "*.pdf" Then strFName = olAttach.fileName olAttach.SaveAsFile tmpPath & strFName NewShell tmpPath & strFName, 3 Sleep 2000 Kill tmpPath & strFName End If Next j End If lbl_Exit: Set olAttach = Nothing Set olItem = Nothing Exit Sub End Sub Public Sub NewShell(cmdLine As String, lngWindowHndl As Long) ShellExecute lngWindowHndl, "Print", cmdLine, "", "", 1 lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thanks Gmayor
I think i am a little on the special side when it comes to scripts. I have copied and pasted everything from your script into VBA on Outlook and adjusting my rule to include the script. I have tested each one with adjusting the sleep and i cant seem to make the script work. Am i doing something wrong? Thanks |
#4
|
||||
|
||||
The code should go in a new VBA module.
Select a message and run the macro TestPrint from the VBA editor. If Sleep needs to be changed you will get an error message about the missing file. Do you get any other error messages? Try commenting out the on error lines and see if that prompts a crash..
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
So i have opened an email up with an invoice and have gone into the developer tab from the email to run the test print and still nothing.
Im not getting any error messages. However, I have just gone into manage rules and alerts and clicked on run rules now then selected my rule and ran it with success, but when i try to email an invoice from another account it wont print with no error messages. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Outlook VBA Script | kcm5153 | Outlook | 1 | 04-07-2015 11:41 PM |
Adapt a script used in Word to highlight words, to work in Outlook? | flatop | Outlook | 5 | 07-15-2014 01:07 PM |
VBL code for automatic attachment of file into email | DrLoveday1 | Outlook | 1 | 04-12-2013 05:36 AM |
Outlook Script Help | TheInfinetGroup | Outlook | 1 | 03-02-2013 07:43 AM |
Automatic reply with variable attachment | subby80 | Outlook | 0 | 10-20-2011 06:59 AM |