Outlook export multiple emails in Excel workbook but different excel worksheets

%3CLINGO-SUB%20id%3D%22lingo-sub-2031967%22%20slang%3D%22en-US%22%3EOutlook%20export%20multiple%20emails%20in%20Excel%20workbook%20but%20different%20excel%20worksheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2031967%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20new%20using%20vba%2C%20I%20want%20to%20export%20from%20outlook%20emails%20selected%20into%20a%20workbook%20path%2C%20and%20each%20email%20(subject%2C%20body%2Cetc.)%20should%20be%20store%20in%20different%20worksheet%20and%20I'm%20trying%20to%20edit%20this%20macro%20because%20its%20almost%20what%20I%20need%3CA%20href%3D%22https%3A%2F%2Fexonlinecalculator.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%2C%3C%2FA%3E%20especialy%20the%20part%20of%20olFormatHTML%20and%20WordEditor%2C%20because%20of%20split%3C%2FP%3E%3CP%3EThe%20idea%20is%3CBR%20%2F%3E1.-select%20multiple%20emails%20in%20Outlook%3CBR%20%2F%3E2.-open%20file%20path%3CBR%20%2F%3E3.-for%20each%20email%20selected%20in%20Outlook%20will%20be%20stored%20in%20a%20single%20worksheet%20from%20file%20opened%3C%2FP%3E%3CP%3EI'm%20getting%20issue%20with%20the%20macro%20in%20this%203rth%20part%3CBR%20%2F%3EA).-%20from%20selected%20items%20the%20macro%20do%20loop%20and%20just%20take%20the%20first%20email%20selected%3CBR%20%2F%3EB).-%20the%20emails%20store%20in%20different%20workbooks%2C%20should%20be%20stored%20in%20the%20same%20workbook%20that%20I%20opened%3C%2FP%3E%3CP%3Ehere%20is%20the%20code%3C%2FP%3E%3CP%3EPublic%20Sub%20SplitEmail()%3C%2FP%3E%3CP%3EDim%20rpl%20As%20Outlook.MailItem%3CBR%20%2F%3EDim%20itm%20As%20Object%3CBR%20%2F%3EDim%20sPath%20As%20String%2C%20sFile%20As%20String%3CBR%20%2F%3EDim%20objDoc%20As%20Word.Document%3CBR%20%2F%3EDim%20txt%20As%20String%3CBR%20%2F%3EDim%20xlApp%20As%20Excel.Application%3CBR%20%2F%3EDim%20wb%20As%20Excel.Workbook%3CBR%20%2F%3EDim%20i%20As%20Long%3CBR%20%2F%3EDim%20x%20As%20Long%3CBR%20%2F%3E'----------------------------%3CBR%20%2F%3EDim%20myOlExp%20As%20Outlook.Explorer%3CBR%20%2F%3EDim%20myOlSel%20As%20Outlook.Selection%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20myOlExp%20%3D%20Application.ActiveExplorer%3CBR%20%2F%3ESet%20myOlSel%20%3D%20myOlExp.Selection%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20x%20%3D%201%20To%20myOlSel.Count%3CBR%20%2F%3E%3CBR%20%2F%3E'----------------------------------------------%3CBR%20%2F%3ESet%20itm%20%3D%20GetCurrentItem()%20'A)I%20think%20the%20issuefrom%20selecting%201%20item%20is%20located%20here%3C%2FP%3E%3CP%3E'%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%3C%2FP%3E%3CP%3EsPath%20%3D%20%22C%3A%5CUsers%5CRay%5C%22%3CBR%20%2F%3EsFile%20%3D%20sPath%20%26amp%3B%20%22Macro.xlsm%22%3C%2FP%3E%3CP%3EIf%20Not%20itm%20Is%20Nothing%20Then%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rpl%20%3D%20itm.Reply%3CBR%20%2F%3Erpl.BodyFormat%20%3D%20olFormatHTML%3CBR%20%2F%3E'rpl.Display%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20objDoc%20%3D%20rpl.GetInspector.WordEditor%3CBR%20%2F%3Etxt%20%3D%20objDoc.Content.Text%3C%2FP%3E%3CP%3E'%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20xlApp%20%3D%20CreateObject(%22Excel.application%22)%3CBR%20%2F%3ExlApp.Visible%20%3D%20True%3CBR%20%2F%3ESet%20wb%20%3D%20xlApp.Workbooks.Open(sFile)%20'B)%20tried%20to%20move%20it%20to%20the%20beginning%20and%20macro%20doesn't%20work%3CBR%20%2F%3E%3CBR%20%2F%3E'%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%3C%2FP%3E%3CP%3EFor%20i%20%3D%20LBound(Split(txt%2C%20Chr(13))%2C%201)%20To%20UBound(Split(txt%2C%20Chr(13))%2C%201)%3CBR%20%2F%3Ewb.Worksheets(x).Range(%22A%22%20%26amp%3B%20i%20%2B%201).Value%20%3D%20Split(txt%2C%20Chr(13))(i)%20'B)emails%20in%20diferrent%20sheet%20but%20no%20same%20workbook%3C%2FP%3E%3CP%3ENext%20i%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E'------------------------------------------------------%3CBR%20%2F%3ENext%20x%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EFunction%20GetCurrentItem()%20As%20Object%3C%2FP%3E%3CP%3EDim%20objApp%20As%20Outlook.Application%3CBR%20%2F%3ESet%20objApp%20%3D%20Application%3CBR%20%2F%3EOn%20Error%20Resume%20Next%3C%2FP%3E%3CP%3ESelect%20Case%20TypeName(objApp.ActiveWindow)%3CBR%20%2F%3ECase%20%22Explorer%22%3CBR%20%2F%3ESet%20GetCurrentItem%20%3D%20objApp.ActiveExplorer.Selection.item(1)%3CBR%20%2F%3ECase%20%22Inspector%22%3CBR%20%2F%3ESet%20GetCurrentItem%20%3D%20objApp.ActiveInspector.CurrentItem%3CBR%20%2F%3EEnd%20Select%3CBR%20%2F%3EGetCurrentItem.UnRead%20%3D%20False%3CBR%20%2F%3ESet%20objApp%20%3D%20Nothing%3C%2FP%3E%3CP%3EEnd%20Function%3CBR%20%2F%3Ethis%20issue%20is%20B%3A%20The%20code%20above%2C%20when%20macro%20do%20loop%20%22x%22%20increase%2C%20the%20email%20store%20in%20different%20sheets%20but%20in%20different%20workbooks%2C%20should%20be%20in%20the%20same%20workbook%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2066751%22%20slang%3D%22en-US%22%3ERe%3A%20Outlook%20export%20multiple%20emails%20in%20Excel%20workbook%20but%20different%20excel%20worksheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2066751%22%20slang%3D%22en-US%22%3E%3CP%3EHI%20beautiful%20people%2C%20if%20anyone%20facing%20same%20problem%2C%20you%20can%20try%20this%20solution%20i%20got%20from%20an%20expert.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20made%20an%20update%20to%20this%20macro%2C%20tell%20me%20%3CA%20href%3D%22https%3A%2F%2Fyesornogenerator.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Eyes%20or%20no%3C%2FA%3E%20if%20its%20helpful%3CBR%20%2F%3Eas%20macro%20do%20loop%20in%20For%20x%20it%20open%20the%20file%20x%20times%2C%3CBR%20%2F%3Eand%20then%20close%20it%20and%20open%20again%20instead%20of%20working%20on%20the%20first%20workbook%20opened%3CBR%20%2F%3Ebut%20the%20macro%20leaves%20open%20instances%3CBR%20%2F%3Ehere%20is%20the%20current%20code%3C%2FP%3E%3CP%3EPublic%20Sub%20SplitEmail()%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EDim%20rpl%20As%20Outlook.MailItem%3CBR%20%2F%3EDim%20itm%20As%20Object%3CBR%20%2F%3EDim%20sPath%20As%20String%2C%20sFile%20As%20String%3CBR%20%2F%3EDim%20objDoc%20As%20Word.Document%3CBR%20%2F%3EDim%20txt%20As%20String%3CBR%20%2F%3EDim%20xlApp%20As%20Excel.Application%3CBR%20%2F%3EDim%20wb%20As%20Excel.Workbook%3CBR%20%2F%3EDim%20i%20As%20Long%3CBR%20%2F%3EDim%20x%20As%20Long%3CBR%20%2F%3E'----------------------------%3CBR%20%2F%3EDim%20myOlExp%20As%20Outlook.Explorer%3CBR%20%2F%3EDim%20myOlSel%20As%20Outlook.Selection%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20myOlExp%20%3D%20Application.ActiveExplorer%3CBR%20%2F%3ESet%20myOlSel%20%3D%20myOlExp.Selection%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20x%20%3D%201%20To%20myOlSel.Count%3CBR%20%2F%3E%3CBR%20%2F%3E'----------------------------------------------%3CBR%20%2F%3E%3CBR%20%2F%3EDim%20objApp%20As%20Outlook.Application%3CBR%20%2F%3EDim%20GetCurrentItem%20As%20Object%3CBR%20%2F%3ESet%20objApp%20%3D%20Application%3CBR%20%2F%3EOn%20Error%20Resume%20Next%3CBR%20%2F%3E%3CBR%20%2F%3ESelect%20Case%20TypeName(objApp.ActiveWindow)%3CBR%20%2F%3ECase%20%22Explorer%22%3CBR%20%2F%3ESet%20GetCurrentItem%20%3D%20objApp.ActiveExplorer.Selection.item(x)%3CBR%20%2F%3ECase%20%22Inspector%22%3CBR%20%2F%3ESet%20GetCurrentItem%20%3D%20objApp.ActiveInspector.CurrentItem%3CBR%20%2F%3EEnd%20Select%3CBR%20%2F%3E%3CBR%20%2F%3EGetCurrentItem.UnRead%20%3D%20False%3CBR%20%2F%3ESet%20objApp%20%3D%20Nothing%3CBR%20%2F%3E%3CBR%20%2F%3E'-----------------------------------------------%3CBR%20%2F%3ESet%20itm%20%3D%20GetCurrentItem%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EsPath%20%3D%20%22C%3A%5CUsers%5CRay%5C%22%3CBR%20%2F%3EsFile%20%3D%20sPath%20%26amp%3B%20%22Macro.xlsm%22%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20itm%20Is%20Nothing%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E'de%20lo%20contrario%2C%20se%20crea%20un%20Reply%20del%20correo%20en%20formato%20HTML%3CBR%20%2F%3ESet%20rpl%20%3D%20itm.Reply%3CBR%20%2F%3Erpl.BodyFormat%20%3D%20olFormatHTML%3CBR%20%2F%3E'rpl.Display%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20objDoc%20%3D%20rpl.GetInspector.WordEditor%3CBR%20%2F%3Etxt%20%3D%20objDoc.Content.Text%3C%2FP%3E%3CP%3E'%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20xlApp%20%3D%20CreateObject(%22Excel.application%22)%3CBR%20%2F%3ExlApp.Visible%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20wb%20%3D%20xlApp.Workbooks.Open(sFile)%3CBR%20%2F%3ExlApp.Windows(%22Macro.xlsm%22).Activate%3CBR%20%2F%3E'Set%20wb%20%3D%20ActiveWorkbook%3CBR%20%2F%3E'%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EFor%20i%20%3D%20LBound(Split(txt%2C%20Chr(13))%2C%201)%20To%20UBound(Split(txt%2C%20Chr(13))%2C%201)%3CBR%20%2F%3Ewb.Worksheets(x).Range(%22A%22%20%26amp%3B%20i%20%2B%201).Value%20%3D%20Split(txt%2C%20Chr(13))(i)%3CBR%20%2F%3ENext%20i%3CBR%20%2F%3E%3CBR%20%2F%3ExlApp.Windows(%22Macro.xlsm%22).Close%20SaveChanges%3A%3DTrue%3CBR%20%2F%3ExlApp.Workbook.Close%20SaveChanges%3A%3DFalse%3CBR%20%2F%3E'------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENext%20x%3CBR%20%2F%3E'------------------------------------------------------%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E'the%20instances%20should%20closed%20but%20not%20working%2C%20instances%20are%20empty%3C%2FP%3E%3CP%3EFor%20Each%20wb%20In%20xlApp%3CBR%20%2F%3Ewb.Close%20SaveChanges%3A%3DFalse%3CBR%20%2F%3ENext%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I'm new using vba, I want to export from outlook emails selected into a workbook path, and each email (subject, body,etc.) should be store in different worksheet and I'm trying to edit this macro because its almost what I need, especialy the part of olFormatHTML and WordEditor, because of split

