VBA Code to Find & Remove certain text from email body

Prince27

New Member
Joined
Nov 24, 2012
Messages
41
Hi All,

I want to find certain text and remove from the body of the mail. Can you help out in providing a macro(VBA Code) to delete specific text from the email body.

For every email the below highlighted text in BOLD is common. But i don't want that information. I want a macro to delete the highlighted text.

For example my body of the mail is as below:


I am contacting you regarding the missing information/documents of this client. Request you to provide the same.


Job ID 13353
:- 1178838
Client/Information/Year : IBM Laptop Manual for the year 2011
: Microsoft Processor Version for the year 2012

Comments Need Original 2 Copies

Request ABC bank to reply with the missing information ASAP. If not, leads to decrease in the production charges.
-----------------------------------
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Please try the below code by creating a new module within Outlook. It searches all emails in the Drafts folder and removes the requested expression. Please note that the number of spaces may differ in your emails to what I copied off the webpage and the code will need to be altered as required. If you need to change the folder replace outNS.GetDefaultFolder(olFolderDrafts) with outNS.GetDefaultFolder(olFolderInbox) or the relevant folder.

Code:
Sub RemoveExpression()

Dim outNS As Outlook.NameSpace
Dim outFldr As Outlook.Folder
Dim outMailItems As Outlook.Items
Dim outMailItem As Outlook.MailItem




    Set outNS = Application.GetNamespace("MAPI")
    Set outFldr = outNS.GetDefaultFolder(olFolderDrafts)
    Set outMailItems = outFldr.Items
    
    For Each outMailItem In outMailItems
        
        outMailItem.Body = Replace(outMailItem.Body, "Client/Information/Year : IBM Laptop Manual for the year 2011 " & _
                                    vbNewLine & ": Microsoft Processor Version for the year 2012 ", "")
    Next
    
    Set outMailItems = Nothing
    Set outFldr = Nothing
    Set outNS = Nothing
    
End Sub
 
Upvote 0
Hi,

Thanks for the reply. But my question is

In my scenario if i will click on some hyperlink 1 email will get opened automatically which contains the below data.


I am contacting you regarding the missing information/documents of this client. Request you to provide the same.


Job ID 13353
:- 1178838
Client/Information/Year : IBM Laptop Manual for the year 2011
: Microsoft Processor Version for the year 2012

Comments Need Original 2 Copies

Request ABC bank to reply with the missing information ASAP. If not, leads to decrease in the production charges.

---------------------------

On the already opened email if i run a macro the highlighted text should get deleted.(i dont want macro to search and delete some text from any of the folder or inbox......)

Please help me out in getting the code for the above scenario.
 
Upvote 0
OK, I think I understand your criteria a little better. This again can be launched from a module within Outlook, or if you want to launch it from Excel (not sure why) you can change the code to Dim Insp As Outlook.Inspector Dim obj As Outlook.Object and add in a reference to the Outlook library by choosing Tools>References and then finding Microsoft Outlook x Object Library (where x is the version number)

This will replace the text in the currently open message within Outlook

Code:
Sub RemoveExpression()

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "abc", "")

    Set obj = Nothing
    Set Insp = Nothing
    
End Sub

Repalce "abc" with your chosen text.

Hope this helps
 
Upvote 0
OK, I think I understand your criteria a little better. This again can be launched from a module within Outlook, or if you want to launch it from Excel (not sure why) you can change the code to Dim Insp As Outlook.Inspector Dim obj As Outlook.Object and add in a reference to the Outlook library by choosing Tools>References and then finding Microsoft Outlook x Object Library (where x is the version number)

This will replace the text in the currently open message within Outlook

Code:
Sub RemoveExpression()

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "abc", "")

    Set obj = Nothing
    Set Insp = Nothing
    
End Sub

Repalce "abc" with your chosen text.

Hope this helps
 
Upvote 0
Hi,

I tried with the above code which u provided. Still am unsuccessful.

Also i did not find Microsoft Outlook x Object Library under <Tools <References in Out look

If you don't mind can you write a code with the information above which i have provide. So that i can just copy paste.

Thanks in advance.
 
Upvote 0
Can you try placing the code within a module in Outlook?

