A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

MailItem.SaveAs not working



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 10th 10, 01:13 PM posted to microsoft.public.outlook.program_vba
Chris
external usenet poster
 
Posts: 280
Default MailItem.SaveAs not working

I am trying to save messages as a MSG format. I checked on the forum and
found the code but when I save, the file name is not completely written and
the file size is 0 bytes. Right now the code is only for the open message.
Ideally, I would like the code to be run on an Outlook folder. I am
operating in a Vista Enterprise environment. Any help would be greatly
appreciated.

-----VBA Code-----
Sub SaveAsTXT()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim strname As String


Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem

strname = "U:\E-Mail\" & objItem.SenderName & " " & objItem.Subject
& objItem.Sent & ".msg"
MsgBox strname
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the item? " & _
"If a file with the same name already exists, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs strname, olMSGUnicode
End If
Else
MsgBox "There is no current active inspector."
End If
End Sub
-----End of VBA Code-----
Ads
  #2  
Old May 10th 10, 03:51 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default MailItem.SaveAs not working

See if the file name you end up with has any illegal characters in it. See
if you can save the same item yourself manually, to see if that works.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
I am trying to save messages as a MSG format. I checked on the forum and
found the code but when I save, the file name is not completely written
and
the file size is 0 bytes. Right now the code is only for the open
message.
Ideally, I would like the code to be run on an Outlook folder. I am
operating in a Vista Enterprise environment. Any help would be greatly
appreciated.

-----VBA Code-----
Sub SaveAsTXT()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim strname As String


Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem

strname = "U:\E-Mail\" & objItem.SenderName & " " & objItem.Subject
& objItem.Sent & ".msg"
MsgBox strname
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the item? " & _
"If a file with the same name already exists, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs strname, olMSGUnicode
End If
Else
MsgBox "There is no current active inspector."
End If
End Sub
-----End of VBA Code-----


  #3  
Old May 11th 10, 02:59 PM posted to microsoft.public.outlook.program_vba
Chris
external usenet poster
 
Posts: 280
Default MailItem.SaveAs not working

Ken,

That partially solved the problem as it was adding an additional path to the
name which had illegal characters. However, it seems to be missing items
such as read receipts, meeting requests and responses, and tasks. Is there a
way to check for these items and then make the filename based on those
available fields? My thought is to do a Select Case TheEmail.Class but I
don't know if this will work and what fields are availble to create a name.
Right now, messages are created by SenderName, a scrubbed subject, and a
scrubbed received date. I would assume that I have to scrub any of the
fields in the other item types.

Thanks for assisting.

"Ken Slovak - [MVP - Outlook]" wrote:

See if the file name you end up with has any illegal characters in it. See
if you can save the same item yourself manually, to see if that works.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
I am trying to save messages as a MSG format. I checked on the forum and
found the code but when I save, the file name is not completely written
and
the file size is 0 bytes. Right now the code is only for the open
message.
Ideally, I would like the code to be run on an Outlook folder. I am
operating in a Vista Enterprise environment. Any help would be greatly
appreciated.

-----VBA Code-----
Sub SaveAsTXT()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim strname As String


Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem

strname = "U:\E-Mail\" & objItem.SenderName & " " & objItem.Subject
& objItem.Sent & ".msg"
MsgBox strname
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the item? " & _
"If a file with the same name already exists, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs strname, olMSGUnicode
End If
Else
MsgBox "There is no current active inspector."
End If
End Sub
-----End of VBA Code-----


.

  #4  
Old May 11th 10, 03:25 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default MailItem.SaveAs not working

Many of those "missing things" would not be available in a saved out MSG
file. You can certainly get any fields you want from a mail item to provide
a naming scheme, but in all cases you will want to make sure there are no
illegal characters in the resulting file name. These days you don't need to
worry about things like making your file name an 8.3 format, but illegal
characters are always a concern.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

That partially solved the problem as it was adding an additional path to
the
name which had illegal characters. However, it seems to be missing items
such as read receipts, meeting requests and responses, and tasks. Is
there a
way to check for these items and then make the filename based on those
available fields? My thought is to do a Select Case TheEmail.Class but I
don't know if this will work and what fields are availble to create a
name.
Right now, messages are created by SenderName, a scrubbed subject, and a
scrubbed received date. I would assume that I have to scrub any of the
fields in the other item types.

Thanks for assisting.


  #5  
Old May 14th 10, 02:47 PM posted to microsoft.public.outlook.program_vba
Chris
external usenet poster
 
