Outlook VBA code not sure how to run it

aiwnjoo

Well-known Member
Joined
Jul 30, 2009
Messages
598
Hi,

I made the stupid error or paying a freelancer to write some code that would convert emails received from certain email address and convert them to plain txt from html and forward onto another email.

Here is the code, I am not sure how to execute this because I want it to only work on emails in certain folder (TEST) and only on unread ones then mark them read or something after forwarding so i can leave it running constantly and so it doesn't loop and send the same email twice.

Code:
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()


  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  
End Sub


Private Sub Items_ItemAdd(ByVal Item As Object)


    On Error GoTo ErrorHandler
    Dim Msg As Outlook.MailItem
    Dim MBody As String
  
    If TypeName(Item) = "MailItem" Then
    
        Set Msg = Item


        If Msg.SenderName = "Andrew Smith" Or Msg.SenderEmailAddress = "Andrew.Smith@azzu.co.uk" Then
        
            MBody = HtmlToText(Msg.HTMLBody)
            Dim objMsg As MailItem
            Set objMsg = Application.CreateItem(olMailItem)
            
            objMsg.Body = MBody
            objMsg.Subject = "FW: " & Item.Subject
            objMsg.Recipients.Add "andrew.smith@azzu.co.uk"
            objMsg.Send
        
        End If
    End If


ProgramExit:
  
  Exit Sub
ErrorHandler:


  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub






Function HtmlToText(sHTML) As String


  Dim oDoc As HTMLDocument
  Set oDoc = New HTMLDocument
  oDoc.Body.innerHTML = sHTML
  HtmlToText = oDoc.Body.innerText
  
End Function

Thankyou.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,225,767
Messages
6,186,906
Members
453,386
Latest member
testmaster

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top