The idea is
1.-select multiple emails in Outlook
2.-open file path
3.-for each email selected in Outlook will be stored in a single worksheet from file opened

I'm getting issue with the macro in this 3rth part
A).- from selected items the macro do loop and just take the first email selected
B).- the emails store in different workbooks, should be stored in the same workbook that I opened

here is the code

Public Sub SplitEmail()

Dim rpl As Outlook.MailItem
Dim itm As Object
Dim sPath As String, sFile As String
Dim objDoc As Word.Document
Dim txt As String
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim i As Long
Dim x As Long
'----------------------------
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection

Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection

For x = 1 To myOlSel.Count

'----------------------------------------------
Set itm = GetCurrentItem() 'A)I think the issuefrom selecting 1 item is located here

'|||||||||||||||||||||||||||||||||||||||||

sPath = "C:\Users\Ray\"
sFile = sPath & "Macro.xlsm"

If Not itm Is Nothing Then

Set rpl = itm.Reply
rpl.BodyFormat = olFormatHTML
'rpl.Display
End If

Set objDoc = rpl.GetInspector.WordEditor
txt = objDoc.Content.Text

'||||||||||||||||||||||||||||||||||||||||||||||

Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open(sFile) 'B) tried to move it to the beginning and macro doesn't work

'||||||||||||||||||||||||||||||||||||||||||||||

