I'm currently writing a VBA macros script run in Microsoft Outlook which should parse key information from emails and store them into an Excel spreadsheet.
Right now, I am stuck on the logic of parsing and extracting what I want.
Here is a short example of an email with the info that needs to be extracted and saved into Excel circled in yellow (Xs being capital or lowercase letters and # being numbers)
Here is the Excel layout and what is happening with my current code, nothing is popping up except the headers!
Here is my current code:
Sub Extract()
On Error Resume Next
Dim messageArray(3) As String
Set myOlApp = Outlook.Application
Dim OlMail As Variant
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 headings
xlobj.Range("a" & 1).Value = "Priority"
xlobj.Range("b" & 1).Value = "Summary"
xlobj.Range("c" & 1).Value = "Description of Trouble"
xlobj.Range("d" & 1).Value = "Device"
'xlobj.Range("e" & 1).Value = "Sender"
For i = 1 To myfolder.Items.Count
Set myitem = myfolder.Items(i)
msgtext = myitem.Body
'Search for specific text
delimtedMessage = Replace(msgtext, "Priority:", "###")
delimtedMessage = Replace(delimtedMessage, "Summary:", "###")
delimtedMessage = Replace(delimtedMessage, "Description of Trouble:", "###")
delimtedMessage = Replace(delimtedMessage, "Device:", "###")
messageArray(i) = Split(delimtedMessage, "###")
'Write to Excel
xlobj.Range("a" & i + 1).Value = messageArray(0)
xlobj.Range("b" & i + 1).Value = messageArray(1)
xlobj.Range("c" & i + 1).Value = messageArray(2)
xlobj.Range("d" & i + 1).Value = messageArray(3)
'xlobj.Range("e" & i + 1).Value = myitem.To
Next
End Sub
This is my first time ever coding in VB so any help/suggestions would be great!
Untested:
here is some code that may get you started
the email message is split into lines
then each line is split at the colon character ... ":"
(the colon is added to end of every line before doing the split, so that blank lines do not produce an error)
then actions are taken, depending on the first few characters of each line
put the code at the end of this post into an excel workbook
make sure that outlook is open when you run it
it is not a good idea to enable vba (macros) in outlook because of security issues that may be present inside the received emails
some pointers that you may already know:
you can single-step through the code by placing the cursor anywhere within the code and pressing F8 repeatably
the yellow highlight indicates which instruction will execute next
hovering mouse pointer over a variable name will indicate the value of that variable (when stopped at any breakpoint)
clicking inside the left side grey bar next to an instruction will set a breakpoint (not all instructions are 'breakpoint-able')(click again to clear)
pressing F5 will run the program up to the next breakpoint or to end of program if there is no breakpoint
use "watch window" to closely examine objects (variables)
to bring up watch window go to "menu bar" ... "view" ... "watch window"
drag any object name or variable name into the watch window, or right click on it and choose"add watch"
then you can monitor the variable value while stopped at a breakpoint
eg. drag "topOlFolder" from the third Dim statement (or from anywhere else in program)
make use of "immediate window"
press ctrl-G to bring up the "immediate window" ... any "Debug.print" command will print to the "immediate window" ... this is used for displaying any debugging info that you need without having to stop at a breakpoint
a good starting point when writing vba code, is to "record macro", then go into vbe ide and edit the resulting macro code to fit your needs
lot of the code in a recorded macro is unnecessary and can be shortenned
for instance, you may be on worksheet "Sheet5" and you need to delete everything from "Sheet2" and continue working on "Sheet5":
you would record a macro for following actions:
"click Sheet2 tab ... select all cells(ctrl-a) ... press delete ... click Sheet5 tab"
produces the following macro
it can be rewritten as:
this clears worksheet named "Sheet2" without "selecting" it, therefore it never flashes briefly on the screen
it can be annoying if some code does a lot of updates to different worksheets and each update flashes up on the screen for a brief moment
here is your code