1

I have 2 columns in Excel. The first column is the subject of the email and the second column is the folder where emails with the specific subject has to move to. For example the two columns:

Subject Folder
 A        1
 B        2
 C        3

So when I get a email with the subject "A" then it has to move to folder 1. So my question is how can I make a code to search in the excel sheet to which folder the mail has to move ( I only need this part of the code).

I can't fount anything at the internet about it.

0m3r
  • 12,286
  • 15
  • 35
  • 71
user5543269
  • 137
  • 2
  • 2
  • 8
  • All you want to do is read the contents of a cell? Do you also need the code to connect to the email server and move the emails? What have you tried? First thing to do is record a macro and take a look at the VBA code that ir generates – Nick.Mc Dec 13 '16 at 12:51
  • When I get a email with a specific subject I want that vba looks in my Excel sheet in which specific folder he has to move a mail with that subject. – user5543269 Dec 13 '16 at 12:54
  • 2
    Have you considered just creating a rule in outlook? It's unlikely that you can get your email client to go look in a spreadsheet everytime a mail arrives – Nick.Mc Dec 13 '16 at 12:56

1 Answers1

0

See Example on How to move each emails from inbox to a sub-folder

Now to loop through 2 columns, see example below

Dim ItemSubject As String
Dim SubFldr As String

i = 2 '  i = Row 2

With Worksheets("Sheet1") ' Sheet Name
   Do Until IsEmpty(.Cells(i, 1))

   ItemSubject = .Cells(i, 1).Value '(i, 1) = (Row2,Column1) = A2 Value = Subject
   SubFldr = .Cells(i, 2).Value '(i, 2) = (Row 2, Column 2) = B2 Value = FolderName

        '// Loop through Inbox Items backwards
        For lngCount = Items.Count To 1 Step -1
            Set Item = Items.Item(lngCount)

                If Item.Subject = ItemSubject Then ' if Subject found then
                    Set SubFolder = Inbox.Folders(SubFldr) ' Set SubFolder
                    Item.Move SubFolder ' Move to SubFldr
                End If

            End If
        Next ' exit loop

        i = i + 1 '  = Row 2 + 1 = Row 3
   Loop ' now loop on Row 3
End With
Community
  • 1
  • 1
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • @user5543269 https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 0m3r Mar 07 '17 at 05:52