For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
wb.Worksheets(x).Range("A" & i + 1).Value = Split(txt, Chr(13))(i) 'B)emails in diferrent sheet but no same workbook

Next i


'------------------------------------------------------
Next x

End Sub


Function GetCurrentItem() As Object

Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next

Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
GetCurrentItem.UnRead = False
Set objApp = Nothing

End Function
this issue is B: The code above, when macro do loop "x" increase, the email store in different sheets but in different workbooks, should be in the same workbook

1 Reply

HI beautiful people, if anyone facing same problem, you can try this solution i got from an expert.

 

I made an update to this macro, tell me yes or no if its helpful
as macro do loop in For x it open the file x times,
and then close it and open again instead of working on the first workbook opened
but the macro leaves open instances
here is the current code

Public Sub SplitEmail()


Dim rpl As Outlook.MailItem
Dim itm As Object
Dim sPath As String, sFile As String
Dim objDoc As Word.Document
Dim txt As String
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim i As Long
Dim x As Long
'----------------------------
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection

Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection

For x = 1 To myOlSel.Count

'----------------------------------------------

Dim objApp As Outlook.Application
Dim GetCurrentItem As Object
Set objApp = Application
On Error Resume Next

Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(x)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

GetCurrentItem.UnRead = False
Set objApp = Nothing

'-----------------------------------------------
Set itm = GetCurrentItem


sPath = "C:\Users\Ray\"
sFile = sPath & "Macro.xlsm"


If Not itm Is Nothing Then

'de lo contrario, se crea un Reply del correo en formato HTML
Set rpl = itm.Reply
rpl.BodyFormat = olFormatHTML
'rpl.Display
End If


Set objDoc = rpl.GetInspector.WordEditor
txt = objDoc.Content.Text

'||||||||||||||||||||||||||||||||||||||||||||||

Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True

Set wb = xlApp.Workbooks.Open(sFile)
xlApp.Windows("Macro.xlsm").Activate
'Set wb = ActiveWorkbook
'||||||||||||||||||||||||||||||||||||||||||||||

 


For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
wb.Worksheets(x).Range("A" & i + 1).Value = Split(txt, Chr(13))(i)
Next i

xlApp.Windows("Macro.xlsm").Close SaveChanges:=True
xlApp.Workbook.Close SaveChanges:=False
'------------------------------------------------------

 

Next x
'------------------------------------------------------


'the instances should closed but not working, instances are empty

For Each wb In xlApp
wb.Close SaveChanges:=False
Next


End Sub