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
Printable View
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
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:
Dim myOlSel As Outlook.Selection Dim olExp, olCurrentFolder Dim x As Integer Set olExp = Outlook.ActiveExplorer Set olCurrentFolder = olExp.CurrentFolder MsgBox “Folder “ & olCurrentFolder & _ “ has “ & olCurrentFolder.Items & “ items in it” Set myOlSel = olExp.Selection ‘ Get the subject, SaveAs the message, ‘ modify the subject in the message, and save the changes For x = 1 To myOlSel.Count thisSubject = CheckSubject(myOlSel.Item(x).Subject) myOlSel.Item(x).SaveAs thisSubject.MSG, olMSG MyOLSel.Item(x).Subject = “FILED – “ & thisSubject MyOLSel.Item(x).Display MyOLSel.Item(x).Save Next x
Or:
VB Code:
Dim ol as Outlook.Application Dim olMail as Outlook.MailItem Set ol = CreateObject(“Outlook.Application”) Set olMail = ol.CreateItem(0) olMail.Recipients.Add [email][email protected][/email] olMail.Subject = “Testing a send of a message” olMail.Body = “Some longer text for the body” olMail.Display Set olMail = Nothing ol.Quit Set ol = Nothing
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
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:
Dim myNameSpac Set myNameSpace = Me.GetNamespace("MAPI") Dim myFolder Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox) 'myFolder.Display Dim myItem Set myItem = myFolder.Items("chris") myItem.Display
Seahag
You probably don't want to .DISPLAY anything...
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.).VB Code:
SUB LookThroughFolder (thisFolder) for each myItem in thisFolder Msgbox "Looking at: " & myItem & ". In: " & myItem.PARENT & " (" & thisFolder.COUNT & ")." ' ' Step through all items in the Folder ' if myItem.CLASS = "43" then for each Attachment in myItem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' if Attachment = "myAttachemen.mld" then msgbox "FOUND IN: " & myItem.PARENT & " (IN: " & myitem.PARENT.PARENT & ")" & ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment end if next end if next END SUB
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
VB Code:
Dim myNameSpac Set myNameSpace = Me.GetNamespace("MAPI") Dim myfolder Set myfolder = myNameSpace.GetDefaultFolder(olFolderInbox) Call LookThroughFolder(myfolder) 'MEE CALLING SUB Sub LookThroughFolder(thisFolder) For Each myItem In thisFolder'I GET ERROR HERE 'RUNTIME ERROR 438 OBJECT DOESN`T SUPPORT THIS METHOD. MsgBox "Looking at: " & myItem & ". In: " & myItem.Parent & " (" & thisFolder.Count & ")." ' ' Step through all items in the Folder ' If myItem.Class = "43" Then For Each Attachment In myItem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' If Attachment = "myAttachemen.mld" Then MsgBox "FOUND IN: " & myItem.Parent & " (IN: " & myItem.Parent.Parent & ")" & ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment End If Next End If Next End Sub
I get that error... whats the syntax for calling that??
Seahag
The code that I used to call that Sub-routine was actually straight .VBS script. It looked like this:
So you need to call the routine, not with the name of the folder, but with a collection of the .ITEMS in the folder.VB Code:
' ' Now for the actual code, that steps through your mailbox and your .PSTs... ' set OL = GetObject(,"OUTLOOK.APPLICATION") set MAPI = OL.GetNamespace("MAPI") ' ' Step through all stores in your profile ' for each Store in MAPI.FOLDERS rtn = msgbox ("Look in Store: " & Store & "?", 1) if rtn <> 2 then for each Folder in Store.FOLDERS ' ' Step through all folders in that Store ' LookThroughFolder (Folder.ITEMS) for each subFolder in Folder.FOLDERS ' ' If we have any sub-folders, look through those. ' LookThroughFolder (SubFolder.ITEMS) For each subsubFolder in subFolder.FOLDERS ' ' If we have any sub-folders of those sub-folders, look through those. ' LookThroughFolder (SubSubFolder.ITEMS) next next next end if next
And, yes, you are right, there is nothing like this in the help or examples!
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
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:
Don't forget to press "Cancel" on the stores you don't want to search through.VB Code:
for each Store in MAPI.FOLDERS rtn = msgbox ("Look in Store: " & Store & "?", 1) if rtn <> 2 then for each folder in STORE.Folders If Folder = "Inbox" then msgBox "Looking through the: " & Folder LookThroughFolder (Folder.ITEMS) end if next end if next
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.
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:
Dim myOlSel As Outlook.Selection Dim olExp, olCurrentFolder Dim x As Integer Set olExp = Outlook.ActiveExplorer Set olCurrentFolder = olExp.CurrentFolder MsgBox “Folder “ & olCurrentFolder & _ “ has “ & olCurrentFolder.Items & “ items in it” Set myOlSel = olExp.Selection ‘ Get the subject ‘ modify the subject in the message, and save the changes For x = 1 To myOlSel.Count thisSubject = CheckSubject(myOlSel.Item(x).Subject) MyOLSel.Item(x).Subject = “FILED – “ & thisSubject MyOLSel.Item(x).Save Next x
Or you should be able to go straight to the Inbox with something like:
VB Code:
Dim oOutlook As New Outlook.Application Dim oNameSpace As NameSpace Dim myItems As Items Set oNameSpace = oOutlook.GetNamespace("MAPI") Set myItems = oNameSpace.GetDefaultFolder (olFolderInbox).Items Call LookThroughFolder(myItems)
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
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:
msgbox "Searching Outlook Inbox for certain attachments . . ." ' ' First the sub-routine that looks through a specified folder looking for mail ' items and their attachments ' SUB LookThroughFolder (thisFolder) for each myItem in thisFolder ' ' Step through all items in the Folder ' if myItem.CLASS = "43" then for each Attachment in myItem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' if Attachment > "C" and Attachment < "D" then msgbox "FOUND IN: " & myItem.PARENT & " (IN: " & myitem.PARENT.PARENT & ")" & ". SUBJECT = " & myItem & ". ATTACHMENT = " & Attachment end if next end if next END SUB ' ' Now for the actual code, that looks in your default Inbox ' set OL = GetObject(,"OUTLOOK.APPLICATION") set MAPI = OL.GetNamespace("MAPI") Set myItems = MAPI.GetDefaultFolder(6).Items Msgbox "Looking at: " & myItems.Parent & " (" & myItems.COUNT & " items)." Call LookThroughFolder(myItems) Msgbox "Ending the search . . ."
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:
Sub LookThroughFolder(thisFolder) For Each myitem In thisFolder 'MsgBox "Looking at: " & myitem & ". In: " & myitem.Parent & " (" & thisFolder.Count & ")." ' ' Step through all items in the Folder ' If myitem.Class = "43" Then For Each Attachment In myitem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' ' If Attachment = "myAttachemen.mld" Then MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment 'End If Next End If Next End Sub Sub mylittletest() Dim MAPI As NameSpace Set MAPI = ThisOutlookSession.Application.GetNamespace("MAPI") Dim myitms As Items Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)." LookThroughFolder (myitms.Parent) End Sub
I get a weired error when the Sub LookThroughFolder(thisFolder)
starts..
I sorry for being a pain in the a@@
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
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
OK. Here is your code, but I have used different names to try to indicate what each collection really is:
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.VB Code:
Sub LookThroughFolder(ItemsInFolder) ' ' Step through all items in the Folder ' For Each myitem In ItemsInFolder 'MsgBox "Looking at: " & myitem & ". In: " & myitem.Parent & " (" & thisFolder.Count & ")." If myitem.Class = "43" Then For Each Attachment In myitem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment Next End If Next End Sub Sub mylittletest() Dim MAPI As NameSpace Set MAPI = ThisOutlookSession.Application.GetNamespace("MAPI") Dim myitms As Items Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)." 'THIS NEXT LINE HAS CHANGED LookThroughFolder (myitms) End Sub
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
oops..
myitms.parents was my last attempt before asking..
I tryed that..
this is what i get..
Seahag
OK. I have now switched from VBScript to standard VB, and YOU ARE RIGHT. You do get an error!
How about this:
VB Code:
Sub LookThroughFolder(inFolder) Set ItemsInThisFolder = inFolder.Items For Each myitem In ItemsInThisFolder ' ' Step through all items in the Folder ' If myitem.Class = "43" Then For Each Attachment In myitem.Attachments ' ' For any mail item (class 43) with an attachment, check its name ' ' If Attachment = "myAttachemen.mld" Then MsgBox "FOUND IN: " & myitem.Parent & " (IN: " & myitem.Parent.Parent & ")" & ". SUBJECT = " & myitem & ". ATTACHMENT = " & Attachment 'End If Next End If Next End Sub Set OL = GetObject(, "OUTLOOK.APPLICATION") Set MAPI = OL.GetNamespace("MAPI") Set myitms = MAPI.GetDefaultFolder(olFolderInbox).Items MsgBox "Looking at: " & myitms.Parent & " (" & myitms.Count & " items)." 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".
Ahhhhh..
YOur the best.. thnks..
How did you learn the object model?
(i am guessing not over night)
Seahag
:)
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