Results 1 to 22 of 22

Thread: Outlook open_mail()

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Outlook open_mail()

    Is there such a thing.

    I spent a whole day investigating...

    The outlook object model sucks!! explorer, inspector..
    can`t they make it easier!!!

    Seahag

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    You probably want .DISPLAY as in:

    The call from Visual Basic back into Outlook 2000 can only be accomplished once a reference to Outlook has been added to the VB project (Project menu, References option, “Microsoft Outlook 9.0 Object Library” selected).

    Talking to Outlook requires access to the current Explorer (the main Window in Outlook), as well as to the selection within this window. The piece of code below shows the number of items in the selected folder, does a Save As on each of the selected items using the subject as the file name to be used, and then modifies the name of the item in Outlook.

    VB Code:
    1. Dim myOlSel As Outlook.Selection
    2. Dim olExp, olCurrentFolder
    3. Dim x As Integer
    4.     Set olExp = Outlook.ActiveExplorer
    5.     Set olCurrentFolder = olExp.CurrentFolder
    6.     MsgBox “Folder “ & olCurrentFolder & _
    7.         “ has “ & olCurrentFolder.Items & “ items in it”
    8.     Set myOlSel = olExp.Selection
    9. ‘ Get the subject, SaveAs the message,
    10. ‘  modify the subject in the message, and save the changes
    11.    For x = 1 To myOlSel.Count
    12.         thisSubject = CheckSubject(myOlSel.Item(x).Subject)
    13.         myOlSel.Item(x).SaveAs thisSubject.MSG, olMSG
    14.         MyOLSel.Item(x).Subject = “FILED – “ & thisSubject
    15.         MyOLSel.Item(x).Display
    16.         MyOLSel.Item(x).Save
    17.     Next x

    Or:
    VB Code:
    1. Dim ol as Outlook.Application
    2. Dim olMail as Outlook.MailItem
    3.     Set ol = CreateObject(“Outlook.Application”)
    4.     Set olMail = ol.CreateItem(0)
    5.     olMail.Recipients.Add [email][email protected][/email]
    6.     olMail.Subject = “Testing a send of a message”
    7.     olMail.Body = “Some longer text for the body”
    8.                 olMail.Display
    9.     Set olMail = Nothing
    10.     ol.Quit
    11.     Set ol = Nothing

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Ya.. that looks about right.

    What i am trying to do (no VB required because i am using the vba editor) is On new mail, determain who mail item is from, then if it has attachement of type myAttachemen.mld (my own.. its an expense report) then saves it to temp location, then I need to shell a utility program to run. Then it uses that attachemen to up dates the master database...

    I am still working on the method .... but i need to figure out and maybe find a way to trigger an update through the mail programs..


    Eekkk Seahag

  4. #4

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Just mucking around

    I have figured this much out...

    It look through the inbox for a specific subjet then opens it.
    problem is, it opens (i think) a new instance..


    VB Code:
    1. Dim myNameSpac
    2. Set myNameSpace = Me.GetNamespace("MAPI")
    3.     Dim myFolder
    4. Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
    5. 'myFolder.Display
    6.   Dim myItem
    7. Set myItem = myFolder.Items("chris")
    8. myItem.Display

    Seahag

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    You probably don't want to .DISPLAY anything...

    VB Code:
    1. SUB LookThroughFolder (thisFolder)
    2.     for each myItem in thisFolder
    3.     Msgbox "Looking at: " & myItem & ". In: " & myItem.PARENT & " (" & thisFolder.COUNT & ")."
    4. '
    5. ' Step through all items in the Folder
    6. '
    7.         if myItem.CLASS = "43" then
    8.     for each Attachment in myItem.Attachments
    9. '
    10. ' For any mail item (class 43) with an attachment, check its name
    11. '
    12.         if Attachment = "myAttachemen.mld" then
    13.         msgbox "FOUND IN: " & myItem.PARENT & " (IN: " & myitem.PARENT.PARENT & ")" & ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment
    14.         end if
    15.     next
    16.         end if
    17.     next
    18. END SUB
    Instead of the second message box (the first message box you will want to comment out as soon as possible!), you can do things with the attachment (like copy it, Save Attachments etc.).

  6. #6

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    WOW THATS GOLD..
    I will look at that..

    couple of questions.....

    1. that looks like it works.. Why? (lol) i spend a whole day in help
    and i did not run accross that type of syntax..

    2. What will ThisFolder look like.. olInbox ?


    seahag

  7. #7

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    VB Code:
    1. Dim myNameSpac
    2. Set myNameSpace = Me.GetNamespace("MAPI")
    3.     Dim myfolder
    4. Set myfolder = myNameSpace.GetDefaultFolder(olFolderInbox)
    5. Call LookThroughFolder(myfolder) 'MEE CALLING SUB
    6.  
    7.  
    8.  
    9.  
    10.  
    11. Sub LookThroughFolder(thisFolder)
    12.  
    13.     For Each myItem In thisFolder'I GET ERROR HERE
    14. 'RUNTIME ERROR 438 OBJECT DOESN`T SUPPORT THIS METHOD.
    15.     MsgBox "Looking at: " & myItem & ". In: " & myItem.Parent & " (" & thisFolder.Count & ")."
    16. '
    17. ' Step through all items in the Folder
    18. '
    19.         If myItem.Class = "43" Then
    20.     For Each Attachment In myItem.Attachments
    21. '
    22. ' For any mail item (class 43) with an attachment, check its name
    23. '
    24.         If Attachment = "myAttachemen.mld" Then
    25.         MsgBox "FOUND IN: " & myItem.Parent & " (IN: " & myItem.Parent.Parent & ")" & ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment
    26.         End If
    27.     Next
    28.         End If
    29.     Next
    30. End Sub


    I get that error... whats the syntax for calling that??

    Seahag

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    The code that I used to call that Sub-routine was actually straight .VBS script. It looked like this:

    VB Code:
    1. '
    2. ' Now for the actual code, that steps through your mailbox and your .PSTs...
    3. '
    4. set OL = GetObject(,"OUTLOOK.APPLICATION")
    5. set MAPI = OL.GetNamespace("MAPI")
    6. '
    7. ' Step through all stores in your profile
    8. '
    9. for each Store in MAPI.FOLDERS
    10.     rtn = msgbox ("Look in Store: " & Store & "?", 1)
    11.     if rtn <> 2 then
    12.     for each Folder in Store.FOLDERS
    13. '
    14. ' Step through all folders in that Store
    15. '
    16.         LookThroughFolder (Folder.ITEMS)
    17.         for each subFolder in Folder.FOLDERS
    18. '
    19. ' If we have any sub-folders, look through those.
    20. '
    21.         LookThroughFolder (SubFolder.ITEMS)
    22.         For each subsubFolder in subFolder.FOLDERS
    23. '
    24. ' If we have any sub-folders of those sub-folders, look through those.
    25. '
    26.             LookThroughFolder (SubSubFolder.ITEMS)
    27.         next
    28.         next
    29.     next
    30.     end if
    31. next
    So you need to call the routine, not with the name of the folder, but with a collection of the .ITEMS in the folder.

    And, yes, you are right, there is nothing like this in the help or examples!

  9. #9

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Cool Thanks..

    BUt still some questions.
    How on earth did you learn that..

    The code you posted looked through alot.
    actually too much.. The good ctrl alt del..


    Why wont it look in the inbox.. or does it..

    What are the levels of folders? looking in help dont help...

    Seahag

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Its supposed to look through every one of your Stores, and every one of your folders. I am sure you could get the default store (where your Inbox is positioned) and the default Inbox (where your mail is delivered) quite easily.

    MAPI is the connection. Within this are Stores (mailbox, other shared user mailboxes, .PST files etc.). Within each Store are Folders. Within each Folder are Items and other Folders. You need to get down to the correct level...

    Alternatively (if you are still at the testing stage, rather than the actual "right, lets do it"), you could use this to call the original routine:
    VB Code:
    1. for each Store in MAPI.FOLDERS
    2.         rtn = msgbox ("Look in Store: " & Store & "?", 1)
    3.         if rtn <> 2 then
    4.     for each folder in STORE.Folders
    5.             If Folder = "Inbox" then
    6.         msgBox "Looking through the: " & Folder
    7.         LookThroughFolder (Folder.ITEMS)
    8.             end if
    9.     next
    10.         end if
    11. next
    Don't forget to press "Cancel" on the stores you don't want to search through.

  11. #11
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92
    Hi guys,

    I am about to do a similar project, but I am using normal Visual Basic.

    Within my code, I am wanting it to monitor incoming mail as well (then check if it is the right mail and inform the user etc).

    Will this code do this also, as monitoring the email is fundamental to my proggy.

    Cheers!

    Paul.
    Paul

  12. #12
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Its not really a monitor function. It is more code that you can run once it is decided that you do want to process something in your mailbox.


    By the way, if you know that the user has selected the item in Outlook that you want to process, it can be MUCH simpler to do something like:
    VB Code:
    1. Dim myOlSel As Outlook.Selection
    2. Dim olExp, olCurrentFolder
    3. Dim x As Integer
    4.  
    5.     Set olExp = Outlook.ActiveExplorer
    6.     Set olCurrentFolder = olExp.CurrentFolder
    7.     MsgBox “Folder “ & olCurrentFolder & _
    8.         “ has “ & olCurrentFolder.Items & “ items in it”
    9.     Set myOlSel = olExp.Selection
    10. ‘ Get the subject
    11. ‘  modify the subject in the message, and save the changes
    12.    For x = 1 To myOlSel.Count
    13.         thisSubject = CheckSubject(myOlSel.Item(x).Subject)
    14.         MyOLSel.Item(x).Subject = “FILED – “ & thisSubject
    15.         MyOLSel.Item(x).Save
    16.     Next x

    Or you should be able to go straight to the Inbox with something like:
    VB Code:
    1. Dim oOutlook As New Outlook.Application
    2. Dim oNameSpace As NameSpace
    3. Dim myItems As Items
    4.  
    5. Set oNameSpace = oOutlook.GetNamespace("MAPI")
    6. Set myItems = oNameSpace.GetDefaultFolder (olFolderInbox).Items
    7. Call LookThroughFolder(myItems)

  13. #13

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    K.. this is becoming very clear.
    Thanks for your help JordanChris ..

    I am thinking about that last piece of code you posted.
    It`s exactly what i am looking for. But i dont thinki you can
    pass the Items to that function that accepts a folder collection.

    I can see what going on but i dont know how to fix it..
    How about some hints....

    Seahag

  14. #14
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    I am still using VB Script, rather than VB. So you will need to change the number "6" to the normal OLDefaultInbox string. Also you will need to change the search string (at the moment its looking for any attachments that start with a "C"). However, this code does work....

    VB Code:
    1. msgbox "Searching Outlook Inbox for certain attachments . . ."
    2.  
    3. '
    4. ' First the sub-routine that looks through a specified folder looking for mail
    5. '  items and their attachments
    6. '
    7. SUB LookThroughFolder (thisFolder)
    8.     for each myItem in thisFolder
    9. '
    10. ' Step through all items in the Folder
    11. '
    12.         if myItem.CLASS = "43" then
    13.         for each Attachment in myItem.Attachments
    14. '
    15. ' For any mail item (class 43) with an attachment, check its name
    16. '
    17.             if Attachment > "C" and Attachment < "D" then
    18.                 msgbox "FOUND IN: " & myItem.PARENT & " (IN: " & myitem.PARENT.PARENT & ")" &
    19.  
    20. ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment
    21.             end if
    22.         next
    23.         end if
    24.     next
    25. END SUB
    26.  
    27.  
    28. '
    29. ' Now for the actual code, that looks in your default Inbox
    30. '
    31. set OL = GetObject(,"OUTLOOK.APPLICATION")
    32. set MAPI = OL.GetNamespace("MAPI")
    33. Set myItems = MAPI.GetDefaultFolder(6).Items
    34. Msgbox "Looking at: " & myItems.Parent & " (" & myItems.COUNT & " items)."
    35. Call LookThroughFolder(myItems)
    36.  
    37. Msgbox "Ending the search . . ."

  15. #15

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Looks good.

    I am getting confuesed what the function is looking for.

    I am getting all sorts of errors..

    in.. SUB LookThroughFolder (thisFolder)
    for each myItem in thisFolder

    thisFolder is a mapi folder collection ???

    What is myitem? should i declare it?

    OUTLOOK SUCKS!~~!
    (if you dont know what your doing)

    Here is what i have.

    VB Code:
    1. Sub LookThroughFolder(thisFolder)
    2.     For Each myitem In thisFolder
    3.     'MsgBox "Looking at: " & myitem & ". In: " & myitem.Parent & " (" & thisFolder.Count & ")."
    4. '
    5. ' Step through all items in the Folder
    6. '
    7.         If myitem.Class = "43" Then
    8.                 For Each Attachment In myitem.Attachments
    9.                     '
    10.                     ' For any mail item (class 43) with an attachment, check its name
    11.                     '
    12.                    ' If Attachment = "myAttachemen.mld" Then
    13.                     MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment
    14.                     'End If
    15.                 Next
    16.         End If
    17.     Next
    18. End Sub
    19.  
    20. Sub mylittletest()
    21. Dim MAPI As NameSpace
    22. Set MAPI = ThisOutlookSession.Application.GetNamespace("MAPI")
    23. Dim myitms As Items
    24. Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items
    25. MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)."
    26.  
    27. LookThroughFolder (myitms.Parent)
    28.  
    29. End Sub


    I get a weired error when the Sub LookThroughFolder(thisFolder)
    starts..

    I sorry for being a pain in the a@@

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    just because i like to throw my 2 cents in about email automation questions... MS placed restrictions on using VB to send email when using outlook 2000 with service release 1a on it. I also heard that this "feature" is built in to office XP right outta the box..

    it is a REAL pain in the a$$

    to check out the details.. here is a link

    http://support.microsoft.com/default...;en-us;Q290500

  17. #17

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    MicroA$$

    I am not trying to automate through VB..
    Thats whats so frustrating..
    Help tell half the story..

    Where can i get a detailed object model?


    Seahag

  18. #18
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    OK. Here is your code, but I have used different names to try to indicate what each collection really is:

    VB Code:
    1. Sub LookThroughFolder(ItemsInFolder)
    2. '
    3. ' Step through all items in the Folder
    4. '
    5.     For Each myitem In ItemsInFolder
    6.         'MsgBox "Looking at: " & myitem & ". In: " & myitem.Parent & " (" & thisFolder.Count & ")."
    7.         If myitem.Class = "43" Then
    8.                 For Each Attachment In myitem.Attachments
    9.                     '
    10.                     ' For any mail item (class 43) with an attachment, check its name
    11.                     '
    12.                     MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment
    13.                 Next
    14.         End If
    15.     Next
    16. End Sub
    17.  
    18. Sub mylittletest()
    19. Dim MAPI As NameSpace
    20. Set MAPI = ThisOutlookSession.Application.GetNamespace("MAPI")
    21. Dim myitms As Items
    22. Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items
    23. MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)."
    24.  
    25. 'THIS NEXT LINE HAS CHANGED
    26. LookThroughFolder (myitms)
    27.  
    28. End Sub
    The routine is expecting a collection of items in the folder; NOT the folder name itself. So instead of calling it with myItms.Parent, just use myItms.

    If you want to change the routine to accept a folder name, then the first bit of the routine will need to include the statement (approximately):
    ItemsInFolder = FolderName.Items

  19. #19

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    oops..

    myitms.parents was my last attempt before asking..

    I tryed that..


    this is what i get..

    Seahag
    Attached Images Attached Images  

  20. #20
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    OK. I have now switched from VBScript to standard VB, and YOU ARE RIGHT. You do get an error!

    How about this:

    VB Code:
    1. Sub LookThroughFolder(inFolder)
    2.     Set ItemsInThisFolder = inFolder.Items
    3.     For Each myitem In ItemsInThisFolder
    4. '
    5. ' Step through all items in the Folder
    6. '
    7.         If myitem.Class = "43" Then
    8.                 For Each Attachment In myitem.Attachments
    9.                     '
    10.                     ' For any mail item (class 43) with an attachment, check its name
    11.                     '
    12.                    ' If Attachment = "myAttachemen.mld" Then
    13.                     MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment
    14.                     'End If
    15.                 Next
    16.         End If
    17.     Next
    18. End Sub
    19.  
    20. Set OL = GetObject(, "OUTLOOK.APPLICATION")
    21. Set MAPI = OL.GetNamespace("MAPI")
    22. Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items
    23. MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)."
    24.  
    25. LookThroughFolder (myitms.Parent)

    Of course, the "Set myItms" line doesn't really need the ".Items" on the end; and then the call to the routine wont need the ".Parent".

  21. #21

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Ahhhhh..

    YOur the best.. thnks..

    How did you learn the object model?

    (i am guessing not over night)

    Seahag


  22. #22

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    OK.. 1 last question

    I have taken your code (the last set that works)
    An i am trying to declare all the variables to
    get a better understanding of whats going on.
    It appears that i will never..

    Looking throuhg the examples in help, and your help here,
    I have determained that maybe you dont have to declare
    them..

    Is there a reason for this.. if not, could you rewrite that code with all the variable and object variable declared..
    I think that will help me understand..
    (would this be considered intence oo programing.?)


    Thanks in advance.

    Seahag

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width