PDA

View Full Version : SelectAll in VBA or copy the body



gijsjgsj
10-24-2011, 10:04 AM
I need to copy all of the body of a particular email into Excel.

I can get my code as far as opening up the new email using 'Item.Display' I now want to run the SelectAll function (Ctrl+A or from the Message tab in the ribbon) but I don't know how to do this in code.

Alternatively how do I just copy the body of the email to the clipboard?

Everything I can find on this just inserts it into one cell, I want the macro to behave as though I'd selected all of the message, copied and pasted into a new worksheet.

Thanks

monarchd
10-25-2011, 12:55 PM
There's some nice code at Mr.Excel by jimrward (in thread called Macro to list all mails present in my outlook inbox. Thread # 503118. I can't post links yet.) which takes the direction of pulling Outlook emails into Excel. Toward the bottom you can see the different things it's pulling into the columns.

I made a minor adjustment to get the Body of the message, see if this is what you're going for.

Sub GetMail()
Dim OLF As Outlook.MAPIFolder, CurrUser As String
Dim EmailItemCount As Integer, i As Integer, EmailCount As Integer
Dim FileName As String
Application.ScreenUpdating = False
Workbooks.Add
Cells(1, 1).Formula = "Subject"
Cells(1, 2).Formula = "Message Body"
Cells(1, 3).Formula = "Received"
Cells(1, 4).Formula = "Attachments"
Cells(1, 5).Formula = "Sender Name"
Cells(1, 6).Formula = "Sender Email Address"
With Range("A1:F1").Font
.Bold = True
.Size = 14
End With
Columns("A:F").AutoFit
Application.Calculation = xlCalculationManual
Set OLF = GetObject("", _
"Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
EmailItemCount = OLF.Items.count
i = 0: EmailCount = 0
'read email info
While i < EmailItemCount
i = i + 1
If i Mod 50 = 0 Then Application.StatusBar = "Reading email messages " & _
Format(i / EmailItemCount, "0%") & "..."
With OLF.Items(i)
EmailCount = EmailCount + 1
Cells(EmailCount + 1, 1).Formula = .Subject
Cells(EmailCount + 1, 2).Formula = .Body
Cells(EmailCount + 1, 3).Formula = Format(.ReceivedTime, "dd.mm.yyyy hh:mm")
Cells(EmailCount + 1, 4).Formula = .Attachments.count
Cells(EmailCount + 1, 5).Formula = .SenderName
Cells(EmailCount + 1, 6).Formula = .SenderEmailAddress
End With
Wend
Application.Calculation = xlCalculationAutomatic
Set OLF = Nothing
Range("A2").Select
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub