Option
Explicit
Private
Const
xlUp
As
Long
= -4162
Sub
CopyAllMessagesToExcel()
Dim
objOL
As
Outlook.Application
Dim
objItems
As
Outlook.Items
Dim
objFolder
As
Outlook.MAPIFolder
Dim
olItem
As
Outlook.MailItem
Dim
xlApp
As
Object
Dim
xlWB
As
Object
Dim
xlSheet
As
Object
Dim
vText, vText2, vText3, vText4, vText5
As
Variant
Dim
sText
As
String
Dim
rCount
As
Long
Dim
bXStarted
As
Boolean
Dim
enviro
As
String
Dim
strPath
As
String
Dim
Reg1
As
Object
Dim
M1
As
Object
Dim
M
As
Object
enviro =
CStr
(Environ(
"USERPROFILE"
))
strPath = enviro &
"\Desktop\neu.xlsx"
On
Error
Resume
Next
Set
xlApp = GetObject(,
"Excel.Application"
)
If
Err <> 0
Then
Application.StatusBar =
"Please wait while Excel source is opened ... "
Set
xlApp = CreateObject(
"Excel.Application"
)
bXStarted =
True
End
If
On
Error
GoTo
0
Set
xlWB = xlApp.Workbooks.Open(strPath)
Set
xlSheet = xlWB.Sheets(
"Tabelle1"
)
rCount = xlSheet.Range(
"B"
& xlSheet.Rows.Count).
End
(xlUp).Row
rCount = rCount + 1
Set
objOL = Outlook.Application
Set
objFolder = objOL.ActiveExplorer.CurrentFolder
Set
objItems = objFolder.Items
For
Each
olItem
In
objItems
On
Error
Resume
Next
With
olItem
sText = olItem.Body
Set
Reg1 = CreateObject(
"VBScript.RegExp"
)
With
Reg1
.MultiLine =
True
.Pattern =
"(\bLieferadresse\n (\w+).*)"
End
With
If
Reg1.Test(sText)
Then
Set
M1 = Reg1.Execute(sText)
For
Each
M
In
M1
vText = Trim(M.SubMatches(1))
vText2 = Trim(M.SubMatches(2))
Next
xlSheet.Range(
"B"
& rCount) = vText
xlSheet.Range(
"c"
& rCount) = vText2
xlSheet.Range(
"d"
& rCount) = .Subject
xlSheet.Range(
"e"
& rCount) = .ReceivedTime
rCount = rCount + 1
End
If
Debug.Print .Subject
End
With
Next
xlWB.Close 1
If
bXStarted
Then
xlApp.Quit
End
If
Set
M =
Nothing
Set
M1 =
Nothing
Set
Reg1 =
Nothing
Set
xlApp =
Nothing
Set
xlWB =
Nothing
Set
xlSheet =
Nothing
Set
objItems =
Nothing
Set
objFolder =
Nothing
Set
objOL =
Nothing
End
Sub