Is there a VBA macro that can embed an email that is in the outlook Inbox into an Excel sheet?

purinavin

New Member
Joined
May 12, 2005
Messages
7
To all Excel Gurus!
Is there a vba macro that can run from Excel that will open up the Outlook Inbox so that a specific email can be selected and be embedded into a spreadsheet as an object? ie. When a user double clicks the embedded email icon in the sheet, then that particular email opens up. I know how to do this manually, but was wondering if anyone has a macro that does the same thing?
 
Hello Worf, Thank you so much for your help. I tried the code you shared and received some errors. I check how to fix it. And finally It is working now!!!
Please have the steps, and working code for your control and review.

1.Step : I changed the following folder as a folder in my desktop. (just change d:\test as a folder you choose)

before :
sn = "d:\test\" & sn & ".msg"

after :
sn = "C:\Users\Public\Desktop\vba\" & sn & ".msg"

2.Step: I received "Run Time Error 1004 Can not insert object" error when I run the code. And solution come up with changing Link:True as Link:False.

debug line is :

Set o = ActiveSheet.OLEObjects.Add(FileName:=sn, Link:=True, DisplayAsIcon:=False)

I changed as :

Set o = ActiveSheet.OLEObjects.Add(FileName:=sn, Link:=False, DisplayAsIcon:=False)


VBA Code:
Dim sn$, olapp As Outlook.Application
' Excel module
Private Sub Embedder()
Dim olNameSpace As Namespace, olFolder  As MAPIFolder, olapp As Outlook.Application, _
o As OLEObject, ci As Object
Set olapp = New Outlook.Application
Set olNameSpace = olapp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
On Error Resume Next
Set ci = olapp.ActiveInspector.CurrentItem                                          ' current message
If TypeName(ci) = "Nothing" Then Set ci = olapp.ActiveExplorer.Selection.Item(1)    ' or selected one
On Error GoTo 0
SaveMessageAsMsg ci
Set o = ActiveSheet.OLEObjects.Add(FileName:=sn, Link:=False, DisplayAsIcon:=False)
'Kill sn                                                                             ' delete file
Set olapp = Nothing
End Sub

Sub SaveMessageAsMsg(Item As MailItem)
'Enviro = CStr(Environ("USERPROFILE"))
sn = Item.Subject
ReplaceChars sn, "-"
sn = "C:\Users\Public\Desktop\vba\" & sn & ".msg"
Item.SaveAs sn, olMSG                       ' save to disk
End Sub

Private Sub ReplaceChars(ByVal nam$, sChr$)
  nam = Replace(nam, "'", sChr):  nam = Replace(nam, "*", sChr)
  nam = Replace(nam, "/", sChr):  nam = Replace(nam, "\", sChr)
  nam = Replace(nam, ":", sChr)
  nam = Replace(nam, "?", sChr)
  nam = Replace(nam, Chr(34), sChr)
  nam = Replace(nam, "<", sChr)
  nam = Replace(nam, ">", sChr)
  nam = Replace(nam, "|", sChr)
End Sub
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top