Link to home
Start Free TrialLog in
Avatar of rdy123 rdy
rdy123 rdy

asked on

vba code in excel to save the current open mail item as .msg or .oft file in a location

i have a opened outlook mail, i want to save that opened current mail to location C:\temp as .msg or .oft,is there any code to save the current mail as .msg format and i also want to replace the .msg file always with the new mail and save it with the same .msg file name.
please suggest
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Try this:
Public Sub SaveMessageAsMsg()
  Dim oMail As Outlook.MailItem
  Dim objItem As Object
   For Each objItem In ActiveExplorer.Selection
   If objItem.MessageClass = "IPM.Note" Then
    Set oMail = objItem   
    oMail.SaveAs  C:\temp & "name_of_message" & ".msg", olMSG
  End If
  Next
End Sub

Open in new window

Avatar of rdy123 rdy
rdy123 rdy

ASKER

hi,

thank you for your response,but i am getting runtime error 70, any suggestions please
i have tried giving location in my documents its working fine but it is not saving the current opened mail,its saving the first mail in outlook
Hi,

i used the below code,but it is opening new mail and saving it in the specified location,but i want to run the code on opened current mail,please suggest which line to be changed to save the current mail

Sub msg()

Const olMailItem = 0
Const olMSG = 3

Dim olkApp, olkMsg
Dim myItem As Outlook.MailItem

'Set olkApp = CreateObject("Outlook.Application")
Set olkApp = GetObject(, "Outlook.Application")
    'Set myItem = olkApp.ActiveInspector.CurrentItem
olkApp.Session.Logon "Outlook"

Set olkMsg = olkApp.CreateItem(olMailItem)
Set myItem = olkApp.ActiveInspector.CurrentItem
With olkMsg
    .Subject = "Some Subject"
    .Body = "Some text"
    .SaveAs "C:\untitle.msg", olMSG

End With

Set olkMsg = Nothing


Set olkApp = Nothing
End Sub
Runtime error 70 means that you don't have permission to write to that directory.
Line 7 of my original code should read:
oMail.SaveAs  "C:\temp\" & "name_of_message" & ".msg", olMSG

Open in new window

i have tried the same,and by giving other folder it worked,but the issue is, it is saving the first mail of inbox instead of saving the current opened mail.
It should be saving the selected message, is that not the case?
no,it should save the current mail which is already opened,please advise.
Try this:
Public Sub SaveMessageAsMsg()
    Dim myItem As Outlook.Inspector
    Dim objItem As Object
    Set myItem = Application.ActiveInspector
    If Not TypeName(myItem) = "Nothing" Then
        Set objItem = myItem.CurrentItem
        oMail.SaveAs "C:\temp\" & "name_of_message" & ".msg", olMSG
    Else
        MsgBox "There is no current active inspector."
    End If
End Sub

Open in new window

hi, thank you,i have tried the above code but i am getting run time error 438.please advise.
Oops, my bad, here this should work:
Public Sub SaveMessageAsMsg()
    Dim myItem As Outlook.Inspector
    Dim objItem As Object
    Set myItem = Application.ActiveInspector
    If Not TypeName(myItem) = "Nothing" Then
        Set objItem = myItem.CurrentItem
        objItem.SaveAs "C:\temp\" & "name_of_message" & ".msg", olMSG
    Else
        MsgBox "There is no current active inspector."
    End If
End Sub

Open in new window

thank you for quick response,but its the same and giving the same error.
On which line does the error occur?
Set myItem = Application.ActiveInspector
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wow,that's perfect -Subodh Tiwari (Neeraj),thank you both again
You're welcome. Glad it worked for you.
Also I think you should have split the points considering the assistance offered by MacroShadow. If you agree, please click the Request Attention button and raise a request to open the question so that you can split the points by accepting the answers again. :)
yup,i thought the same to split the points instead of cancel i have clicked submit,again i am unable to change,can i know how to share points again,and where is Request Attention button?
No problem.
As I said you may use Request Attention link in this case.
In your first post with question itself, you will find the three dots next to the share icon and if you click the three dots, you will find a link to Request Attention. Click it and request to reopen the question explaining the issue and a moderator will take care of this.
thanks a lot..
Thanks for splitting the points. :)
Glad we could help.