copy outlook body into excel without leaving column space

SamSan78

New Member
Joined
Sep 20, 2020
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

my object is to copy information from an email body into excel. the email has the data in different lines which needs to be updated in an excel file exactly in the same order.

i have the following code which is doing that however it is leaving one column blank for every carrage return from the email

the information in email is like the following

this is me
this is my test
this is my result

the above needs to be put into column A,B, and c of the worksheet, however it saves in column A,C,E

please help

Dim wb As Workbook Dim wks As Worksheet Dim ObjOutlook As Object Dim MyNamespace As Object Dim i As Integer Dim j As Long Dim abody() As String Dim rstart As Range Set wb = Workbooks.Open("C:\Users\s213581\Documents\Test\EmailBody.xlsx") Set wks = wb.Worksheets("Test") Set ObjOutlook = GetObject(, "Outlook.Application") Set MyNamespace = ObjOutlook.GetNamespace("MAPI") For i = 1 To MyNamespace.GetDefaultFolder(6).Folders("HQ Dashboard").Items.Count abody = Split(MyNamespace.GetDefaultFolder(6).Folders("HQ Dashboard").Items(i).Body, Chr(13) & Chr(10)) Set rstart = wks.Cells(65000, 1).End(xlUp).Offset(1, 0) For j = 0 To UBound(abody) If Len(abody(j)) > 1 Then rstart.Offset(0, j).Value = abody(j) End If Next j Next i wb.Close True
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to MrExcel forums - but please use VBA code tags.

Try replacing the For j .... Next j loop with:
VBA Code:
  Dim colOffset As Long
  colOffset = 0
  For j = 0 To UBound(abody)
    If Len(abody(j)) > 1 Then
      rstart.Offset(0, colOffset).Value = abody(j)
      colOffset = colOffset + 1
    End If
  Next j
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,576
Members
448,972
Latest member
Shantanu2024

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