Solved

Email  Subject

Posted on 2006-11-19
17
360 Views
Last Modified: 2008-02-07
Hi

How to go into a particular sub folder and then in the subject search for a particular word "GameNumber= 455543 and capture the number after = i.e 455543.

I am giving the subfolder but it is going to the default folder only.

Option Explicit
Dim oMAPI As Outlook.NameSpace
Dim oParentFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.MAPIFolder
Dim oFolder As Outlook.MAPIFolder
Private Sub Command1_Click()
   
Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
Set oParentFolder = oMAPI.Folders("Mailbox Name")
Set objFolder = OutlookFolderNames(oParentFolder, "Inbox\subfoldername")

End Sub


Public Function OutlookFolderNames(objFolder As Outlook.MAPIFolder, strFolderName As String) As Object
'*********************************************************
;
search for the  GameNumber= 455543  and take the 455543  into a variable
;
End function
0
Comment
Question by:Joserip
  • 10
  • 7
17 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Dim oMAPI As Outlook.NameSpace
Dim oParentFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.MAPIFolder
Dim oFolder As Outlook.MAPIFolder

Private Sub Command1_Click()
    Dim fldr As Outlook.MAPIFolder
    Dim itm As MailItem
   
    Dim strGameNumber As String
    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oParentFolder = oMAPI.Folders("Personal Folders")
    Set objFolder = oParentFolder.Folders("Inbox")
    For Each itm In objFolder.Items
        If Left$(itm.Subject, 11) = "GameNumber=" Then
            strGameNumber = Split(itm.Subject, "=")(1)
            Exit For
        End If
    Next itm
    MsgBox "Game " & strGameNumber & " found"
End Sub
0
 

Author Comment

by:Joserip
Comment Utility
Thanks !  Sorry I was not clear.

it is going to the Mail box I want and to the Inbox but I want to into a subfolder or the sub sub folder with in the Inbox.

And the subject will be having a message  say like the below. In such messages there will be a GameNumber= 4545454 . I need the number after GameNumber= i.e 4545454

This number is the one which you need to play GameNumber= 4545454 . If you want you can add prefix number
when you play. If you play with a sufix number then add 0 at the end.HHHHHH  HHHHH  hyhHH  hhhghg hfh  something
like this.

Each message will be different but will have the GameNumber =4545454. Having the GameNumber =4545454 is not compulsary so if a subject does not have it we need not get any number from that email.

As I am having 3 mail boxes
MailBoxA
      Inbox
      Sent Item
      OutBox
MailBoxB
      Inbox
          SubFolder1
          SubFolder2
               SubSubFolder2
          SubFolder3
      Sent Item
      OutBox

MailBoxC
      Inbox
      Sent Item
      OutBox
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Are you asking to search through all folders and subfolders?
Do you want to look for a particular Gamenumber subject line, the first one found or all of them?
If you need to find more than one, are you expecting to collect them into a string array of Subject lines?
0
 

Author Comment

by:Joserip
Comment Utility
Thank you very much!

Not all say I want to go to  SubFolder1 and then search in the body of the message . Once I detect and get the number I can come out of the loop but he GameNumber= 4545454  will be some where in between the body of the message . I need to search only for any given day . say when I give today date then it will search for email only for today. if I give yesterdays date then it need to search only yesterdays date. The similar logic we did for one week email received date info. Now it is on any particular date body message search

Say I have 10 emails for today then I will search for all the 10 in the body message for GameNumber= 4545454   and if GameNumber= if found I pick 4545454 .. If I don't get  GameNumber=  then I go to the next email on that day.

MailBoxB
      Inbox
          SubFolder1
                  email  Subject   Body ( in this body the GameNumber= 4545454   will be there and if it is there I need to
                                                       pick the number)

0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
If your problem is finding mail items in folders and all sub folders then you need to use recursion.

I cannot tell you how to search in other mailboxes. There doesn't seem to be a mailbox object in the Outlook Object model.

So this code show how to recurse from Personal Folders downwards. I look at the subject line, but you could look in the the body with itm.Body

