I'm trying to move emails from Inprogress Folder to Completed Folder, based on the word placed in the body of the mail when replied to.
if the reply mail has the word Completed then the reply mails have to be moved to Completed Folder.
if the reply mail has the word Cancelled then the reply mails have to be moved to Cancelled Folder.
I tried the below code, but it throws
Compile Error: Excepted expression
Error is because of this two-line.
If InStr(1,olMail.Body, "Completed") And
If InStr(1, Item.Subject, olObj.Subject) > 0
Full Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim olNameSpace As NameSpace
Set olNameSpace = GetNamespace("MAPI")
Dim olDestFolder As folder
Set olDestFolder = olNameSpace.Folders("xxx#xxx.com").Folders("Completed")
Dim olLookUpFolder As folder
Set olLookUpFolder = olNameSpace.Folders("xxx#xxx.com").Folders("InProgress")
' olMail is a Class. Avoid as a variable name
'Dim olMail As MailItem
Dim olObj As Object ' Outlook items are not necessarily mailitems
For Each olObj In olLookUpFolder.Items 'loop through Tickets folder to find original mail
If olObj.Class = olMail Then
If InStr(1,olMail.Body, "Completed") And
If InStr(1, Item.Subject, olObj.Subject) > 0 Then 'look for olObj.Subject in Item.Subject
olObj.Move olDestFolder ' move to InProgress folder
Exit For
End If
End If
Next
End Sub
You wanted to write
If InStr(1,olMail.Body, "Completed") > 0 And InStr(1, Item.Subject, olObj.Subject) > 0 Then 'look for olObj.Subject in Item.Subject
olObj.Move olDestFolder ' move to InProgress folder
Exit For
End If
Related
I've New Ticket folder, once replied from the New Ticket folder mails have to be moved to Completed folder.
Getting error message
This method can't be used in an inline response mail item.
at olMail.Move olDestFolder ' move to InProgress folder.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim olNameSpace As Outlook.NameSpace
Set olNameSpace = GetNamespace("MAPI")
Dim olDestFolder As Outlook.Folder
Set olDestFolder = olNameSpace.Folders("xxx#xxx.com").Folders("In Progress")
Dim olLookUpFolder As Outlook.Folder
Set olLookUpFolder = olNameSpace.Folders("xxx#xxx.com").Folders("Tickets")
Dim olMail As Outlook.MailItem
For Each olMail In olLookUpFolder.Items 'loop through Tickets folder to find original mail
If InStr(1, olMail.Subject, strTicket) > 0 Then 'look for unique ticket Id
olMail.Move olDestFolder ' move to InProgress folder
Exit For
End If
Next
End Sub
From the comment "strTicket- to read the subject line and see if the particular subject line as a response".
You need strTicket = "text based on Item.Subject" for
If InStr(1, olMail.Subject, strTicket) > 0
For example:
Item.Subject "Re: Ticket #123456"
strTicket would be 123456.
If InStr(1, olMail.Subject, strTicket) > 0 Then 'look for unique ticket Id in olLookUpFolder.Items
There is no need to extract the unique ticket Id. olMail.Subject is unique and will be in Item.Subject.
If Item.Subject is "Re: Ticket #123456"
then olMail.Subject is "Ticket #123456"
Reverse the order of the search terms in InStr.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim olNameSpace As NameSpace
Set olNameSpace = GetNamespace("MAPI")
Dim olDestFolder As folder
Set olDestFolder = olNameSpace.Folders("xxx#xxx.com").Folders("In Progress")
Dim olLookUpFolder As folder
Set olLookUpFolder = olNameSpace.Folders("xxx#xxx.com").Folders("Tickets")
' olMail is a Class. Avoid as a variable name
'Dim olMail As MailItem
Dim olObj As Object ' Outlook items are not necessarily mailitems
For Each olObj In olLookUpFolder.Items 'loop through Tickets folder to find original mail
If olObj.Class = olMail Then
If InStr(1, Item.Subject, olObj.Subject) > 0 Then 'look for olObj.Subject in Item.Subject
olObj.Move olDestFolder ' move to InProgress folder
Exit For
End If
End If
Next
End Sub
If the preview pane is on then
Error: "This method can't be used with an inline response mail item."
This code restarted Outlook and disabled VBA the first time. Subsequently it only restarted Outlook. If you get similar results you may decide to turn off the preview pane yourself so the preview pane check is not invoked.
If InStr(1, Item.Subject, olObj.Subject) > 0 Then 'look for olObj.Subject in Item.Subject
If ActiveExplorer.IsPaneVisible(olPreview) = True Then
' Hide Preview Pane
' https://learn.microsoft.com/en-us/office/vba/api/outlook.explorer.ispanevisible
ActiveExplorer.ShowPane olPreview, False
olObj.Move olDestFolder ' move to InProgress folder
' Show Preview Pane
ActiveExplorer.ShowPane olPreview, True
Else
olObj.Move olDestFolder ' move to InProgress folder
End If
Exit For
End If
More information about the error related to inline response.
All the reply (RE:) and the forward (FWD:) mails received in a shared inbox has to automatically move to an "Ongoing folder".
This code is not working.
Private Sub Application_NewMail()
Dim olNameSpace As NameSpace
Set olNameSpace = GetNamespace("MAPI")
Dim olDestFolder As Folder
Set olDestFolder = olNameSpace.Folders("xxx#gmail.com").Folders("Ongoing")
Dim olLookUpFolder As Folder
Set olLookUpFolder = olNameSpace.Folders("xxx#gmail.com").Folders("Inbox")
' olMail is a Class. Avoid as a variable name
'Dim olMail As MailItem
Dim olObj As Object ' Outlook items are not necessarily mailitems
For Each olObj In olLookUpFolder.Items 'loop through Tickets folder to find original mail
If olObj.Class = olMail Then
Set objMail = objItem
v = objMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003")
If (v = 102) Or (v = 104) Then
olObj.Move olDestFolder ' move to InProgress folder
Exit For
End If
End If
Next
End Sub
Firstly, do not use "for each" against a collection that you are modifying (by calling MailItem.Move). Use a down loop.
Secondly, never loop through all items in a folder (you wouldn't use a SELECT query in SQL without a WHERE clause, would you?) - use Items.Find/FindNext or Items.Restrict:
set query = "#SQL=(""http://schemas.microsoft.com/mapi/proptag/0x10810003"" = 102) or (""http://schemas.microsoft.com/mapi/proptag/0x10810003"" = 104)"
set olItems = olLookUpFolder.Items.Restrict(query)
for i = olItems.Count to 1 step -1
set olObj = olItems.Item(i)
...
I want to autosave attachments from a sub-folder in Outlook.
I need to only save those in mail with a particular subject line (inStr) and the time received, like today.
I have code, but don't know how to add the conditionals, the subject and time received; and I would like to rename the Excel attachment when I save it.
Option Explicit
Const folderPath = "C:\Documents\nike\My Documents\emailTest\"
Sub CompanyChange()
On Error Resume Next
Dim ns As NameSpace
Set ns = GetNamespace("MAPI")
Dim Inbox As MAPIFolder
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Dim searchFolder As String
searchFolder = InputBox("What is your subfolder name?")
Dim subFolder As MAPIFolder
Dim Item As Object
Dim Attach As Attachment
Dim FileName As String
Dim i As Integer
If searchFolder <> "inbox" Then
Set subFolder = Inbox.Folders(searchFolder)
i = 0
If subFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
For Each Item In subFolder.Items
For Each Attach In Item.Attachments
Attach.SaveAsFile (folderPath & Attach.FileName)
i = i + 1
Next Attach
Next Item
Else
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", _
vbInformation, "Nothing Found"
Exit Sub
End If
On Error Resume Next
For Each Item In Inbox.Items
For Each Attach In Item.Attachments
FileName = folderPath & Attach.FileName
Attach.SaveAsFile FileName
i = i + 1
Next Attach
Next Item
End If
End Sub
Like this:
Place your if condition before the loop that is saving your files.
For Each Item In subFolder.Items
If Item.Subject = "Subject you want to Macth with" Then 'Condition
For Each Attach In Item.Attachments
Attach.SaveAsFile (folderPath & Attach.FileName)
i = i + 1
Next Attach
End If
Next Item
I'm trying to write a macro to move email if the attachment filename matches a string (for example, "asdfqwerty"). The email would move from my Inbox to the folder "Test" under my Inbox.
Using Redemption is not an option unfortunately.
Any help is appreciated!
Edit
Here is my updated code based on the tips from Dmitry. I am now getting a 'Type mismatch' error on the very last Next and am not sure why:
Sub SaveOlAttachments()
Dim olFolder As MAPIFolder
Dim olFolder2 As MAPIFolder
Dim msg As mailItem
Set olFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olFolder2 = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test")
For Each msg In olFolder.Items
If msg.Class = 43 Then
If msg.Attachments.Count > 0 Then
If Left$(msg.Attachments(1).FileName, 10) = "asdfqwerty" Then
msg.Move (oldFolder2)
End If
End If
End If
Next
End Sub
Did you try to run that code? It will error on the msg.Attachments > 0 line. You need msg.Attachments.Count > 0.
The next line also won't run - you need to loop through all attachments in the msg.Attachments collection:
for each attach in msg.Attachments
if InStr(attach.FileName, "asdfqwerty") Then
msg.Move (olFolder2)
Exit for
End If
next
Before posting, please at least try to apply some effort to make sure your code compiles and maybe even runs. Do not expect other people to do that for you.
The email with the attachment comes in and a Rule executes the following VBA script:
Sub Test()
'Declaration
Dim myItems, myItem, myAttachments, myAttachment As Object
Dim myOrt As String
Dim myFin As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
'Ask for destination folder
myOrt = "W:\"
On Error Resume Next
'work on selected items
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
'for all items do...
For Each myItem In myOlSel
'point on attachments
Set myAttachments = myItem.Attachments
'if there are some...
If myAttachments.Count > 0 Then
'for all attachments do...
For i = 1 To myAttachments.Count
'Ask for destination folder
myFin = InputBox("Please type a filename below:", "Saving
recording...", "")
'save them to destination
myAttachments(i).SaveAsFile myOrt & _
myFin
Next i
End If
Next
End Sub
This question already has answers here:
For Each loop: Some items get skipped when looping through Outlook mailbox to delete items
(2 answers)
Closed 7 years ago.
I seem to be getting issues with moving emails from inbox to a sub-folder of inbox. I always thought my code was working until today. I noticed it's only moving half of the emails. I do not need a "move all" code, I have a purpose for this but I just need to move each emails and not all at once (I needed to check each emails). Please take a look at my code below. myNamespace.Folders.Item(1).Folders.Item(2) is my main Inbox.
Sub MoveEachInboxItems()
Dim myNamespace As Outlook.NameSpace
Set myNamespace = Application.GetNamespace("MAPI")
For Each Item In myNamespace.Folders.Item(1).Folders.Item(2).Items
Dim oMail As Outlook.MailItem: Set oMail = Item
Item.UnRead = True
Item.move myNamespace.Folders.Item(1).Folders.Item(2).Folders("Other Emails")
Next
End Sub
here is good link
Moves Outlook Mail items to a Sub folder by Email address
Option Explicit
Public Sub Move_Items()
' // Declare your Variables
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim olNs As Outlook.NameSpace
Dim Item As Object
Dim Items As Outlook.Items
Dim lngCount As Long
On Error GoTo MsgErr
' Set Inbox Reference
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items
' // Loop through the Items in the folder backwards
For lngCount = Items.Count To 1 Step -1
Set Item = Items(lngCount)
If Item.Class = olMail Then
Select Case Item.SenderEmailAddress
' // Email_One
Case "Email_One#email.com"
' // Set SubFolder of Inbox
Set SubFolder = Inbox.Folders("Folder One")
Set Item = Items.Find("[SenderEmailAddress] = 'Email_One#email.com'")
If TypeName(Item) <> "Nothing" Then
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to sub Folder
Item.Move SubFolder
End If
' // Email_Two
Case "Email_Two#email.com"
' // Set SubFolder of Inbox
Set SubFolder = Inbox.Folders("Folder Two")
Set Item = Items.Find("[SenderEmailAddress] = 'Email_Two#email.com'")
If TypeName(Item) <> "Nothing" Then
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to sub Folder
Item.Move SubFolder
End If
End Select
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set olNs = Nothing
Set Item = Nothing
Set Items = Nothing
Exit Sub
'// Error information
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub
Or to move all Mail items Inbox to sub folder
Option Explicit
Public Sub Move_Items()
' // Declare your Variables
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim olNs As Outlook.NameSpace
Dim Item As Object
Dim lngCount As Long
Dim Items As Outlook.Items
On Error GoTo MsgErr
' Set Inbox Reference
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items
' // Loop through the Items in the folder backwards
For lngCount = Items.Count To 1 Step -1
Set Item = Items(lngCount)
Debug.Print Item.Subject
If Item.Class = olMail Then
' // Set SubFolder of Inbox
Set SubFolder = Inbox.Folders("Temp")
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to sub Folder
Item.Move SubFolder
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set olNs = Nothing
Set Item = Nothing
Exit Sub
'// Error information
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub