Click here to Skip to main content
15,889,851 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
when i send a email it must print without the attachment but the receiving must print the email aswell the attachments im getting to work but i get a error code were it says 424 object required when i get receiving emails but it still printing as how i want it to work so i don't know if it is the code or the printer when it is off or in sleep mode.

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 Ns As Outlook.NameSpace
  Dim Folder As Outlook.MAPIFolder
 
  Set Ns = Application.GetNamespace("MAPI")
  Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
  Set Items = Folder.Items
End Sub
 
Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
      Item.PrintOut
    
  End If
End Sub

Sub attachmentPrint(Item As Outlook.MailItem)

On Error GoTo OError

' This script finds the system's Temp folders,
' saves any attachments, and runs the Print
' command for that file.

    Dim ofs As FileSystemObject
    Dim sTempFolder As String
    Set ofs = New FileSystemObject
    sTempFolder = ofs.GetSpecialFolder(TemporaryFolder)

    cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
    MkDir (cTmpFld)
    
    ' in the next few lines, you'll see an entry that
    ' says FileType = . This line gets the last 4
    ' characters of the file name, which we'll use later.

    Dim oAtt As Attachment
    For Each oAtt In Item.Attachments
        FileName = oAtt.FileName
        FileType = LCase$(Right$(FileName, 4))
        FullFile = cTmpFld & "\" & FileName
        oAtt.SaveAsFile (FullFile)
        
        ' We're using the FileType text. Note that it's the
        ' last 4 characters of the file name, which is why
        ' the next chunk has .xls and xlsx (without the period)
        ' - the period counts as the fourth character.
        ' Insert any file extensions you want printed.

        Select Case FileType
        Case ".doc", "docx", ".xls", "xlsx", ".ppt", "pptx", ".pdf"
            Set objShell = CreateObject("Shell.Application")
            Set objFolder = objShell.NameSpace(0)
            Set objFolderItem = objFolder.ParseName(FullFile)
            objFolderItem.InvokeVerbEx ("print")
        End Select
    Next oAtt


    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


What I have tried:

i have tried to make one as module and one as the app but no luck i even tried to specify in the rules
print settings tick a box to print attachments automatically
working but also on sending don't want to print attach on sending only on receiving
Posted
Updated 19-May-16 5:40am

1 solution

That is a extremely odd requirement and one that will be nearly impossible to enforce outside an organization. You tag this question as C#, but your code is VBA.

The only way I can think of to even attempt this is using VSTO to create two add-ons for Outlook. You can see in introduction to the topic Walkthrough: Creating Your First Application-Level Add-in for Outlook[^]

You will have to install the add-ons on both sides, the sender and receiver, and both will have to be using Outlook. If the receiver is outside your organization good luck getting that person to install an add-on just to receive emails from you.

What my first suggestion would be is to step back and look at why you are attempting this in the first place. If your only answer is because it is required then you need to look at your requirements. Because that requirement is almost impossible to implement in any correct form or fashion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900