Posts: 280
Default MailItem.SaveAs not working

Ken,

Is there a way to save the delivery and read receipts and meeting request
acceptances/declines?

I looked at the Class of the delivery receipt and it came up the same as a
message. There is no ol equivalent for a read or delivery receipt but there
is for meeting requests.

I need to be able to copy everything in a folder to a .msg format. My code
works on messages perfectly but not on those items mentioned. I did scrub
the information and it still would not save. PLEASE HELP!!!!!

Chris

"Ken Slovak - [MVP - Outlook]" wrote:

Many of those "missing things" would not be available in a saved out MSG
file. You can certainly get any fields you want from a mail item to provide
a naming scheme, but in all cases you will want to make sure there are no
illegal characters in the resulting file name. These days you don't need to
worry about things like making your file name an 8.3 format, but illegal
characters are always a concern.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

That partially solved the problem as it was adding an additional path to
the
name which had illegal characters. However, it seems to be missing items
such as read receipts, meeting requests and responses, and tasks. Is
there a
way to check for these items and then make the filename based on those
available fields? My thought is to do a Select Case TheEmail.Class but I
don't know if this will work and what fields are availble to create a
name.
Right now, messages are created by SenderName, a scrubbed subject, and a
scrubbed received date. I would assume that I have to scrub any of the
fields in the other item types.

Thanks for assisting.


.

  #6  
Old May 14th 10, 03:19 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default MailItem.SaveAs not working

I'm not sure what you're talking about.

Delivery reports for example have a MessageClass of "REPORT.IPM.Note.DR".
That's not an email MessageClass.

You would instantiate any item where the MessageClass starts with "REPORT"
as a ReportItem, with which you should be able to use SaveAs().

Most recipients won't allow delivery of either types of reports, but where
you do get them back that's what you'd do.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

Is there a way to save the delivery and read receipts and meeting request
acceptances/declines?

I looked at the Class of the delivery receipt and it came up the same as a
message. There is no ol equivalent for a read or delivery receipt but
there
is for meeting requests.

I need to be able to copy everything in a folder to a .msg format. My
code
works on messages perfectly but not on those items mentioned. I did scrub
the information and it still would not save. PLEASE HELP!!!!!

Chris


  #7  
Old May 14th 10, 05:11 PM posted to microsoft.public.outlook.program_vba
Chris
external usenet poster
 
Posts: 280
Default MailItem.SaveAs not working

Ken,

The Class I was referring to is the
Application.ActiveExplorer.CurrentFolder.Items.Ite m(I).Class. I had a return
of 43.

I do not know how to utilize MessageClass to isolate as it seems to call the
delivery receipt IPM.NOTE according to what was outputted to the messagebox
and not as what you had below. I am using Outlook 2007.

Chris

"Ken Slovak - [MVP - Outlook]" wrote:

I'm not sure what you're talking about.

Delivery reports for example have a MessageClass of "REPORT.IPM.Note.DR".
That's not an email MessageClass.

You would instantiate any item where the MessageClass starts with "REPORT"
as a ReportItem, with which you should be able to use SaveAs().

Most recipients won't allow delivery of either types of reports, but where
you do get them back that's what you'd do.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

Is there a way to save the delivery and read receipts and meeting request
acceptances/declines?

I looked at the Class of the delivery receipt and it came up the same as a
message. There is no ol equivalent for a read or delivery receipt but
there
is for meeting requests.

I need to be able to copy everything in a folder to a .msg format. My
code
works on messages perfectly but not on those items mentioned. I did scrub
the information and it still would not save. PLEASE HELP!!!!!

Chris


.

  #8  
Old May 14th 10, 08:44 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default MailItem.SaveAs not working

You need to check the item's MessageClass property, which is a string. If
you are doing that in a procedure or event handler where the item is an
Object you would do that using reflection.

If you use Class, which won't work for you correctly, you will need to try a
cast to MailItem, if that fails try a cast to ReportItem and so on.
Personally I'd be using MessageClass myself.

I'd get MessageClass and use string.StartsWith() to see if the MessageClass
starts with "REPORT".

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

The Class I was referring to is the
Application.ActiveExplorer.CurrentFolder.Items.Ite m(I).Class. I had a
return
of 43.

I do not know how to utilize MessageClass to isolate as it seems to call
the
delivery receipt IPM.NOTE according to what was outputted to the
messagebox
and not as what you had below. I am using Outlook 2007.

Chris


  #9  
