2019 Mar 26 1:48 PM
Dear Experts,
I use Outlook to read subjects form the E-Mails into a internal table and It works fine!
At the end of the day, there are 2 problems left and I hope, somebody can help me:
1) I need to know the amount of E-Mails in the Standard-Inbox.
2) I want to move the E-Mails from Standort-Inbox into a subfolder named "NEW".
Microsoft says, that Outlook has "Count-" and "Move-Methods". But how to use this methods in ABAP?
Best regards,
Martin
2019 Apr 03 4:00 PM
Here is the equivalence between your VBA code and the ABAP code:
Sub MoveItems() FORM moveitems.
Dim OutlookApp As Outlook.Application DATA outlookapp TYPE ole2_object.
Dim myNamespace As Outlook.NameSpace DATA mynamespace TYPE ole2_object.
Dim myFolder As Outlook.MAPIFolder DATA myfolder TYPE ole2_object.
Dim destFolder As Outlook.MAPIFolder DATA destfolder TYPE ole2_object.
Dim count As Long, n As Long DATA count TYPE i.
Dim movItems As Outlook.Items DATA movitems TYPE ole2_object.
Dim mItem As Object DATA mitem TYPE ole2_object.
DATA n TYPE i.
Set OutlookApp = New Outlook.Application CREATE OBJECT outlookapp 'Outlook.Application'.
Set myNamespace = OutlookApp.GetNamespace("MAPI") CALL METHOD OF outlookapp 'GetNameSpace' = mynamespace
EXPORTING #1 = 'MAPI'.
' Default Inbox
Set myFolder = myNamespace.GetDefaultFolder(6) CALL METHOD OF mynamespace 'GetDefaultFolder'
= myfolder EXPORTING #1 = 6.
' Subfolder of Default Inbox
Set destFolder = myFolder.Folders("NEW") CALL METHOD OF myfolder 'Folders' = destfolder
EXPORTING #1 = 'NEW'.
Set movItems = myFolder.Items GET PROPERTY OF myfolder 'Items' = movitems.
count = movItems.count GET PROPERTY OF movitems 'Count' = count.
For n = count To 1 Step-1 DO count TIMES.
n = count - sy-index + 1.
movItems(n).Move destFolder CALL METHOD OF myfolder 'Items' = mitem
EXPORTING #1 = n.
CALL METHOD OF mitem 'Move' EXPORTING #1
= destfolder.
Next n ENDDO.
End Sub ENDFORM.
2019 Mar 26 6:09 PM
First ask a outlook vba forum, and post the vba code here, that will help to convert it into abap!
2019 Mar 28 2:50 PM
2019 Mar 28 3:16 PM
Martin Grischkat Thanks but it's full of error syntaxes when I paste it in Outlook. Are you sure this is the one which does what you want?
Please use the CODE button to format your code so that it's more user-friendly.
2019 Mar 28 3:43 PM
Sorry, my fault.
Here is the code I need in ABAP (works fine in VBA, Early Binding):
Sub MoveItems()
Dim OutlookApp As Outlook.Application
Set OutlookApp = New Outlook.Application
Dim myNamespace As Outlook.NameSpace
Set myNamespace = OutlookApp.GetNamespace("MAPI")
Dim myFolder As Outlook.MAPIFolder
Dim destFolder As Outlook.MAPIFolder
Dim count As Long, n As Long
Dim movItems As Outlook.Items
Dim mItem As Object
' Definition of Source- and Target-Folders
Set myFolder = myNamespace.GetDefaultFolder(6) ' Default Inbox
Set destFolder = myFolder.Folders("NEW") ' Subfolder of Default Inbox
Set movItems = myFolder.Items
count = movItems.count
For n = count To 1 Step -1
movItems(n).Move destFolder
Next n
End Sub
2019 Mar 28 4:01 PM
Why don't you first create and test the VBA code you need? Then I can translate it into a working version? With the code above, maybe the resulting ABAP won't include all VBA-to-ABAP conversion rules you need. Or if you prefer, do the conversion yourself by following the advices I gave here: https://answers.sap.com/questions/287201/como-eliminar-filas-vacias-en-ole-abap.html
2019 Mar 28 5:37 PM
Dear Sandra,
thanks for your patience 😉 I have edited my code above.
2019 Apr 03 2:48 PM
I found out for myself. Here is my solution, which I have to integrate now in my main-program:
REPORT Z_TEST2.
INCLUDE: ole2incl.
TYPES: BEGIN OF type_email,
mailsen TYPE c LENGTH 1024, "+ E-Mail Absender
mailhdc TYPE c LENGTH 1024, "+ E-Mail Header komplett inkl. Ticket-Nr. und Timestemp
mailhdr TYPE c LENGTH 2048, "+ E-Mail Header Raw (Original)
maildat TYPE string, "+ E-Mail Datum
mailtim TYPE string, "+ E-Mail Uhrzeit
mailjah TYPE c LENGTH 4, "+ E-Mail Jahr
END OF type_email.
*###############################################################################
DATA: l_oleoutlook TYPE ole2_object,
l_olenmspace TYPE ole2_object,
l_oleeingang TYPE ole2_object, "+ Posteingang
l_olemainpth TYPE ole2_object, "+ Zielordner
l_oleemail01 TYPE ole2_object, "+ E-Mail
l_oledtarget TYPE ole2_object,
l_hdrrawvers TYPE c LENGTH 2048, "+ Header RAW (Original)
l_laengeabs1 TYPE i, "+ Länge des Absendernames (um leere Datensätze festzustellen)
l_dszaehler1 TYPE sy-tabix, "+ Zähler für die E-Mails
l_origdatum1 TYPE string, "+ Original-Datum aus E-Mail
l_pfadmailcp TYPE string, "+ Lokals Windows-Verzeichnis für die gespeicherten E-Mails
l_sendername TYPE string, "+ Absender der E-Mail
l_hdrcomplet TYPE string, "+ Header inkl. Ticket-Nr. und Timestamp
l_timestamp1 TYPE string, "+ Timestamp
l_sendreceiv TYPE i. "+ Flag, ob "R" (E-Mail-Eingang) oder "S" (Gesendete E-Mail) ausgelesen werden soll
*+ Variablen (für Erstellung Timestamp):
DATA: l_emaildatum TYPE string, "+ Datum für interne Tabelle it_emails
l_emailuhrze TYPE string, "+ Uhrzeit für interne Tabelle it_emails
l_tempjahr01 TYPE c LENGTH 4, "+ Jahr
l_tempmonat1 TYPE c LENGTH 2, "+ Monat
l_temptag001 TYPE c LENGTH 2, "+ Tag
l_tempstunde TYPE c LENGTH 2, "+ Stunde
l_tempminute TYPE c LENGTH 2, "+ Minute
l_tempsekund TYPE c LENGTH 2. "+ Sekunde
DATA: lit_emails TYPE STANDARD TABLE OF type_email,
w_lit_emails TYPE type_email.
*###############################################################################
"+ Wird nur zu Testzwecken benötigt:
************************************************************
PARAMETERS: p_pronum TYPE string OBLIGATORY. "+ Projekt-Nr.
PARAMETERS: p_sndrec TYPE c DEFAULT 'R' OBLIGATORY. "+ Eingang / Gesendet
************************************************************
START-OF-SELECTION.
TRANSLATE p_sndrec TO UPPER CASE.
IF p_sndrec NE 'R' AND p_sndrec NE 'S'.
MESSAGE 'R für Erhalten oder S für Gesendet eingeben' TYPE 'E'.
ENDIF.
IF p_sndrec EQ 'R'.
l_sendreceiv = 6.
ELSE.
l_sendreceiv = 5.
ENDIF.
REFRESH lit_emails.
CREATE OBJECT l_oleoutlook 'Outlook.Application'.
CALL METHOD OF l_oleoutlook 'GetNameSpace' = l_olenmspace
EXPORTING
#1 = 'MAPI'.
CALL METHOD OF l_olenmspace 'GetDefaultFolder' = l_oleeingang
EXPORTING
#1 = l_sendreceiv. "3:Deleted items - 4:Outbox - 5:Sent Items - 6:Inbox - 9:Calendar - 10:Contacts - 11:Journal - 12:Notes - 13:Tasks
CALL METHOD OF l_olenmspace 'Folders' = l_olemainpth
EXPORTING
#1 = 'Persönlicher Ordner'.
CALL METHOD OF l_olemainpth 'Folders' = l_oledtarget
EXPORTING
#1 = '_Tickets'.
ULINE.
CLEAR: l_dszaehler1.
DO.
l_dszaehler1 = l_dszaehler1 + 1.
CALL METHOD OF l_oleeingang 'Items' = l_oleemail01 "+ Zugriff auf E-Mail Nr. l_dszaehler1
EXPORTING
#1 = l_dszaehler1.
IF sy-subrc = 0.
GET PROPERTY OF l_oleemail01 'SenderName' = l_sendername. "+ Absender
CONDENSE l_sendername.
MOVE Strlen( l_sendername ) TO l_laengeabs1.
IF l_laengeabs1 NE 0.
IF p_sndrec EQ 'R'.
GET PROPERTY OF l_oleemail01 'ReceivedTime' = l_origdatum1. "+ Datum + Zeit Empfang
ELSE.
GET PROPERTY OF l_oleemail01 'SentOn' = l_origdatum1. "+ Datum + Zeit Gesendet
ENDIF.
GET PROPERTY OF l_oleemail01 'Subject' = l_hdrrawvers. "+ Betreff
SPLIT l_origdatum1 AT SPACE INTO l_emaildatum l_emailuhrze.
l_tempjahr01 = l_emaildatum+6(4).
l_tempmonat1 = l_emaildatum+3(2).
l_temptag001 = l_emaildatum(2).
l_tempstunde = l_emailuhrze(2).
l_tempminute = l_emailuhrze+3(2).
l_tempsekund = l_emailuhrze+6(2).
CONDENSE: l_hdrrawvers.
CONCATENATE l_tempjahr01 l_tempmonat1 l_temptag001 l_tempstunde l_tempminute l_tempsekund '+' INTO l_timestamp1.
CONCATENATE p_pronum '+' l_timestamp1 l_hdrrawvers INTO l_hdrcomplet.
**[Speichern der E-Mail als Datei:]
REPLACE ALL OCCURRENCES OF ':' IN l_hdrcomplet WITH '_'.
REPLACE ALL OCCURRENCES OF '/' IN l_hdrcomplet WITH '-'.
REPLACE ALL OCCURRENCES OF '&' IN l_hdrcomplet WITH '+'.
REPLACE ALL OCCURRENCES OF '"' IN l_hdrcomplet WITH ''''.
REPLACE ALL OCCURRENCES OF '\' IN l_hdrcomplet WITH '-'.
REPLACE ALL OCCURRENCES OF '*' IN l_hdrcomplet WITH '+'.
REPLACE ALL OCCURRENCES OF '?' IN l_hdrcomplet WITH '+'.
REPLACE ALL OCCURRENCES OF '<' IN l_hdrcomplet WITH '_'.
REPLACE ALL OCCURRENCES OF '|' IN l_hdrcomplet WITH '-'.
REPLACE ALL OCCURRENCES OF '>' IN l_hdrcomplet WITH '_'.
CONCATENATE 'U:/Tickets/Mails/' l_hdrcomplet '.mht' INTO l_pfadmailcp. "+ Verzeichnis, wo die E-Mail außerhalb von Outlook
"+ gespeichert werden soll
CALL METHOD OF l_oleemail01 'SAVEAS'
EXPORTING
#1 = l_pfadmailcp
#2 = 10.
* ** Füllen der internen Tabelle:
CONDENSE: l_emaildatum, l_emailuhrze.
w_lit_emails-mailsen = l_sendername.
w_lit_emails-maildat = l_emaildatum.
w_lit_emails-mailtim = l_emailuhrze.
w_lit_emails-mailjah = l_tempjahr01.
w_lit_emails-mailhdc = l_hdrcomplet.
w_lit_emails-mailhdr = l_hdrrawvers.
APPEND w_lit_emails TO lit_emails.
FREE OBJECT l_oleemail01.
ENDIF.
ELSE.
EXIT.
ENDIF.
CLEAR: w_lit_emails, l_emailuhrze, l_emaildatum, l_pfadmailcp, l_hdrrawvers, l_laengeabs1, l_sendername.
CLEAR: l_hdrcomplet, l_origdatum1, l_tempjahr01, l_tempmonat1, l_temptag001, l_tempstunde, l_tempminute, l_tempsekund.
ENDDO.
ULINE.
CLEAR l_dszaehler1.
* Ergebnisse aus interner Tabelle ausgeben:
LOOP AT lit_emails INTO w_lit_emails.
l_dszaehler1 = l_dszaehler1 + 1.
WRITE:/ w_lit_emails-maildat, w_lit_emails-mailtim, w_lit_emails-mailsen, w_lit_emails-mailhdr, w_lit_emails-mailhdc.
CALL METHOD OF l_oleeingang 'Items' = l_oleemail01 "+ Zugriff auf E-Mail Nr. l_dszaehler1
EXPORTING
#1 = l_dszaehler1.
CALL METHOD OF l_oleemail01 'MOVE'
EXPORTING
#1 = l_oledtarget.
IF sy-subrc EQ 0.
l_dszaehler1 = l_dszaehler1 - 1.
ENDIF.
CLEAR w_lit_emails.
FREE OBJECT l_oleemail01.
ENDLOOP.
FREE OBJECT l_oleoutlook.
FREE OBJECT l_olenmspace.
FREE OBJECT l_oleeingang.
FREE OBJECT l_olemainpth.
FREE OBJECT l_oledtarget.
2019 Apr 03 4:00 PM
Here is the equivalence between your VBA code and the ABAP code:
Sub MoveItems() FORM moveitems.
Dim OutlookApp As Outlook.Application DATA outlookapp TYPE ole2_object.
Dim myNamespace As Outlook.NameSpace DATA mynamespace TYPE ole2_object.
Dim myFolder As Outlook.MAPIFolder DATA myfolder TYPE ole2_object.
Dim destFolder As Outlook.MAPIFolder DATA destfolder TYPE ole2_object.
Dim count As Long, n As Long DATA count TYPE i.
Dim movItems As Outlook.Items DATA movitems TYPE ole2_object.
Dim mItem As Object DATA mitem TYPE ole2_object.
DATA n TYPE i.
Set OutlookApp = New Outlook.Application CREATE OBJECT outlookapp 'Outlook.Application'.
Set myNamespace = OutlookApp.GetNamespace("MAPI") CALL METHOD OF outlookapp 'GetNameSpace' = mynamespace
EXPORTING #1 = 'MAPI'.
' Default Inbox
Set myFolder = myNamespace.GetDefaultFolder(6) CALL METHOD OF mynamespace 'GetDefaultFolder'
= myfolder EXPORTING #1 = 6.
' Subfolder of Default Inbox
Set destFolder = myFolder.Folders("NEW") CALL METHOD OF myfolder 'Folders' = destfolder
EXPORTING #1 = 'NEW'.
Set movItems = myFolder.Items GET PROPERTY OF myfolder 'Items' = movitems.
count = movItems.count GET PROPERTY OF movitems 'Count' = count.
For n = count To 1 Step-1 DO count TIMES.
n = count - sy-index + 1.
movItems(n).Move destFolder CALL METHOD OF myfolder 'Items' = mitem
EXPORTING #1 = n.
CALL METHOD OF mitem 'Move' EXPORTING #1
= destfolder.
Next n ENDDO.
End Sub ENDFORM.
2019 Apr 04 3:17 PM
Thanks a lot. Now I have the additional request by the specialist department, to save the attachment, too. I tried to translate this VBA-Code (that works fine):
Sub Test()
Dim OutlookApp As Outlook.Application
Set OutlookApp = New Outlook.Application
Dim myNamespace As Outlook.NameSpace
Set myNamespace = OutlookApp.GetNamespace("MAPI")
Dim myFolder As Outlook.MAPIFolder
Set myFolder = myNamespace.GetDefaultFolder(6) ' Default Inbox
Dim Items As Outlook.Items
Set Items = myFolder.Items
Dim mItem As Outlook.MailItem
Dim Atts As Attachments
Dim Att As Attachment
Dim count As Long, n As Long
Dim strPath As String
strPath = "c:\__Daten\"
count = Items.count
For n = 1 To count
Set mItem = Items(n)
Set Atts = mItem.Attachments
For Each Att In Atts
Att.SaveAsFile strPath & Att.FileName
Next
Next n
End Sub
And here the ABAP-Code. The Method within the stars results SY-SUBRC = 2.
REPORT Z_TEST3.
DATA: outlook TYPE ole2_object,
nmspace TYPE ole2_object,
myfolder TYPE ole2_object, "+ Posteingang
items TYPE ole2_object,
item TYPE ole2_object, "+ E-Mail
attachments TYPE ole2_object,
attachment TYPE ole2_object.
DATA: count1 TYPE i,
count2 TYPE i,
strPath TYPE string VALUE 'c:\__Daten\',
x TYPE i,
y TYPE i.
CREATE OBJECT outlook 'Outlook.Application'.
CALL METHOD OF outlook 'GetNameSpace' = nmspace
EXPORTING
#1 = 'MAPI'.
CALL METHOD OF nmspace 'GetDefaultFolder' = myfolder
EXPORTING
#1 = 6.
GET PROPERTY OF myfolder 'Items' = items.
GET PROPERTY OF items 'Count' = count1.
WHILE ( x < count1 ).
x = x + 1.
CALL METHOD OF myfolder 'Items' = item
EXPORTING
#1 = x.
CALL METHOD OF item 'Attachments' = attachments.
GET PROPERTY OF attachments 'Count' = count2.
WHILE ( y < count2 ).
y = y + 1.
**************************************************************************************
CALL METHOD OF attachments 'Attachment' = attachment
EXPORTING
#1 = y.
**************************************************************************************
CALL METHOD OF attachment 'SaveAsFile'
EXPORTING
#1 = strPath.
ENDWHILE.
CLEAR: y, count2.
ENDWHILE.
CLEAR: x, count1, count2.
2019 Apr 04 3:30 PM
CALL METHOD OF attachments 'Item' = attachment EXPORTING #1 = y.