Programmatically moving selected messages to network folder

  • Thread starter Thread starter Ken Warthen
  • Start date Start date
K

Ken Warthen

I'm working with a company who would like to automate some of their Outlook
2007 processes. They maintain a project folder on a server with folders
containing every document and communication related to a given project. They
currently move Outlook email messages to the respective project folder by
dragging and dropping them from Outlook to an Explorer window. I've set up a
contextual menu so that when a message is selected and right clicked the user
can select an option from the pop up menu to move the selected message to a
projects folder.

My intent was to have a folder dialog box open where the user can select the
appropriate project's folder and then move the message from Outlook to the
selected folder. It appears the Outlook's .Move command will only allow you
to move a message to an Outlook folder. Any ideas on how I might proceed
from here?

TIA,

Ken
 
Ken

I've been playing with SaveAs as you can see in my code below. When I check
the destination folder there is an object there but it's label is truncated,
there's no .msg extension, and no message icon, as you would get when
dragging and dropping messages directly into the folder. The explorer window
also shows the file type as "File", where the messages that were dragged and
dropped have a file type "Outlook Item".

Private Sub MoveToDSiProjectFolder()
On Error GoTo PROC_ERROR
Dim objNamespace As NameSpace
Dim objItem As MailItem
Dim objRecipient As Recipient
Dim strEntryID As String
Dim strSubject As String
Dim strSender As String
Dim strRecipient As String
Dim strDestinationFolder As String

strEntryID =
Application.ActiveExplorer.CommandBars.ActionControl.Parameter

If strEntryID <> "" Then
Set objNamespace = Application.GetNamespace("MAPI")
Set objItem = objNamespace.GetItemFromID(strEntryID)

With objItem
strSubject = .Subject
strSender = .SenderName
strRecipient = .ReceivedByName
End With
strDestinationFolder = "C:\Users\kwarthen\Desktop\Project
Related Mail\"
objItem.SaveAs strDestinationFolder & objItem, olMSG
End If

PROC_EXIT:
On Error GoTo 0
Set objRecipient = Nothing
Set objItem = Nothing
Set objNamespace = Nothing
Exit Sub
PROC_ERROR:
Call ShowError("modDSi", "MoveToDSiProjectFolder", Err.Number,
Err.Description, Err.Source)
Resume PROC_EXIT
Resume
End Sub





Ken
 
You shouldn't be using objItem in SaveAs and relying on a default property.
Use something list objItem.Subject and then make sure to add the file
extension (objItem.Subject & ".MSG").
 
Ken,

Excellent! Of course, that worked. Thanks so much for your help. Any
suggestions on how to get user input on where to save the file? As I
understand it, Outlook does not support items like
Application.FileDialog(msoFileDialogFolderPicker). This is actually my first
venture in Outlook programming. Most of the development I've done in the
past has been in Access. Any suggestions on resources for getting up to
speed with Outlook programming?

Ken
 
Ken,

After additional testing it seems the code will work with some messages but
not others. Is there anything I need to look for in Subject fields that
might trigger an error?

Subject = "Mitch's father.msg" Works
Subject = "Confirmation: Getting Up and Going with Revit Structure 2009:
From Pilot to Production.msg" Triggers Error number -2147286788

Thanks again for your expertise.

Ken
 
Please disregard the above post, as a little more reading provided
information on invalid characters. After passing the subject to a function
to clean up any invalid characters, my code is working consistently.

I still haven't figured out how to get user input to select the appropriate
project folder to move (or save) the message to.

Ken
 
I use Win32 API calls to display the folder selector dialog, or if the code
will run on a machine with VB6 installed I use the common dialogs control
from that distribution. If I use Win32 calls I end up at common dialogs
anyway.

You can search for code that calls ComDlg32, I believe that VBAdvantage has
some samples for that. They probably use VB6 code but would work almost
identically in VBA code.

For general Outlook code samples you can't do better than
www.outlookcode.com. There's a wealth of sample code there as well as lots
of other information on Outlook programming.
 
Back
Top