Trying to find code to copy specific text from emails in a specific Outlook folder to Excel.
Found this code that works for me, but pastes my data into Word.
Is it possible to change it to paste into Excel? It's all a bit beyond my knowledge...
Any help appreciated
Found this code that works for me, but pastes my data into Word.
Is it possible to change it to paste into Excel? It's all a bit beyond my knowledge...
Code:
Option Explicit
'Create this macro in Word
'It requires a reference in vba tools > references
'to the Outlook object library e.g. for Outlook 2007
'Microsoft Outlook 12.0 object library
Sub ExtractOLMessage()
Dim sFname As String
Dim i As Long
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim oDoc As Document
Dim oTable As Table
Dim oRow As Row
Dim sText As String
Dim strName As String
Dim strLocation As String
Dim strDept As String
Dim bStarted As Boolean
Dim vText As Variant
Dim sDate As String
Dim sDay As String
Dim sMonth As String
Dim sYear As String
bStarted = False 'Set a flag
'Document containing the table
sFname = "E:\_Word stuff\MailTest\MailTest.docx"
'If the document is open, set it as the active document
If ActiveDocument.FullName = sFname Then
Set oDoc = ActiveDocument
Else 'otherwise open it
Set oDoc = Documents.Open(FileName:=sFname)
bStarted = True 'And set the flag to true
End If
Set oTable = oDoc.Tables(1)
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err = 429 Then 'Outlook is closed so open it
Set olApp = CreateObject("Outlook.Application")
End If
Set olNs = olApp.GetNamespace("MAPI")
'Indicate which Outlook folder to access
Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders("MailTest")
'Indicate the last message
Set olItem = olFolder.Items(olFolder.Items.Count)
'Get the text of the message
sText = olItem.Body
'and split it by paragraph
vText = Split(sText, Chr(13))
'and split it by colon (ASCII character 58)
'vText = Split(sText, Chr(58))
'Examine each paragraph
For i = 1 To UBound(vText)
'and locate the text relating to the item required
If InStr(1, vText(i), "be joining the company") Then
'The Name we want is in the 1st paragraph so we add 0
strName = vText(i + 0)
'The Location we want is in the 3rd paragraph, so we add 3
strLocation = vText(i + 3)
'The Location we want is in the 5th paragraph, so we add 5
strDept = vText(i + 5)
'Log the date the message was sent
sDate = Format(olItem.SentOn, "dd.MM.yyyy")
'The entry has been found so stop looking for it
Exit For
End If
Next i
'Mark the message as read
olItem.UnRead = False
'Then clear the Outlook variables
Set olItem = Nothing
Set olItem = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
'Add another row to the table
Set oRow = oTable.Rows.Add
'and fill the cells in that row with the extracted data
oRow.Cells(1).Range = sDate
oRow.Cells(2).Range = strName
oRow.Cells(2).Range = Replace(oRow.Cells(2).Range, Chr(13), "")
oRow.Cells(3).Range = strLocation
oRow.Cells(3).Range = Replace(oRow.Cells(3).Range, Chr(13), "")
oRow.Cells(4).Range = strDept
oRow.Cells(4).Range = Replace(oRow.Cells(4).Range, Chr(13), "")
'Establish whether the date is a Saturday or a Sunday (don't need this but not sure what to get rid of)
sMonth = MonthName(Mid(sDate, 4, 2))
sDay = Left(sDate, 2)
sYear = Right(sDate, 4)
sDate = sDay & Chr(32) & sMonth & Chr(32) & sYear
sDate = Weekday(sDate)
If sDate = 1 Or sDate = 7 Then 'it is a weekend
'So colour the date cell
oRow.Cells(1).Range.Shading.BackgroundPatternColor = -654245991
Else 'it is not a weekend so leave it white
oRow.Cells(1).Range.Shading.BackgroundPatternColor = -603914241
End If
Application.ScreenRefresh
'this is my bit is to 'clean' the data in Word, to leave what I need:
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Location: "
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Department: "
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ", will be joining the company with effect from "
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Further details are as follows:"
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^#^#-^?^?^?-^#^#."
.Replacement.Text = ""
'my 'cleaning' bit ends here
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Collapse
If bStarted = True Then 'The document was opened by the macro so save it and close
oDoc.Close SaveChanges:=wdSaveChanges
End If
Set oDoc = Nothing
End Sub