Problems With Outlook 2013 VBA To Send and Print an email

Status
Not open for further replies.

Horatio_too

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
Hi
We have a business need to send an email and then print the message once it has been sent.

(We operate in a regulated environment and the sent emails are stored in paper files......)

We initially tried the suggestions from

https://forums.slipstick.com/threads/90476-button-to-send-and-print-emails/

which pointed us in the right direction, but unfortunately did not actually work for us.

However, we DO have some VBA that DOES now work successfully BUT only when it is run in the code window (step-by-step).
.
If we run the VBA as the users will from a button on the outlook toolbar, then it fails, as the code to print the message is executed before the message is moved from the Outbox to Sent Items.

We have tried Wait statements, loops to check for the existence of the email in Sent Items etc and ALL work fine with the code window open - and all fail "in the real world".

The current code is attached - any help or suggestions gratefully received as always.

Kind regards
Horatio_too
 

Attachments

  • OutlookSendAndPrint.txt
    3.8 KB · Views: 592
Have you tried this timer? - http://www.vboffice.net/en/developers/pause-code-execution

Being the forgetful type, I'd use an ItemAdd macro so i didn't have to remember to use the button. It also insures the correct message is printed. If you don't want to print every message, add a dialog asking if they want to print it.

It lookslike you are checking the subject and to field before sending
If Len(obj.To) > 0 And Len(obj.Subject) > 0 Then
you don't need that with 2010 and 2013 - they check for blank subjects and outlook won't send mail with a blank to field.

Code:
Option Explicit
  
 Private WithEvents Items As Outlook.Items
  Dim strDirectory As String
 Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliSeconds As Long)
Private 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 Sub Application_Startup()
  
 Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
  
 End Sub

 Private Sub Items_ItemAdd(ByVal Item As Object)
  Dim intUserAnswer As Integer
Dim i As Long
  If TypeOf Item Is Outlook.MailItem Then
  
  Item.PrintOut
  If Item.Attachments.Count > 0 Then
  
  Select Case Item.Attachments.Count
  Case 1
  intUserAnswer = MsgBox("There is " & CStr(Item.Attachments.Count) & " attachment to this email. Do you wish to print it ?", vbYesNo)
  Case Else
  'More than one attachment
  intUserAnswer = MsgBox("There are " & CStr(Item.Attachments.Count) & " attachments to this email. Do you wish to print these ?", vbYesNo)
  End Select
  If intUserAnswer = vbYes Then
  PrintAttachments Item
  Else
  MsgBox "The attachments will not be printed"
  End If
  Else
  MsgBox "There are no attachments."
  End If

  End If
DeleteFiles
 End Sub
Public Sub PrintAttachments(Mail As Outlook.MailItem)
 On Error Resume Next
 Dim colAtts As Outlook.Attachments
 Dim oAtt As Outlook.Attachment
 Dim strFile As String
 Dim strFileType As String
strDirectory = "C:\Users\dianep\Documents\print\"
Set colAtts = Mail.Attachments
If colAtts.Count Then
  For Each oAtt In colAtts
  strFileType = LCase$(Right$(oAtt.FileName, 4))
  Select Case strFileType
  Case ".xls", ".doc", "xlsx", ".pdf", "docx"
  strFile = strDirectory & oAtt.FileName
  oAtt.SaveAsFile strFile
  ShellExecute 0, "print", strFile, vbNullString, vbNullString, 0
  Case Else
  'Do nothing
  End Select
  Next
 End If
 End Sub
Public Sub DeleteFiles()
'Call WaitTimer
Sleep 10000
'Delete file from folder
Kill strDirectory & "*.*"
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Outlook 2013 Yahoo IMAP Sync problems Using Outlook 1
O Outlook 2013 Problems sending large mails Using Outlook 4
G Problems with Active Directory Rights Management Services (AD RMS) in Outlook 2013 Exchange Server Administration 0
L Outlook 2013 setup problems Exchange Server Administration 1
T Outlook 2013 and outlook.com sync problems Using Outlook 5
R Outlook 2013 email folder sync problems using OneDrive Using Outlook 3
Jon Bakrley problems with outlook 2013 and pst files Using Outlook 4
C Outlook 2013 compatibility problems with outlook 2007 Using Outlook 3
G Outlook 2013 problems Using Outlook 2
K Outlook for Mac problems Using Outlook 0
V iCloud calendar problems, Outlook shuts down immediately Using Outlook 2
V Outlook 2016 Outlook-2016 and iCloud for Windows - Problems Using Outlook 11
T Many problems with outlook.com - will upgrade to Outlook soon Using Outlook 1
Q Problems with Autodiscover Outlook 2016 Using Outlook 0
M Indexing Problems Outlook 365 Using Outlook 2
C 2016 Outlook Certificate Problems Outlook VBA and Custom Forms 3
S Problems syncing emails with webmail after changing to Outlook 2016 Using Outlook 1
Diane Poremsky iCloud and Outlook Problems: Syncing Calendar Using Outlook 0
G Outlook problems Using Outlook 2
A Problems Opening Excel from Outlook Outlook VBA and Custom Forms 3
T Outlook Connector calendar sync problems with Android phone Using Outlook.com accounts in Outlook 1
J Outlook 2010 sync problems with Outlook Connector Using Outlook 1
B Outlook being closed causing problems during copy to Excel Outlook VBA and Custom Forms 6
A Problems with Outlook Connector in Outlook 2010 Using Outlook 2
J problems downloading POP3 emails to Outlook 2003 Using Outlook 1
M Outlook problems after yesterday's update Using Outlook 4
L Weird Inbox problems Outlook 2010/13 and Exchange Exchange Server Administration 2
P outlook.com problems Using Outlook 0
D Outlook and Icloud problems Using Outlook 0
E Outlook 2010 network printing problems Using Outlook 4
P Encoding problems after synchronization with Outlook Connector Using Outlook.com accounts in Outlook 6
J problems with viewing aol emails in Outlook Using Outlook 3
B Problems with outlook calendar and android phone syncing calendars Using Outlook.com accounts in Outlook 1
P Sync Problems Outlook with iPad 2 Using Outlook 3
C migrating from Windows Live Mail over to Outlook 2010 Problems ... Using Outlook 2
J Outlook 2010 and Icloud multiple calenders problems. Using Outlook 10
T Outlook 2007 - iTunes - iCloud Sync Problems Using Outlook 7
S Public FolderEmail Sort Problems Outlook 2010/Migration from 2003 SBS to 2011S Using Outlook 3
S Outlook 2010 and certificate problems Using Outlook 9
M Several Problems using Outlook Using Outlook 3
W Outlook exp. 2007 problems - won't receive new mail Using Outlook 1
P Printing problems with MS Outlook 2000 Using Outlook 1
A Two strange calender problems in Outlook 2003 ... Using Outlook 2
T Outlook 2007 shut down problems Using Outlook 1
A Outlook Advanced Search Problems...returns with an Error: Argument Outlook VBA and Custom Forms 1
J Problems with Outlook.Recipient.Resolve Outlook VBA and Custom Forms 3
T Problems re-installing BCM for Outlook 2007 BCM (Business Contact Manager) 2
D a general question re how backup programs handle pst files ... I have no problems, just curious Using Outlook 1
A iCloud add in problems Using Outlook 4
M Export-Import .pst file problems Using Outlook 2

Similar threads

Back
Top