Code:
Sub RemoveExpression()

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "Client/Information/Year : IBM Laptop Manual for the year 2011" & _
                                    vbNewLine & ": Microsoft Processor Version for the year 2012", "")

    Set obj = Nothing
    Set Insp = Nothing

End Sub

That should work, if it doesn't then it is because of the leading spaces after 'year 2011' which you'll need to determine within your actual email.

If you really have to run it from Excel then try this (untested)

Code:
Sub RemoveExpression()

Dim outApp As Object
Dim outInsp As Object
Dim outObj As Object

    Set outApp = CreateObject("Outlook.Application")
    Set outInsp = outApp.ActiveInspector
    Set outObj = outInsp.CurrentItem

    outObj.Body = Replace(outObj.Body, "Client/Information/Year : IBM Laptop Manual for the year 2011" & _
                                    vbNewLine & ": Microsoft Processor Version for the year 2012", "")

    Set outObj = Nothing
    Set outInsp = Nothing
    Set outApp = Nothing
    
End Sub
 
Upvote 0
Hi,

Sorry to bother you again. I tried the below code:

Sub RemoveExpression() Dim outApp As Object Dim outInsp As Object Dim outObj As Object Set outApp = CreateObject("Outlook.Application") Set outInsp = outApp.ActiveInspector Set outObj = outInsp.CurrentItem outObj.Body = Replace(outObj.Body, "Client/Information/Year : IBM Laptop Manual for the year 2011" & _ vbNewLine & ": Microsoft Processor Version for the year 2012", "") Set outObj = Nothing Set outInsp = Nothing Set outApp = Nothing End Sub


</pre>



Still my problem has not solved. For example in opened outlook mail if i have word "GREETING", the moment i click on the macro the word should get deleted.

I want a code for that. With the help of above code the text in the email is getting closer but no words are getting deleted.

Thanks for the reply.
 
Upvote 0
Not a problem, however I'm now not really understanding your question. Can you go through your process from start to finish?

As much detail as is possible would be great but if you could specifically mention:

How are you opening/creating a new email? Manually or via some other code somewhere?

Once the email has been opened/created what exactly do you want to do? eg. Delete a specific phrase every single time if it exists. Note: if the phrase you want to delete is dynamic then you need to provide the rules around that.

Do you then want this email to be sent? etc

As for the code I gave you yesterday, now I've looked at it, it defintiely wouldn't work as the code is using outlook.inspectors and if you are creating a new instance of Outlook there will be no open windows. I guess thats why you should always test code.

I have tested the code below in Office 2007 and it works and should work in 2003 and 2010 as well. To give a brief explanation of what is happening - Because you should already have an email open it is looking at the first open instance of Outlook and then the important line is
Code:
Set outInsp = outApp.ActiveInspector
which is looking for the active (open) email and then replacing the phrase "GREETING" in its body with "" (the outObj.Body = Replace() line). If you want to replace more than one line of the body at a time you need to concatenate the lines like
Code:
"Line 1" & vbNewLine & "Line 2"
Hope this makes sense. Feel free to come back if you are unable to edit this code to suit your needs.

Code:
Sub RemoveExpression()

Dim outApp As Object
Dim outInsp As Object
Dim outObj As Object

    Set outApp = GetObject(, "Outlook.Application")
    Set outInsp = outApp.ActiveInspector
    Set outObj = outInsp.CurrentItem

    outObj.Body = Replace(outObj.Body, "GREETING", "")

    Set outObj = Nothing
    Set outInsp = Nothing
    Set outApp = Nothing

End Sub
 
Upvote 0
Hi,

Please find the below step by step procedure for what i am looking for.

We have specific software in which we have links the moment i click on any of the link an outlook email will get opened(attached for your reference).

In the opened email the below text is common every time, but i don't want the information to be in the email.

I am looking for a macro by one click the below data should get deleted.

Client/Information/Year : IBM --------------------------------Laptop Manual for the year 2011



Hope my question is clear now. Thanks in advance for continuous support.

Iam unable to attach the sample email. However the body of the email will be exactly as below:

Greetings ABC,

I am contacting you regarding the missing information/documents of this client. Request you to provide the same.

Job ID :- 13353

Required Information :-

Client/Information/Year : IBM --------------------------------Laptop Manual for the year 2011


Comments : Need Original


Thank You,
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,306
Members
449,095
Latest member
Chestertim

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