email to excel problem

mmenofy

New Member
Joined
Apr 30, 2019
Messages
23
hi guys,

I am using the following code to save e-mails to an excel sheet, but I noticed that when e-mail comes while I am opening the excel sheet (any excel), it closes any opened excel and don't save (only saving the email tracking found in the VBA code)

please help

the code:
Sub ExportToExcelclient(MyMail As MailItem)
Dim strID As String, olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim strFileName As String
'~~> Excel Variables
Dim oXLApp As Object, oXLwb As Object, oXLws As Object
Dim lRow As Long
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
DoEvents
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
DoEvents
'~~> Show Excel
oXLApp.Visible = False
'~~> Open the relevant file

Set oXLwb = oXLApp.Workbooks.Open("D:\E-MOVE\OUT-MAILS.xlsb")
'~~> Set the relevant output sheet. Change as applicable
Set oXLws = oXLwb.Sheets("FROM-MAIL")
lRow = oXLws.Range("A" & oXLApp.Rows.Count).End(xlUp).Row + 1
'~~> Write to outlook
With oXLws
'
'~~> Code here to output data from email to Excel File
'~~> For example
'
.Range("A" & lRow).Value = olMail.Subject
' .Range("B" & lRow).Value = olMail.SenderName
.Range("B" & lRow).Value = olMail.ReceivedTime
.Range("C" & lRow).Value = olMail.Body
'
End With
'~~> Close and Clean up Excel
On Error Resume Next
oXLwb.Sheets("FROM-MAIL").ShowAllData
On Error GoTo 0
oXLwb.Close savechanges:=True
Set oXLwb = oXLApp.Workbooks.Open("D:\E-MOVE\OUT-MAILS.xlsb")
Set oXLws = Nothing
Set oXLwb = Nothing
Set oXLApp = Nothing
Set olMail = Nothing
Set olNS = Nothing
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,330
Messages
6,124,305
Members
449,150
Latest member
NyDarR

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