Check the Subject Field in Outlook - vba

I see this code all over the internet and everyone says how wonderful it works but it doesn't for me, I'm clueless, any ideas why?
I have Windows 7 and Outlook 2010.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
End Sub

Please ensure you have set the correct references in your project for Outlook 2010 Library. Also ensure that Outlook 2010 should already be in running state. Thanks

you posted a link to a screen capture on May 20 at 16:04
go to that same place in the code and delete all the code in the sub, including sub and endsub (the code that you posted)
now click on "application" dropdown at top-left
you should see the following fill in
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
End Sub
if something else pops up, then click on ItemSend in dropdown on top right
put in one command to get this very minimal sub (if it does not work, then nothing else will)
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
msgbox "this works"
End Sub
now send any email, if it works then you should see a dialog box pop up
note: i have found that "just pasting in code" does not always work and you have to click on the dropdown menus, after you paste in the code, to get the sub to be registered

Related

How to close or dismiss an Outlook Reminder via Outlook VBA

I want to run a Macro in Outlook at a certain time and so I'm using the Outlook Reminders to do it. I have written the below code, which successfully runs the Macro but after it has finished the If statement, it then pops up the reminder which I don't need to see and so therefore need to close/dismiss it.
Public Sub Application_Reminder(ByVal Item As Object)
If Item.Subject = "Refresh Data Test" Then
Call RunExcelMacros.TestRun
End If
End Sub
Please can someone help suggest how I can dismiss the reminder?
Okay, I think I've got it - the below seems to work, all code is setup in the "ThisOutlookSession" Module:
Private WithEvents OutlookReminders As Outlook.Reminders
Public Sub Application_Reminder(ByVal Item As Object)
Set OutlookReminders = Outlook.Reminders
If Item.Subject = "Refresh Data Test" Then
Call RunExcelMacros.TestRun
End If
End Sub
Private Sub OutlookReminders_BeforeReminderShow(Cancel As Boolean)
Dim OutlookReminder As Reminder
'After the "Application_Reminder" has run it will then run this code straight after which stops the reminder from actually popping up
For Each OutlookReminder In OutlookReminders
If OutlookReminder.Caption = "Refresh Data Test" Then
If OutlookReminder.IsVisible Then
OutlookReminder.Dismiss
Cancel = True
End If
Exit For
End If
Next OutlookReminder
End Sub

vba code to send email based on dialog box answer

Here are the steps that I want to happen or something similar. I basically want a dialog box to cancel an email if special instructions were not reviewed:
The email is tagged with a Category named: Special Delivery. This is already done prior to everything.
I complete the job request and ready to send back a reply.
Click on "Reply All"
Email pops up, I can add attachments and body of the text etc...
When I click send I want a dialog box to pop up asking "Did you review delivery instructions?"
If I hit "OK", the email sends, if I hit "Cancel" the email closes and nothing is sent.
This is the code I'm using now; however, it sends the email regardless of what I click... Any help would be great.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Categories = "Special Delivery" And MsgBox("Did you review delivery instructions?", vbOKCancel) <> vbOK Then
Cancel = True
End If
End Sub
Take the Item.Categories out because the Item will not have Categories when your replying and use vbOKCancel) = vbCancel
Example
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Prompt As String
Prompt = "Did you review delivery instructions?"
If MsgBox(Prompt, vbOKCancel) = vbCancel Then
Cancel = True
End If
End Sub
You can determine which condition is the problem by separating them.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Categories = "Special Delivery" Then
debug.print "Item has Special Delivery category"
If MsgBox("Did you review delivery instructions?", vbOKCancel) <> vbOK Then
Cancel = True
End If
Else
debug.print "If item has Special Delivery category it was not identified"
End If
End Sub

VBA Button to Clear a Range of Cells

As the title states, I am trying to create a button to clear a range of cells. Prior to clearing the cells, I have a dialog box pop up to confirm the selection. Here is my code:
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbNo Then Exit Sub
Range("A28:AA47").ClearContents
End Sub
The code works when I'm in Design Mode and press "Play" but the button won't work on my sheet when I'm out of Design Mode.
Is the code completely ending itself when I select "No", to the point where it will not run again even if I hit the button again?
Your code looks a little incomplete. Try this instead:
EDIT: Cleaned it up a little more. No need for a vbNo, as if you don't select yes, the subroutine will simply end.
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbYes Then
Range("A28:AA47").ClearContents
End If
End Sub

How can I automatically run a macro when an email is sent in Outlook?

The script below works great but I have to manually run the Initialize_handler routine every time I open Outlook for it to work.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.Application")
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
As far as I can see to make this work automatically I should be able to add the below script to ThisOutlookSession. However this doesn't work and I have no idea why.
My macro security is set properly and it is running the code on startup but for some reason it doesn't work until I manually open the VBA editor click into Initialize_handler and press F5.
Private Sub Application_Startup()
Initialize_handler
End Sub
The convoluted method described here https://msdn.microsoft.com/en-us/library/office/ff865076.aspx indicates "The sample code must be placed in a class module".
I suggest you use the special class module ThisOutlookSession only. You could experiment with your own class module but if you just want this to work then replace all your code with this in ThisOutlookSession.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
You can instead put it directly in ThisOutlookSession:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "Sent somthing!"
End Sub
As simple as that. Note you need to change your outMail.Display to outMail.Display (True) and there you are, full code:
...
...
...
outMail.Display (True)
On Error Resume Next
bSent = outMail.sent 'This will NOT SEND. Used to get error.
If Err <> 0 Then
'Email was sent. Put followed actions here.
Else
'Email was not sent. Put followed actions here.
End If
On Error GoTo 0
Pros:
You get what you want
It's simple.
Cons:
Excel (or any other runtime you are running this code from) will freeze until you cancel or send email.

Prompt for password when sending

I want Outlook to prompt for a password or some sort of authentication on all outgoing mail items, because someone keeps sending on behalf on my account.
I have written:
If Omail.SendUsingAccount = "My Domain Email account typed here" Then
Sub password()
Dim pass As String
pass = InputBox("Enter Password")
If pass <> "thepassword" Then Exit Sub
End Sub
This doesn't work. After I have the correct code can I then just insert that into a custom action rule?
Please use the below code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
prompt$ = "Enter Password to Send Mail"
Dim pass As String
pass = InputBox("Enter Password")
If pass <> "yourpwd" Then
Cancel = True
End If
End Sub
Its tested and its working fine.
make sure you have enabled macro from trust centre.
You can develop a VBA macro where you can handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
For example:
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
You may find the Getting Started with VBA in Outlook 2010 article helpful.