Using VBA code to move current message to a folder

D

Dean

Hello,
I want to automate the task of moving the currently open Outlook message into
a folder called "TEMP" I found a code example on this forum but have a few
questions. My variation of the code pasted below.

1) How do I dimension those object variables (note ????? in the code)?
2) Does the code look right?

Thanks in advance for any help you can provide.
Best Regards,
Dean

'---------------------------------------------------------------------------
Public Sub moveMsgToTEMP()
On Error GoTo Err_moveMsgToTEMP

'this sub moves the currently open message to the TEMP folder

Dim objApp As Outlook.Application
Dim olnBoxItems as ?????
Dim olNS As Outlook.NameSpace
'Dim objTargetFolder as ???????
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim objCurItem as ??????

Set oInBoxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set olNS = Item.Application.GetNamespace("MAPI")
Set objTargetFolder = objInbox.Folders("TEMP")
Set objCurItem = objCurItem.Move(objTargetFolder)
objCurItem.Save

Exit_moveMsgToTEMP:
Exit Sub
Err_moveMsgToTEMP:
MsgBox "sub moveMsgToTEMP " & Err.Description
Resume Exit_moveMsgToTEMP
End Sub
'---------------------------------------------------------------------------
 
M

Michael Bauer

Am Sat, 25 Feb 2006 08:24:56 GMT schrieb Dean:

Very good Dean, just a little messed up :)

You need a ref onto the currently opened item. If you want to handle every
item types with then you can use a declaration As Object. Else, if there are
MailItems only you could declare the variable As MailItem.

Then you need a ref onto the target folder. Every folder is the same object
type of and is called MapiFolder.

If the target folder is a subfolder of the Inbox then get a ref onto the
Inbox (itself, not its Items) first then onto a child of it.

In code:

Dim objCurItem as Object
Dim InboxFolder as Outlook.MapiFolder
Dim TargetFolder as Outlook.MapiFolder

' currently opened item
Set objCurItem = Application.ActiveInspector.CurrentItem

' Inbox folder
Set InboxFolder = Application.Session.GetDefaultFolder(olInboxFolder)

' Subfolder of the Inbox
Set TargetFolder = InboxFolder.Item("TEMP")

' Move the message
Set objCurItem = objCurItem.Move(TargetFolder)


--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.vbOffice.net --

Hello,
I want to automate the task of moving the currently open Outlook message into
a folder called "TEMP" I found a code example on this forum but have a few
questions. My variation of the code pasted below.

1) How do I dimension those object variables (note ????? in the code)?
2) Does the code look right?

Thanks in advance for any help you can provide.
Best Regards,
Dean

'---------------------------------------------------------------------------
Public Sub moveMsgToTEMP()
On Error GoTo Err_moveMsgToTEMP

'this sub moves the currently open message to the TEMP folder

Dim objApp As Outlook.Application
Dim olnBoxItems as ?????
Dim olNS As Outlook.NameSpace
'Dim objTargetFolder as ???????
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim objCurItem as ??????

Set oInBoxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set olNS = Item.Application.GetNamespace("MAPI")
Set objTargetFolder = objInbox.Folders("TEMP")
Set objCurItem = objCurItem.Move(objTargetFolder)
objCurItem.Save

Exit_moveMsgToTEMP:
Exit Sub
Err_moveMsgToTEMP:
MsgBox "sub moveMsgToTEMP " & Err.Description
Resume Exit_moveMsgToTEMP
End Sub
'---------------------------------------------------------------------------
 
D

Dean

Hi Michael,
Thanks for the advice. I'm still having a problem with this line:

Set InboxFolder = Application.Session.GetDefaultFolder(olInboxFolder)

I thought maybe it was a problem with "olInboxFolder" and tried the constant
"olFolderInbox" but that didn't work. I'm also wondering if I need the lack
of NameSpace and BoxItems instantiations is a problem. Thanks.


Michael said:
Am Sat, 25 Feb 2006 08:24:56 GMT schrieb Dean:

Very good Dean, just a little messed up :)

You need a ref onto the currently opened item. If you want to handle every
item types with then you can use a declaration As Object. Else, if there are
MailItems only you could declare the variable As MailItem.

Then you need a ref onto the target folder. Every folder is the same object
type of and is called MapiFolder.

