[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1920
  • Last Modified:

moving a message from one mailbox to another with VBA

I need to move a message from one folder in outlook to another folder in another mailbox in outlook using VBA from Access.

I have no problem moving messages around within the first mailbox (my personal outlook mailbox) but I do not know how to refer to my department mailbox in the same outlook application.

so I have a message in mailbox - [MyName], folder "Receipts" and i want to move it to mailbox - [Department], folder "Receipts"
0
efedora
Asked:
efedora
  • 3
  • 2
1 Solution
 
mvidasCommented:
Hi,

I'm not sure what criteria you're using to determine the message(s) to move, so I'll give you an example of moving the first message (as well as a shortened version of the code if you're really only going to be moving one item):

Sub efedoraExample()
 Dim vFolder1 As Object 'Outlook.MAPIFolder
 Dim vFolder2 As Object 'Outlook.MAPIFolder
 Dim olApp As Object 'Outlook.Application
 Set olApp = CreateObject("outlook.application")
 Set vFolder1 = olApp.Session.Folders("Mailbox - Your name").Folders("Receipts")
 Set vFolder2 = olApp.Session.Folders("Mailbox - Department").Folders("Receipts")
 vFolder1.Items(1).Move vFolder2
 Set vFolder1 = Nothing
 Set vFolder2 = Nothing
 Set olApp = Nothing
End Sub

Sub efedoraShortenedExample()
 Dim olApp As Object
 Set olApp = CreateObject("outlook.application")
 olApp.Session.Folders("Mailbox - Your name").Folders("Receipts").Items(1).Move _
  olApp.Session.Folders("Mailbox - Department").Folders("Receipts")
 Set olApp = Nothing
End Sub

Please let me know if you have any questions!
Matt
0
 
efedoraAuthor Commented:
sweet! thanks a lot matt. worked like a charm.

for some reason i don't have the little help boxes when i'm writing outlook code so it was hard to figure out any of the methods or properties since i'm unfamiliar with outlook references.
0
 
mvidasCommented:
Glad to help!
Did you
a) set a reference to microsoft outlook?
b) check (in vba) Tool / Options / Code Settings: Auto List Members?

Either way its faster just asking, and now you can play from there to figure out what else you can do :)
0
 
efedoraAuthor Commented:
yep and yep.
i did part of this module on friday and had them, but i lost that work and when i started again today i was basing it off a different code sample.

maybe because i'm working pretty much entirely with declared objects?

that is, if you declare an object objExample and set it, when you start a line with objExample will the editor know you are talking about an object that it knows some members for?
0
 
mvidasCommented:
Yes and no, the key to using that intellisense (what the Auto List Members is also called) is declaring the object right.

For example, if you use:
 Dim objOutlookApplication As Object

the intellisense won't work, but using:
 Dim objOutlookApplication As Outlook.Application

it will work. Occasionally the VBE won't know the members of an object if the object is a result of a method that can create a few different things. That may sound confusing, let me give you an example, again using outlook. For both of these examples, assume I have olApp variable declared as:
 Dim olApp As Outlook.Application

The outlook.application object has a method called "CreateItem", it can be used to make a Message, Contact, Appointment, etc, any number of different things. For this reason, the VBE won't know exactly what you're creating, so it can't list the members accordingly.  
While this will work fine to create a new message:

 With olApp.CreateItem(olMailItem)
  .To = "hi@domain.com"
  .Subject = "hi"
  .Body = "hello"
  .Send
 End With

The auto-list members won't work for that, since it isn't smart enough to read between the lines and see the olMailItem part (even moreso if you use the 0 constant equivalent). Using the above format (and the 0 constant) is great if you don't want to set a reference (something I try to avoid doing), but while writing it you might have some trouble unless you know the object.  What you can do in that case is something like:

 Dim olApp As Outlook.Application, olMail As Outlook.MailItem
 Set olMail = olApp.CreateItem(olMailItem)
 With olMail
  .To = "hi@domain.com"
  .Subject = "hi"
  .Body = "hello"
  .Send
 End With

Which will give you the intellisense, then simply change it to the first version when you're all set (of course for late binding you would change the olMailItem to 0)

Sorry if you got more than you were asking, figured it couldnt hurt :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now