Print Email from sender

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
709
Office Version
  1. 365
Hi,

i am using outlook office 365 now and i have a rule and vba script that in 2007 worked fine but now even though i put as default Internet explorer as PDF reader keep asking every time that a new email comes that i have a rule for which program to use open the PDF and I have to print it myself once it opens it before in outlook 2007 will print it automatically, please help.

here is the code:

Code:
Option Explicit



Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, _
        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr


'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




Public Sub PrintEmailFromSender(outMailItem As Outlook.MailItem)


    Dim outAttachment As Outlook.Attachment
    Dim saveInFolder As String
    Dim fileType As String, filePath As String
    
    'Folder where attachments in this email will be saved
    
    saveInFolder = "C:\Attachments\"
    
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    'Print this email
    
    outMailItem.PrintOut
    
    'Extract and print attachments in this email
    
    For Each outAttachment In outMailItem.Attachments
    
        'Get the file type from the filename
        fileType = Mid(outAttachment.FileName, InStrRev(outAttachment.FileName, ".") + 1)
        
        Select Case LCase(fileType)
        
            'Add additional file types below
            Case "doc", "docx", "pdf" '"xls", "xlsx",
            
                'Save attachment in folder
                filePath = saveInFolder & outAttachment.FileName
                outAttachment.SaveAsFile filePath
            
                'Print saved attachment
                ShellExecute 0, "print", filePath, vbNullString, vbNullString, 0
        End Select
    
    Next


End Sub

Thanks,
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,546
Messages
5,838,018
Members
430,526
Latest member
NiceGuyWithExcel2007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top