If the target folder is a subfolder of the Inbox then get a ref onto the
Inbox (itself, not its Items) first then onto a child of it.

In code:

Dim objCurItem as Object
Dim InboxFolder as Outlook.MapiFolder
Dim TargetFolder as Outlook.MapiFolder

' currently opened item
Set objCurItem = Application.ActiveInspector.CurrentItem

' Inbox folder
Set InboxFolder = Application.Session.GetDefaultFolder(olInboxFolder)

' Subfolder of the Inbox
Set TargetFolder = InboxFolder.Item("TEMP")

' Move the message
Set objCurItem = objCurItem.Move(TargetFolder)
Hello,
I want to automate the task of moving the currently open Outlook message into
[quoted text clipped - 7 lines]
Best Regards,
Dean
'---------------------------------------------------------------------------
Public Sub moveMsgToTEMP()
On Error GoTo Err_moveMsgToTEMP
[quoted text clipped - 21 lines]
Resume Exit_moveMsgToTEMP
End Sub

'---------------------------------------------------------------------------
 
M

Michael Bauer

Am Sat, 25 Feb 2006 11:31:37 GMT schrieb Dean:

Sorry, olFolderInbox is correct.

What do you mean by "that doesn´t work", do you get an error?

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.vbOffice.net --

Hi Michael,
Thanks for the advice. I'm still having a problem with this line:

Set InboxFolder = Application.Session.GetDefaultFolder(olInboxFolder)

I thought maybe it was a problem with "olInboxFolder" and tried the constant
"olFolderInbox" but that didn't work. I'm also wondering if I need the lack
of NameSpace and BoxItems instantiations is a problem. Thanks.


Michael said:
Am Sat, 25 Feb 2006 08:24:56 GMT schrieb Dean:

Very good Dean, just a little messed up :)

You need a ref onto the currently opened item. If you want to handle every
item types with then you can use a declaration As Object. Else, if there are
MailItems only you could declare the variable As MailItem.

Then you need a ref onto the target folder. Every folder is the same object
type of and is called MapiFolder.

If the target folder is a subfolder of the Inbox then get a ref onto the
Inbox (itself, not its Items) first then onto a child of it.

In code:

Dim objCurItem as Object
Dim InboxFolder as Outlook.MapiFolder
Dim TargetFolder as Outlook.MapiFolder

' currently opened item
Set objCurItem = Application.ActiveInspector.CurrentItem

' Inbox folder
Set InboxFolder = Application.Session.GetDefaultFolder(olInboxFolder)

' Subfolder of the Inbox
Set TargetFolder = InboxFolder.Item("TEMP")

' Move the message
Set objCurItem = objCurItem.Move(TargetFolder)
Hello,
I want to automate the task of moving the currently open Outlook message
into
[quoted text clipped - 7 lines]
Best Regards,
Dean
'---------------------------------------------------------------------------
Public Sub moveMsgToTEMP()
On Error GoTo Err_moveMsgToTEMP
[quoted text clipped - 21 lines]
Resume Exit_moveMsgToTEMP
End Sub

'---------------------------------------------------------------------------
 
D

Dean via OfficeKB.com

Hi Michael,
I was getting an error (forget the exact error message). I combined the
advice you gave me along with some code I pulled off of the MS website and
came up with something that works (pasted below). Thanks for your help!
Best Regards,
Dean


'-----------------------------------------------------------------------------
-------------
Public Sub moveMsgToTEMP()
On Error GoTo Err_moveMsgToTEMP

'this sub moves the currently open message to the TEMP folder

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("TEMP")
Set myItem = Application.ActiveInspector.CurrentItem

myItem.Move myDestFolder

Exit_moveMsgToTEMP:
Exit Sub
Err_moveMsgToTEMP:
MsgBox "sub moveMsgToTEMP " & Err.Description
Resume Exit_moveMsgToTEMP
End Sub
'---------------------------------------------------------------------



Michael said:
Am Sat, 25 Feb 2006 11:31:37 GMT schrieb Dean:

Sorry, olFolderInbox is correct.

What do you mean by "that doesn´t work", do you get an error?
Hi Michael,
Thanks for the advice. I'm still having a problem with this line:
[quoted text clipped - 51 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top