Option Explicit
Public gFolder As Outlook.MAPIFolder
Public gItem As MailItem
Private Sub Command1_Click()
Dim oMAPI As Outlook.NameSpace
Dim oFolder As Outlook.MAPIFolder
    Set gItem = Nothing
    Dim strGameNumber As String
    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oFolder = oMAPI.Folders("Personal Folders")
    LookInFolder oFolder
    If Not gItem Is Nothing Then
        MsgBox gItem.SenderName & ", " & strGameNumber & ", " & gFolder.FolderPath
    Else
        MsgBox "No games found"
    End If
End Sub


Function LookInFolder(Infldr As Outlook.MAPIFolder) As Boolean
    Dim fldr As Outlook.MAPIFolder
    Dim itm As Object 'MailItem
    Debug.Print Infldr.FolderPath
    For Each itm In Infldr.Items
        Debug.Print TypeName(itm)
        If TypeName(itm) = "MailItem" Then
            If Left$(itm.Subject, 11) = "GameNumber=" Then
            'If InStr(itm.Body, "GameNumber=") then
                Set gFolder = Infldr
                Set gItem = itm
                LookInFolder = True
                Exit Function
            End If
        End If
    Next itm
    For Each fldr In Infldr.Folders
        If LookInFolder(fldr) Then
            LookInFolder = True
            Exit Function
        End If
    Next fldr
End Function
0
 

Author Comment

by:Joserip
Comment Utility
Thank you so much ! I add all the point I have.

This  going to search all the folders . I need to directly go into 2 or 3  subfolder only direclty . I can pass the 2 or 3 folder from a config file or so . Say I want to go to  SubFolder1 ,  SubFolder2 in Inbox and then go to SentItems  SubFolder3. I want to give these 3 in a XML  file.  I want  to also  give the search for value in the xml/config file.

So in XML file I need read these into the code
                               MailBoxB\ Inbox\SubFolder1\     StipsNumber= 34353
                               MailBoxB\ Inbox\SubFolder2\     GameNumber= 7676767

                              MailBoxB\ Sent Items\SubFolder3\     Jackypot= 7676767

I don't want to search all the folders.
I went into to particular  MailBoxB  by setting Set oFolder = oMAPI.Folders("MailBoxB"). Now I need to directly go to SubFolder1 in Inbox directly and search the subject for which is inbetween many sentences GameNumber=

