Consulting

Results 1 to 3 of 3

Thread: Modify VBA Sciprt to get the Body

  1. #1

    Modify VBA Sciprt to get the Body

    Hi,

    I got the macro script from my friend and i believe he also got from internet
    in this macro it only take subject, received date and sender.
    Could you please to modify the script to take the body email also.
    Below is the macro scipti
    HTML Code:
    Const MACRO_NAME = "Fetch EMAIL as per time -range and pattern in subject"
     
    Sub ExportMessagesToExcel_Time_range()
        Dim olkLst As Object, _
            olkMsg As Object, _
            excApp As Object, _
            excWkb As Object, _
            excWks As Object, _
            intRow As Integer, _
            intVersion As Integer, _
            strFilename As String, _
            strSerachpatern As String, _
            strDateRange As String, _
            arrTemp As Variant, _
            datStart As Date, _
            datEnd As Date
        strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
        strSerachpatern = InputBox("Provide Subject Serach pattern.", MACRO_NAME)
        If strFilename <> "" Then
            strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
            arrTemp = Split(strDateRange, "to")
            datStart = IIf(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
            datEnd = IIf(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
            intVersion = GetOutlookVersion()
            Set excApp = CreateObject("Excel.Application")
            Set excWkb = excApp.Workbooks.Add()
            Set excWks = excWkb.ActiveSheet
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "Subject"
                .Cells(1, 2) = "Received"
                .Cells(1, 3) = "Sender"
            End With
            intRow = 2
            'Write messages to spreadsheet
            Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
            For Each olkMsg In olkLst
                'Only export messages, not receipts or appointment requests, etc.
                If olkMsg.Class = olMail Then
                    'Add a row for each field in the message you want to export
                     If InStr(1, UCase(olkMsg.Subject), strSerachpatern) Then
                     excWks.Cells(intRow, 1) = olkMsg.Subject
                     excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                     excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                     intRow = intRow + 1
                    End If
                End If
            Next
            Set olkMsg = Nothing
            excWkb.SaveAs strFilename
            excWkb.Close
        End If
        Set olkLst = Nothing
        Set excWks = Nothing
        Set excWkb = Nothing
        Set excApp = Nothing
        MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
    End Sub
     
    Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
        Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
        On Error Resume Next
        Select Case intOutlookVersion
            Case Is < 14
                If Item.SenderEmailType = "EX" Then
                    GetSMTPAddress = SMTP2007(Item)
                Else
                    GetSMTPAddress = Item.SenderEmailAddress
                End If
            Case Else
                Set olkSnd = Item.Sender
                If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                    Set olkEnt = olkSnd.GetExchangeUser
                    GetSMTPAddress = olkEnt.PrimarySmtpAddress
                Else
                    GetSMTPAddress = Item.SenderEmailAddress
                End If
        End Select
        On Error GoTo 0
        Set olkPrp = Nothing
        Set olkSnd = Nothing
        Set olkEnt = Nothing
    End Function
     
    Function GetOutlookVersion() As Integer
        Dim arrVer As Variant
        arrVer = Split(Outlook.Version, ".")
        GetOutlookVersion = arrVer(0)
    End Function
     
    Function SMTP2007(olkMsg As Outlook.MailItem) As String
        Dim olkPA As Outlook.PropertyAccessor
        On Error Resume Next
        Set olkPA = olkMsg.PropertyAccessor
        SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
        On Error GoTo 0
        Set olkPA = Nothing
    End Function
    Thanks in advance

  2. #2
    try
    excWks.Cells(intRow, 4) = olkMsg.Body
    thought the content may well not fit in a single cell

  3. #3
    Quote Originally Posted by westconn1 View Post
    try
    excWks.Cells(intRow, 4) = olkMsg.Body
    thought the content may well not fit in a single cell
    its works..
    Thank you westconn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •