Save sent messages as msg file automatically

Cardstang
Lounger
Posts: 35
Joined: 17 Feb 2010, 22:08

Save sent messages as msg file automatically

Post by Cardstang »

Hi all,

I have the below code in my "ThisOutlookSession" Object. As messages come into my Inbox, it automatically saves messages as an msg file to a server that meet the subject criteria. (I've modified for the example, so hopefully I didn't break anything modifying it). It works without any issues at all.

What I'm wondering, is there a way to perform the same function, except when sending a message? I just need certain types of messages saved as msg files...not all of them.

Code: Select all

Option Explicit

Public WithEvents itmsNewMessages As Outlook.Items

Private Sub Application_Startup()
Set itmsNewMessages = Outlook.Session.GetDefaultFolder(olFolderInbox).Items

End Sub

Private Sub Application_Quit()
Set itmsNewMessages = Nothing
End Sub

Private Sub itmsNewMessages_ItemAdd(ByVal Item As Object)
With Item

If .Class = olMail Then
If .Subject Like "Message" & "*" Then
.SaveAs "\\server\path" & .msg:, olMSG
End If
End If
End With
End Sub
Thanks!

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Save sent messages as msg file automatically

Post by HansV »

I haven't tested this code, so you'll have to do that yourself. Like the existing code, it should go into the ThisOutlookSession module:

Code: Select all

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  With Item
    If .Class = olMail Then
      If .Subject Like "Message" & "*" Then
        .SaveAs "\\server\path\" & Item.Subject & ".msg", olMSG
      End If
    End If
  End With
End Sub
Best wishes,
Hans

Cardstang
Lounger
Posts: 35
Joined: 17 Feb 2010, 22:08

Re: Save sent messages as msg file automatically

Post by Cardstang »

HansV,

That did it.

Thanks a bunch!

Cardstang
Lounger
Posts: 35
Joined: 17 Feb 2010, 22:08

Re: Save sent messages as msg file automatically

Post by Cardstang »

Well, it saves the message like I said it did, but it seems to save it where it looks like a draft message. There is no Sent time within the message itself when opened up.

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Save sent messages as msg file automatically

Post by HansV »

Does this work better?

Code: Select all

Public WithEvents itmsNewMessages As Outlook.Items
Public WithEvents itmsSentMessages As Outlook.Items

Private Sub Application_Startup()
  Set itmsNewMessages = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
  Set itmsSentMessages = Outlook.Session.GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub Application_Quit()
  Set itmsNewMessages = Nothing
  Set itmsSentMessages = Nothing
End Sub

Private Sub itmsNewMessages_ItemAdd(ByVal Item As Object)
  With Item
    If .Class = olMail Then
      If .Subject Like "Message" & "*" Then
        .SaveAs "\\server\path\" & Item.Subject & ".msg", olMSG
      End If
    End If
 End With
End Sub

Private Sub itmsSentMessages_ItemAdd(ByVal Item As Object)
  With Item
    If .Class = olMail Then
      If .Subject Like "Message" & "*" Then
        .SaveAs "\\server\path\" & Item.Subject & ".msg", olMSG
      End If
    End If
 End With
End Sub
Best wishes,
Hans

Cardstang
Lounger
Posts: 35
Joined: 17 Feb 2010, 22:08

Re: Save sent messages as msg file automatically

Post by Cardstang »

Hans,

That works as needed. It seems to pause a bit to save the file, but it saves them with the time stamp.

Thanks so much.

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Save sent messages as msg file automatically

Post by Goshute »

Hans' revised code triggers after the message is sent, so I expect there will be a delay after you hit the Send button; first it has to send, then be placed in the Sent Folder, then the code runs.
Goshute
I float in liquid gardens