Old May 21st 10, 05:19 PM posted to microsoft.public.outlook.program_vba
Chris
external usenet poster
 
Posts: 280
Default MailItem.SaveAs not working

OK Ken,

It seems so close and yet so far away. I am doing the message class but
anytime it htis a delivery report or read receipt, I cannot get the message
class. I have a check to add the category "Not Copied" (it exists in the
list) and it will change the category of the message prior to the receipt.
The message box never displays a "REPORT" message class just "IPM.NOTE" and
the out of office one. I am including the code and am hoping a light will
shine on the error in the code. Thanks for your continued assistance.

Chris
-----CODE START-----
Dim TheEmail As Outlook.MailItem
Dim eItem As Outlook.Items
Dim EmailNS As NameSpace
Dim fldrCount, EmailPath2, NbrItem, myfolder
Dim strSubj, strTime, mailClassCheck, EmailPath As String
Dim NewFileName As String
Dim Cats
Dim CheckErr, Exists As Boolean

CheckErr = False
Set EmailNS = Application.GetNamespace("MAPI")
Set myfolder = Application.ActiveExplorer.CurrentFolder
NbrItem = myfolder.Items.Count
On Error GoTo Error_Handler

'EmailPath = InputBox("Enter the save folder location:", "Email Save
Path", CurDir)
EmailPath = "C:\users\CMPurdom\Desktop\Mail Burn\Tester\"
For i = 1 To NbrItem
Set TheEmail = Application.ActiveExplorer.CurrentFolder.Items.Ite m(i)
mailClassCheck = TheEmail.MessageClass
MsgBox mailClassCheck
If Right(mailClassCheck, 6) = "REPORT" Then
SaveMailAsFile TheEmail, olSaveAsMsg,
"C:\users\CMPurdom\Desktop\Mail Burn\Testers\"
GoTo Step1
End If
If TheEmail.Subject = "" Then strSubj = "no subject"

