Richardxtc
New Member
- Joined
- May 7, 2012
- Messages
- 9
HI,
I have been trying to get help on this matter however the forum i was asking on was taking a while to respond. I need to get this code working, where an email with attachements will automatically be created when an email with a very specific subject is received. I used the coding:
Private Sub Application_NewMail()
Dim strattach As String
Dim myOlApp As Outlook.Application
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Set myOlApp = CreateObject("Outlook.Application")
Set OLNS = myOlApp.GetNamespace("MAPI")
Set myInbox = OLNS.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
If myitems.Subject = "Send Cashflow" Then
Set MyMail = myitems.Reply
MyMail.To = "Email to be sent to Here"
MyMail.Subject = "Cashflow"
MyMail.Body = "Please find attached Cashflow"
Set myAttachments = MyMail.Attachments
strattach = "Attachment Link Here"
myAttachments.Add strattach, olByValue, 1
MyMail.Display
End If
MsgBox ("Cashflow has been sent."), , "Completed"
End Sub
I also had this code which i cut down to my details:
Private Sub Application_NewMail()
Dim strSender, strSubject, strBody, strattach As String
Set ol = GetObject(, "OUTLOOK.APPLICATION")
Set MAPI = ol.GetNamespace("MAPI")
Set mystore = MAPI.GetDefaultFolder(olFolderInbox)
Set myItems = myinbox.items
If myItem.Subject = "send cashflow" Then
Strattach = "Attachment Link Here"
Set MyMail = myItem.Reply
MyMail.To = "Email address here"
MyMail.Subject = "TCC Cashflow"
MyMail.Body = "Please find attached Cashflow"
Set myAttachments = MyMail.Attachments
myAttachments.Add strattach, olByValue, 1
MyMail.Display
MsgBox ("Cashflow has been sent."), , "Completed"
End If
End Sub
which actually worked however it created an email whenever an email was received regardless of the subject.
I then went on to another forum and a member helped me with this coding:
Private Sub Application_NewMail()
' In ThisOutlookSession module
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFld As Outlook.MAPIFolder
Dim olMail As Outlook.mailItem
Debug.Print "Newmail triggered." 'Remove later if you get this working
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFld = olNS.GetDefaultFolder(olFolderInbox)
olFld.Items.sort "Received", False
Set olMail = olFld.Items.GetFirst
SendCashflowReply olMail
Set olMail = Nothing
Set olFld = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
Private Sub SendCashflowReply(ByVal oMail As Outlook.mailItem)
Dim myMail As Outlook.mailItem
If InStr(1, LCase(oMail.Subject), "send cashflow") > 0 Then
Set myMail = oMail.reply
myMail.To = "Email to be sent to Here"
myMail.Subject = "Cashflow"
myMail.Body = "Please find attached Cashflow"
myMail.Display
MsgBox ("Reply has generated."), , "Completed"
Else
Debug.Print "No match: LCase(oMail.Subject) is " & LCase(oMail.Subject) 'Remove later if you get this working
End If
End Sub
But this does nothing, when an email with the specific subject is recieved, no email is created.
Can anyone help me please.
Thanks
I have been trying to get help on this matter however the forum i was asking on was taking a while to respond. I need to get this code working, where an email with attachements will automatically be created when an email with a very specific subject is received. I used the coding:
Private Sub Application_NewMail()
Dim strattach As String
Dim myOlApp As Outlook.Application
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Set myOlApp = CreateObject("Outlook.Application")
Set OLNS = myOlApp.GetNamespace("MAPI")
Set myInbox = OLNS.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
If myitems.Subject = "Send Cashflow" Then
Set MyMail = myitems.Reply
MyMail.To = "Email to be sent to Here"
MyMail.Subject = "Cashflow"
MyMail.Body = "Please find attached Cashflow"
Set myAttachments = MyMail.Attachments
strattach = "Attachment Link Here"
myAttachments.Add strattach, olByValue, 1
MyMail.Display
End If
MsgBox ("Cashflow has been sent."), , "Completed"
End Sub
I also had this code which i cut down to my details:
Private Sub Application_NewMail()
Dim strSender, strSubject, strBody, strattach As String
Set ol = GetObject(, "OUTLOOK.APPLICATION")
Set MAPI = ol.GetNamespace("MAPI")
Set mystore = MAPI.GetDefaultFolder(olFolderInbox)
Set myItems = myinbox.items
If myItem.Subject = "send cashflow" Then
Strattach = "Attachment Link Here"
Set MyMail = myItem.Reply
MyMail.To = "Email address here"
MyMail.Subject = "TCC Cashflow"
MyMail.Body = "Please find attached Cashflow"
Set myAttachments = MyMail.Attachments
myAttachments.Add strattach, olByValue, 1
MyMail.Display
MsgBox ("Cashflow has been sent."), , "Completed"
End If
End Sub
which actually worked however it created an email whenever an email was received regardless of the subject.
I then went on to another forum and a member helped me with this coding:
Private Sub Application_NewMail()
' In ThisOutlookSession module
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFld As Outlook.MAPIFolder
Dim olMail As Outlook.mailItem
Debug.Print "Newmail triggered." 'Remove later if you get this working
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFld = olNS.GetDefaultFolder(olFolderInbox)
olFld.Items.sort "Received", False
Set olMail = olFld.Items.GetFirst
SendCashflowReply olMail
Set olMail = Nothing
Set olFld = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
Private Sub SendCashflowReply(ByVal oMail As Outlook.mailItem)
Dim myMail As Outlook.mailItem
If InStr(1, LCase(oMail.Subject), "send cashflow") > 0 Then
Set myMail = oMail.reply
myMail.To = "Email to be sent to Here"
myMail.Subject = "Cashflow"
myMail.Body = "Please find attached Cashflow"
myMail.Display
MsgBox ("Reply has generated."), , "Completed"
Else
Debug.Print "No match: LCase(oMail.Subject) is " & LCase(oMail.Subject) 'Remove later if you get this working
End If
End Sub
But this does nothing, when an email with the specific subject is recieved, no email is created.
Can anyone help me please.
Thanks