MailBoxB
      Inbox
          SubFolder1 ( With in this subject I want to search for a StipsNumber= 34353)
          SubFolder2  ( With in this subject I want to search for a GameNumber= 7676767)
          SubFolder4 ( I don't want to go to all folders which are there)
          SubFolder5  ( I don't want to go to all folders which are there)      
      SentItems
            SubFolder3  ( With in this subject I want to search for a Jackypot= 7676767)
             SubFolder6   ( I don't want to go to all folders which are there)


0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
This shows how to do it for one of your folders.

It still isn't clear whether you want to search in the Subject or the Body text. This time the code looks in the Body, but I suppose that you can change it back to Subject if you want.

You now mention XML. I don't know where that fits in to your requirements, but I myself have little experience of XML, so I hope you aren't asking for that.

Private Sub Command2_Click()
    Dim oMAPI As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim itm As Object
   
    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oFolder = oMAPI.Folders("MailBoxB").Folders("Inbox").Folders("SubFolder1")
    For Each itm In oFolder.Items
        If TypeName(itm) = "MailItem" Then
            If InStr(itm.Body, "StipsNumber= 34353") > 0 Then
                Exit For
            End If
        End If
    Next itm
   
    If Not itm Is Nothing Then
        MsgBox "StipsNumber= 34353 found in email from " & itm.SenderName & ", Subject: " & itm.Subject
    Else
        MsgBox "Not found"
    End If
End Sub
0
 

Author Comment

by:Joserip
Comment Utility
Thank you so much ! I will try it on Monday and get back to you. Thanks once again!!
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Joserip
Comment Utility
The code is going into the folder I want to go but not searching as I am getting not found when some email body has the strip number.  I also want only to check for one day only at a time.

some emails will have Strip number and some won't have . The one which have will be having it this way
StipsNumber= XXXXX.

So if  StipsNumber=  exists I need the XXXXX.


 From the earlier code which asked the user for which week I need this to ask for which day from today . Say if it is today then if I enter 0 then only todays email (29 th Nov) it should check and if I have 1 then yesterdays email (28th Nov) should be checked and if I give 2 then only day before yesterdays (Nov 27) emails should be checked. So I only want to check only one day at a time .

Here are some email sample subjects

Eamil-A Subject with no Strip number

This is a game with few tries
guess a number basing on the StipsNumber.
No Stiper given to you. somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.

Email-B with strip number
This is a game with few tries
guess a number basing on the StipsNumber.
The number you guess will be with in 10 number of this StipsNumber= XXXXX  somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.

Email-C with strip number
This is a game with few tries
guess a number basing on the StipsNumber.
The number you guess will be with in 10 number of this StipsNumber= YYYYY  somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
somemore info will be there.somemore info will be there.somemore info will be there.
0
 

Author Comment

by:Joserip
Comment Utility
For the emails with no strip number there will not be any StipsNumber= . So in the body if we search for StipsNumber= and if exist then we need the number next to it StipsNumber= XXXXXX
0
 

Author Comment

by:Joserip
Comment Utility
If I can get the date check for an particular day or the present day (default)  and the below hard code out I will be done with this.

Ok when I tried it this way I can get the number . (Mid(itm.Body, pos + 12, 9)). Here the 9 is hard coded but I want to pick up numbers which may be of different lengths. So my end point for search should be space after YYYY 's   StipsNumber= YYYYY   instead of hard code 9.

StipsNumber= YYYYY  
StipsNumber= YYYYYYY
StipsNumber= Y
StipsNumber= YYY


If TypeName(itm) = "MailItem" Then
         If InStr(itm.Body, "StipsNumber=") > 0 Then
         pos = InStr(itm.Body, "StipsNumber=")
        val = (Mid(itm.Body, pos + 12, 9))
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
This addresses your difficulty in specifying the day sent. I have used a constant, but you have a way of getting it from the user

Private Sub Command2_Click()
    Dim oMAPI As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim itm As MailItem 'Object  - use object type if searched folders might have non mail items
    Dim strTargetSent As String
    Dim StripsNumber As Long
    Dim pos As Integer
   
    Const DaysOld = 2 'You r
   
    strTargetSent = Format$(DateAdd("d", -DaysOld, Now()), "yyyyy")

    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oFolder = oMAPI.Folders("MailBoxB").Folders("Inbox").Folders("SubFolder1")
     For Each itm In oFolder.Items
        If TypeName(itm) = "MailItem" Then
            If Format(itm.SentOn, "yyyyy") = strTargetSent Then
                 If InStr(itm.Body, "StipsNumber=") > 0 Then
                    pos = InStr(itm.Body, "StipsNumber=")
                    StripsNumber = Val(Mid(itm.Body, pos + 12, 9))
                    Exit For
                 End If
            End If
        End If
    Next itm
   
    If Not itm Is Nothing Then
        MsgBox "StipsNumber=" & StripsNumber & " found in email from " & itm.SenderName & ", Subject: " & itm.Subject
    Else
        MsgBox "Not found"
    End If
End Sub

0
 

Author Comment

by:Joserip
Comment Utility
Thanks!

I am getting the date as 2006333  for 01/12/2006 when formated into  strTargetSent

So the -2 is not going to 29 th Nov  email .


 strTargetSent = Format$(DateAdd("d", -DaysOld, Now()), "yyyyy")

Also when I am trying to pick up the number I am only getting 5 numbers  10687 . This  "0200010687" will in variable length so can we check for space after the number and then take the number which is in between ="0200010687" .

so if I hard code the value in  StripsNumber = Val(Mid(itm.Body, pos + 12, 9))
 then I will miss the exact number so can be check till the number ends and then pick up that number?


Eg.
1st msg might have 10 number and some migth have only 2 numbers.
adfdfdf  fsfasf       StipsNumber="0200010687"   dfsdfsdsdasd


2nd msg

adfdfdf  fsfasf       StipsNumber="02000999"   dfsdfsdsdasd

3rd msg

adfdfdf  fsfasf       StipsNumber="888"   dfsdfsdsdasd


0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Sorry. It has been hard to discern your precise requirements.

The date was formatted like that simply to do the comparison. I interpreted it that you wanted to find a message for a particular day and to get the number from the Stipsnumber=nnnnn string form the message body. In the example I have chosed -2 for the DaysOld value in the DateAdd, to show you how to look for an eMail two days old.

Yesterday, when you tried it, Nov 29th was two days earlier, so
Format(Cdate("29 Nov 2006"), "yyyyy") produces 2006333, i.e. day 333 in year 2006

For the number to be extracted, perhaps this technique is better

Private Sub Command3_Click()
    Dim oMAPI As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim itm As MailItem 'Object
    Dim strTargetSent As String
    Dim strParts() As String
    Dim strStripsNumber As String
   
    Const DaysOld = 2 'simulate number from user
   
    strTargetSent = Format$(DateAdd("d", -DaysOld, Now()), "yyyyy")
    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oFolder = oMAPI.Folders("MailBoxB").Folders("Inbox").Folders("SubFolder1")
    For Each itm In oFolder.Items
        If TypeName(itm) = "MailItem" Then
            If Format(itm.SentOn, "yyyyy") = strTargetSent Then
                strParts = Split(itm.Body, "StipsNumber=")
                If UBound(strParts) > 0 Then
                    strStripsNumber = Split(Trim$(strParts(1)), " ")(0)
                    Exit For
                 End If
            End If
        End If
    Next itm
   
    If Not itm Is Nothing Then
        MsgBox "StipsNumber=" & strStripsNumber & " found in email from " & itm.SenderName & ", Subject: " & itm.Subject
    Else
        MsgBox "Not found"
    End If
End Sub
0
 

Author Comment

by:Joserip
Comment Utility
Thanks ! it works fine ! I am finally done but the last bit .

Finally if we don't have access to the email box  MailBoxB then it should tell that access or mail box is not found and quit.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 85 total points
Comment Utility
I can't test that exactly (only 1 mailbox, and I have full access), but it might produce the error I am specially trapping here. If the error is somehing else, you could note the number and change it in the Case statement.

Private Sub Command3_Click()
    Dim oMAPI As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim itm As MailItem 'Object
    Dim strTargetSent As String
    Dim strParts() As String
    Dim strStripsNumber As String
   
    Const DaysOld = 2 'simulate number from user

    On Error GoTo Command3_ClickError 'set up error handling

    strTargetSent = Format$(DateAdd("d", -DaysOld, Now()), "yyyyy")
    Set oMAPI = GetObject("", "Outlook.application").GetNamespace("MAPI")
    Set oFolder = oMAPI.Folders("MailBoxB").Folders("Inbox").Folders("SubFolder1")
    For Each itm In oFolder.Items
        If TypeName(itm) = "MailItem" Then
            Debug.Print Format(itm.SentOn, "yyyyy"), itm.SentOn
            If Format(itm.SentOn, "yyyyy") = strTargetSent Then
                strParts = Split(itm.Body, "StipsNumber=")
                If UBound(strParts) > 0 Then
                    strStripsNumber = Split(Trim$(strParts(1)), " ")(0)
                    Exit For
                 End If
            End If
            'If InStr(itm.Body, "Clubcard") > 0 Then
                'Exit For
            'End If
        End If
    Next itm
   
    If Not itm Is Nothing Then
        MsgBox "StipsNumber=" & strStripsNumber & " found in email from " & itm.SenderName & ", Subject: " & itm.Subject
    Else
        MsgBox "Not found"
    End If
Command3_ClickExit:
    Exit Sub
Command3_ClickError:
    Select Case Err.Number
        Case -2147221233
            MsgBox "Folder or Mailbox not found"
        Case Else
            MsgBox "Unexpected error: " & Err.Number & " (" & Hex$(Err.Number) & ")" & vbCrLf & Err.Description
    End Select
    Resume Command3_ClickExit:
End Sub
0
 

Author Comment

by:Joserip
Comment Utility
Thanks!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to the message 13 62
DO Loop not working 4 62
VBA loop through headers using value 3 48
VB 6.0 printer how to align 6 49
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now