PDA

View Full Version : Need full code to edit the text within an open Outlook Email



Godster
03-27-2012, 10:32 PM
Hi,
I am after the code to put in an Outlook Macro to delete the confidentiality message at the end of my email signature.
(I am a self-'taught' beginner at VBA).
Reason: I need the signature to default with the confidentiality message as it must accompany all external emails, but when communicating internally the message is not required. So every internal email, I'm scrolling down to the deletable text and deleting it. V frustrating.

My solution (since you don't seem to be able to record a macro when in an open email) was to copy my Signature into a word doc. Start the recorder and carry out the procedure there.

step1: cntrl-home to top of doc
2: cntrl-f to open the find window
3: type in the text from the opening of the confidentiality waiver. Find next.
4: esc out of the find window
5: cntrl-shift-down to select all the text requiring deletion
6: delete
7: cntrl-home to get back to the top of the page.

As a macro works fine in Word. Not so in Outlook.
I would like this to work only in the current, ie open, email.

Here's the code as copied from Word:

Sub DelConf2()
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "This email is confidential and intended solely for the use"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory

End Sub


I then copied that VBA into the email macro VBA editor.

All help appreciated, and it's safest to assume I know nothing (because I really do know next to nothing!)

I'm sure you can all see why that won't work. But I don't know enough to know what tweaks are required.

Godster
03-28-2012, 02:10 PM
I should also have said that the error message that comes up is:

Runtime error 424
Object required

skatonni
03-29-2012, 04:47 AM
Sub replace_Confidential()
Dim currItem As Outlook.MailItem
Set currItem = ActiveInspector.currentItem
currItem.Body = Replace(currItem.Body, "Confidential", "")
Set currItem = Nothing
End Sub

Godster
03-29-2012, 02:54 PM
Hi, Thanks for the reply.
All this seems to do is turn my signature from Rich text into html.

I realise there are probably a number of obvious things I need to do - but I am afraid they are not obvious to me as yet...

Nevertheless, thanks.

If you have any more thoughts or can elaborate further, it'd be appreciated.

skatonni
03-31-2012, 12:10 PM
Sub replace_Confidential()
Dim currItem As Outlook.MailItem
Set currItem = ActiveInspector.currentItem
Debug.Print currItem.BodyFormat ' 3 = RTF / olFormatRichText
currItem.Body = Replace(currItem.Body, "This email is confidential and intended solely for the use", "")
Debug.Print currItem.BodyFormat ' 3 = RTF / olFormatRichText
Set currItem = Nothing
End Sub

To preserve formatting you can try figuring out Inspector.WordEditor
http://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx