Microsoft Office Forums Automatic Attachment Script for Outlook 16 64 bit

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2019, 07:44 PM
Dale Dale is offline Automatic Attachment Script for Outlook 16 64 bit Windows 7 64bit Automatic Attachment Script for Outlook 16 64 bit Office 2016
Novice
Automatic Attachment Script for Outlook 16 64 bit
 
Join Date: Jul 2019
Posts: 3
Dale is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-09-2019, 10:18 PM
gmayor's Avatar
gmayor gmayor is offline Automatic Attachment Script for Outlook 16 64 bit Windows 10 Automatic Attachment Script for Outlook 16 64 bit Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,800
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

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
Reply With Quote
  #3  
Old 07-10-2019, 03:08 PM
Dale Dale is offline Automatic Attachment Script for Outlook 16 64 bit Windows 7 64bit Automatic Attachment Script for Outlook 16 64 bit Office 2016
Novice
Automatic Attachment Script for Outlook 16 64 bit
 
Join Date: Jul 2019
Posts: 3
Dale is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 07-10-2019, 07:54 PM
gmayor's Avatar
gmayor gmayor is offline Automatic Attachment Script for Outlook 16 64 bit Windows 10 Automatic Attachment Script for Outlook 16 64 bit Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,800
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

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
Reply With Quote
  #5  
Old 07-10-2019, 08:25 PM
Dale Dale is offline Automatic Attachment Script for Outlook 16 64 bit Windows 7 64bit Automatic Attachment Script for Outlook 16 64 bit Office 2016
Novice
Automatic Attachment Script for Outlook 16 64 bit
 
Join Date: Jul 2019
Posts: 3
Dale is on a distinguished road
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Attachment Script for Outlook 16 64 bit 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
Automatic Attachment Script for Outlook 16 64 bit VBL code for automatic attachment of file into email DrLoveday1 Outlook 1 04-12-2013 05:36 AM
Automatic Attachment Script for Outlook 16 64 bit 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


All times are GMT -7. The time now is 08:51 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft