More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 19-Oct-2017 09:12

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 ]  
 Basic Outlook Printer Friendly Version
Working with Outlook items, folders, recipients; dealing with security; writing event handlers
Topic
Add to Excel from Outlook when a email body contain a specific text
Hi,

I am new to VB (& to this forum) but I am ok with C++ programming in unix.

Just wondering how to build a VB code to insert a line from a outlook email. That is when an email arrived with a particular text in email body (can be done with a outlook rule) and need to run a VBA script to add a new line to a existing excel file as a new raw.

Is this possible with VBA?

If any one has done this before, can you please share it here or give a direction to get it starting.

Thanks for your help.

Mathewfer

  08-May-2013  07:24
  08-May-2013  11:48   
Examples

https://groups.google.com/forum/?fromgroups&hl=en#!topic/excel-macros/yCqg5WPSchs

http://www.techrepublic.com/blog/msoffice/quickly-export-outlook-e-mail-items-to-excel/744

An older example of next row
http://www.mrexcel.com/forum/excel-questions/520483-outlook-visual-basic-applications-exporting-body-incoming-message-excel.html
  09-May-2013  03:37   
Hi Niton,

Thank you for the links.

As a start, I tried to run the code in link 2 above but I got a compiler error
I think I know the reason - mine is Outlook 2010 but the code is meant for 2003.

Any idea to modify it to run in 2010?

Here is the code I ran.

Sub ExportToExcel()
  On Error GoTo ErrHandler
  Dim appExcel As Excel.Application Dim wkb As Excel.Workbook

Dim wks As Excel.Worksheet

Dim rng As Excel.Range

Dim strSheet As String

Dim strPath As String

Dim intRowCounter As Integer

Dim intColumnCounter As Integer

Dim msg As Outlook.MailItem

Dim nms As Outlook.NameSpace

Dim fld As Outlook.MAPIFolder

Dim itm As Object
    strSheet = "OutlookItems.xls" strPath = "C:Examples\"

strSheet = strPath & strSheet

Debug.Print strSheet
  'Select export folder
Set nms = Application.GetNamespace("MAPI")

Set fld = nms.PickFolder
  'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then

MsgBox "There are no mail messages to export", vbOKOnly, _

"Error"

Exit Sub

ElseIf fld.DefaultItemType <> olMailItem Then

MsgBox "There are no mail messages to export", vbOKOnly, _

"Error"

Exit Sub

ElseIf fld.Items.Count = 0 Then

MsgBox "There are no mail messages to export", vbOKOnly, _

"Error"

Exit Sub

End If
  'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")

appExcel.Workbooks.Open (strSheet)

Set wkb = appExcel.ActiveWorkbook

Set wks = wkb.Sheets(1)

wks.Activate

appExcel.Application.Visible = True
  'Copy field items in mail folder.
For Each itm In fld.Items

intColumnCounter = 1

Set msg = itm

intRowCounter = intRowCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.To

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.SenderEmailAddress

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.Subject

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.SentOn

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.ReceivedTime

Next itm
  Set appExcel = Nothing Set wkb = Nothing

Set wks = Nothing

Set rng = Nothing

Set msg = Nothing

Set nms = Nothing

Set fld = Nothing

Set itm = Nothing
  Exit Sub
ErrHandler: If Err.Number = 1004 Then

MsgBox strSheet & " doesn't exist", vbOKOnly, _

"Error"

Else

MsgBox Err.Number & "; Description: ", vbOKOnly, _

"Error"

End If

Set appExcel = Nothing

Set wkb = Nothing

Set wks = Nothing

Set rng = Nothing

Set msg = Nothing

Set nms = Nothing

Set fld = Nothing

Set itm = Nothing
End Sub

Mathew
  09-May-2013  16:13   
You did not state the error but try this.
In the Tools menu | References
Check Microsoft Excel Object Library
  11-May-2013  00:07   
Hi Niton,

Thanks for the reply. Yes, MS Excell Object Library is checked.

The error is a popup message which says "Compiler error / syntax error".

Any idea how to get it to office 2010?

Thanks

Mathew
    Page [ 1 ]