Here I found quite a few guides that brought me to the place where I am, but I need some help introducing the finishing touches to my code (I'm a complete newbie to this, so bear with me), I'm trying to use VBA in Outlook for export data from emails that I have in a specific folder of my Outlook to succeed. I need to extract data from a message body from multiple emails into an Excel worksheet. The email template I am retrieving can be found below. I need a 10-digit number after the reference number, a 10-digit number after the serial number, and a 7-digit number after the problem description. (I highlighted the parts that I need if it is not clear)
Dear Mr. / Ms xxxxxxxx,
------------------ No information needed -----------------
Reference number 1234567890 .
STATUS: ---- no information needed -----
Serial number: XXXXXXXXXX Description of the problem: ______________ (data here may vary slightly, I only agree to extract a 7-digit number from this area, but if this cannot be done, be it) _______
Use it ....
----------------- The rest is not needed -----------------------
So far, I have managed to create a script that will look at the Outlook folder that I am currently in, open an Excel sheet, name the headers in excel and import the data. However, it pulls the whole body not only to the segments that I need, but also places them in the wrong columns in excel. This is, as far as I can, unfortunately, since I'm a complete newbie to this. I could find some examples on this site with a similar problem with solutions, but I could not fully understand them. After many trial and error, I resorted to sending myself, and any help would be greatly appreciated. Here is my code in its current incarnation -
Sub Extract() On Error Resume Next Set myOlApp = Outlook.Application Set mynamespace = myOlApp.GetNamespace("mapi") 'open the current folder, I want to be able to name a specific folder if possible⦠Set myfolder = myOlApp.ActiveExplorer.CurrentFolder Set xlobj = CreateObject("excel.application.14") xlobj.Visible = True xlobj.Workbooks.Add 'Set Heading xlobj.Range("a" & 1).Value = "Case Number" xlobj.Range("b" & 1).Value = "HDD Serial Number" xlobj.Range("c" & 1).Value = "Sys Serial Number" xlobj.Range("d" & 1).Value = "User" For i = 1 To myfolder.Items.Count Set myitem = myfolder.Items(i) msgtext = myitem.Body 'search for specific text delimtedMessage = Replace(msgtext, "reference number", "###") delimtedMessage = Replace(delimtedMessage, "Problem description:", "###") delimtedMessage = Replace(delimtedMessage, "Serial Number:", "###") messageArray = Split(delimtedMessage, "###") 'write to excel xlobj.Range("a" & i + 1).Value = messageArray(1) xlobj.Range("b" & i + 1).Value = messageArray(2) xlobj.Range("c" & i + 1).Value = messageArray(3) xlobj.Range("d" & i + 1).Value = myitem.To Next End Sub
The links I have used so far: Using VB / VBA to search for Outlook messages and extract certain data into an Excel worksheet There was another that I used, that I can not find the link, as well as the stream on reddit, but I was still stuck . I'm not sure if this is the best way to achieve the results I want, as this is my first attempt to do something like this. I am open to change anything. thanks in advance