strSubj = Replace(TheEmail.Subject, "/", "-")
strSubj = Replace(strSubj, "\", "-")
strSubj = Replace(strSubj, ":", "--")
strSubj = Replace(strSubj, "?", sReplace)
strSubj = Replace(strSubj, Chr(34), sReplace)
strSubj = Replace(strSubj, "", sReplace)
strSubj = Replace(strSubj, "", sReplace)
strSubj = Replace(strSubj, "|", sReplace)
strTime = Replace(TheEmail.ReceivedTime, "/", "-")
strTime = Replace(strTime, "\", "-")
strTime = Replace(strTime, ":", ".")
strTime = Replace(strTime, "?", sReplace)
strTime = Replace(strTime, Chr(34), sReplace)
strTime = Replace(strTime, "", sReplace)
strTime = Replace(strTime, "", sReplace)
strTime = Replace(strTime, "|", sReplace)
'SaveMailAsFile TheEmail, olSaveAsMsg, "C:\users\CMPurdom\Desktop\Mail
Burn\Testers\"
NewFileName = TheEmail.SenderName & "_" & strTime & "_" & strSubj &
".msg"

If NewFileName "" Then
TheEmail.SaveAs EmailPath & NewFileName, olMSG
Else
MsgBox "No file name was entered. Operation aborted.", 64,
"Cancel Operation"
Exit Sub
End If
Step1:
Next i
GoTo Done

Error_Handler:
MsgBox TheEmail.MessageClass & Chr$(13) & TheEmail.Subject & Chr$(13) &
Err.Number & ": " & Err.Description
TheEmail.Categories = TheEmail.Categories & ";" & "Not Copied"
TheEmail.Save
Resume Next

Done:
End Sub
-----CODE END-----

"Ken Slovak - [MVP - Outlook]" wrote:

You need to check the item's MessageClass property, which is a string. If
you are doing that in a procedure or event handler where the item is an
Object you would do that using reflection.

If you use Class, which won't work for you correctly, you will need to try a
cast to MailItem, if that fails try a cast to ReportItem and so on.
Personally I'd be using MessageClass myself.

I'd get MessageClass and use string.StartsWith() to see if the MessageClass
starts with "REPORT".

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
Ken,

The Class I was referring to is the
Application.ActiveExplorer.CurrentFolder.Items.Ite m(I).Class. I had a
return
of 43.

I do not know how to utilize MessageClass to isolate as it seems to call
the
delivery receipt IPM.NOTE according to what was outputted to the
messagebox
and not as what you had below. I am using Outlook 2007.

Chris


.

  #10  
Old May 21st 10, 05:29 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default MailItem.SaveAs not working

I'm wondering if possibly declaring TheEmail as Object rather than MailItem
would be helpful. Do you ever hit the error handler code? If you do it
could be because instantiating a MailItem object from a report item would
fire an exception.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Chris" wrote in message
...
OK Ken,

It seems so close and yet so far away. I am doing the message class but
anytime it htis a delivery report or read receipt, I cannot get the
message
class. I have a check to add the category "Not Copied" (it exists in the
list) and it will change the category of the message prior to the receipt.
The message box never displays a "REPORT" message class just "IPM.NOTE"
and
the out of office one. I am including the code and am hoping a light will
shine on the error in the code. Thanks for your continued assistance.

Chris
-----CODE START-----
Dim TheEmail As Outlook.MailItem
Dim eItem As Outlook.Items
Dim EmailNS As NameSpace
Dim fldrCount, EmailPath2, NbrItem, myfolder
Dim strSubj, strTime, mailClassCheck, EmailPath As String
Dim NewFileName As String
Dim Cats
Dim CheckErr, Exists As Boolean

CheckErr = False
Set EmailNS = Application.GetNamespace("MAPI")
Set myfolder = Application.ActiveExplorer.CurrentFolder
NbrItem = myfolder.Items.Count
On Error GoTo Error_Handler

'EmailPath = InputBox("Enter the save folder location:", "Email Save
Path", CurDir)
EmailPath = "C:\users\CMPurdom\Desktop\Mail Burn\Tester\"
For i = 1 To NbrItem
Set TheEmail = Application.ActiveExplorer.CurrentFolder.Items.Ite m(i)
mailClassCheck = TheEmail.MessageClass
MsgBox mailClassCheck
If Right(mailClassCheck, 6) = "REPORT" Then
SaveMailAsFile TheEmail, olSaveAsMsg,
"C:\users\CMPurdom\Desktop\Mail Burn\Testers\"
GoTo Step1
End If
If TheEmail.Subject = "" Then strSubj = "no subject"

strSubj = Replace(TheEmail.Subject, "/", "-")
strSubj = Replace(strSubj, "\", "-")
strSubj = Replace(strSubj, ":", "--")
strSubj = Replace(strSubj, "?", sReplace)
strSubj = Replace(strSubj, Chr(34), sReplace)
strSubj = Replace(strSubj, "", sReplace)
strSubj = Replace(strSubj, "", sReplace)
strSubj = Replace(strSubj, "|", sReplace)
strTime = Replace(TheEmail.ReceivedTime, "/", "-")
strTime = Replace(strTime, "\", "-")
strTime = Replace(strTime, ":", ".")
strTime = Replace(strTime, "?", sReplace)
strTime = Replace(strTime, Chr(34), sReplace)
strTime = Replace(strTime, "", sReplace)
strTime = Replace(strTime, "", sReplace)
strTime = Replace(strTime, "|", sReplace)
'SaveMailAsFile TheEmail, olSaveAsMsg, "C:\users\CMPurdom\Desktop\Mail
Burn\Testers\"
NewFileName = TheEmail.SenderName & "_" & strTime & "_" & strSubj &
".msg"

If NewFileName "" Then
TheEmail.SaveAs EmailPath & NewFileName, olMSG
Else
MsgBox "No file name was entered. Operation aborted.", 64,
"Cancel Operation"
Exit Sub
End If
Step1:
Next i
GoTo Done

Error_Handler:
MsgBox TheEmail.MessageClass & Chr$(13) & TheEmail.Subject & Chr$(13) &
Err.Number & ": " & Err.Description
TheEmail.Categories = TheEmail.Categories & ";" & "Not Copied"
TheEmail.Save
Resume Next

Done:
End Sub
-----CODE END-----


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MailItem SaveAs Koen Verwimp Add-ins for Outlook 2 September 22nd 08 02:41 PM
MailItem.SaveAs encoding characters wrong from HTML message MattS Outlook and VBA 3 April 1st 08 03:26 PM
MailItem.SaveAs method Mrunali Outlook - Using Forms 0 April 17th 07 04:16 PM
_MailItem - SaveAs MON205 Add-ins for Outlook 1 February 22nd 07 07:01 PM
How can I create a MailItem that displays like a received MailItem ? Clive Outlook - Using Forms 0 February 27th 06 05:14 PM


All times are GMT +1. The time now is 05:08 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.