-1

So I've got some basics down, but I can't figure out how to do some things like create a task or move the email. I created a script to update the subject manually, but that isn't helping me in my end goal.

Prob not the best way to do this, but it works. Basically I need to make this fire off when a new email is received, edit the subject like below, move it to a sub folder.... let's call it 1, and create a 2 day task to complete.

Sub ChangeSubjects()
Dim Item As Outlook.MailItem
Dim oInspector As Inspector
Dim oExplorer As Explorer
Dim oCt As Integer
Dim cCt As Integer
Dim iSubject As String
Set oInspector = Application.ActiveInspector
Set oExplorer = Application.ActiveExplorer
cCt = 1
oCt = oExplorer.Selection.Count

For Each Item In Application.ActiveExplorer.Selection
    If oInspector Is Nothing Then
       Set Item = Application.ActiveExplorer.Selection.Item(cCt)
       Item.Display
       Set oInspector = Application.ActiveInspector  'Reassign oInpsector and Item again
       Set Item = oInspector.currentItem
    Else
        Set Item = oInspector.currentItem
    End If

    If Left(Item.Subject, 27) = "Look for this string" Then Item.Subject = Mid(Item.Subject, 29)
        Item.Close (2)
    Set oInspector = Application.ActiveInspector
    cCt = cCt + 1
Next
End Sub
1
  • 1
    The site is set up so you would ask four separate questions, if they had not been answered before. Parts are separated so future searchers, like you after this comment, can find answers and put them together as needed. In this way as well if there were a separate solution to each part you could, if you chose to do so, accept four answers, which you cannot do in this broad post. Commented Sep 23, 2017 at 9:36

1 Answer 1

2

Here's how to move an email:

Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object

Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("Test")
Set myItem = myItems.Find("[Subject] = '[Subject Name]'")
While TypeName(myItem) <> "Nothing"
   myItem.Move myDestFolder
   Set myItem = myItems.FindNext
Wend
End Sub

You can also have Set myItem = myItems.Find("[Subject] = '[Subject Name]'") equal Set myItem = myItems.Find("[SenderName] = '[Sender Name]'"), but be warned that it will find all the emails with the Sender or Subject name. The code below will change the subject name of the email(s).

Sub ChangeSubject()
Dim obj As Object
Dim msg As Outlook.MailItem
Dim subject As String
Set obj = ActiveExplorer.Selection
subject = "Picture"
If Not obj Is Nothing Then
    For i = 1 To obj.Count
    Set msg = obj.Item(i)
    With msg
        .subject = subject
    End With
    Next i
ElseIf Not msg Is Nothing Then
    msg.subject = subject
End If
End Sub

Here's a link to an article about creating tasks https://www.devhut.net/2010/09/03/vba-create-an-outlook-task/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.