I am trying to adapt some MS Word VBA macros to run in Outlook 2016. I wonder if anyone can help me adapt the macro reproduced below, by Tomalak? (See Q 33562468)
Within Outlook I have referenced the Microsoft Word 16.0 Object Library, and I have tried to use the suggestions at http://www.vboffice.net/en/developers/use-word-macro-in-outlook/ and https://www.slipstick.com/developer/word-macro-apply-formatting-outlook-email/ but without success.
The purpose of the original VBA was to remove duplicate paragraphs which it does in Word. I want to adapt it to perform the same function on an email message which is open and being edited within Outlook 2016.
The VBA when run in Word deletes all but one instance of each duplicated paragraph; the first instance (the one nearest the top of the document) is retained. I would prefer that instead the last instance (the one nearest the foot of the document) is retained.
Sub DeleteDuplicateParagraphs()
Dim p As Paragraph
Dim d As New Scripting.Dictionary
Dim t As Variant
Dim i As Integer
Dim StartTime As Single
StartTime = Timer
' collect duplicates
For Each p In ActiveDocument.Paragraphs
t = p.Range.Text
If t <> vbCr Then
If Not d.Exists(t) Then d.Add t, New Scripting.Dictionary
d(t).Add d(t).Count + 1, p
End If
Next
' eliminate duplicates
Application.ScreenUpdating = False
For Each t In d
For i = 2 To d(t).Count
d(t)(i).Range.Delete
Next
Next
Application.ScreenUpdating = True
MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & "
seconds", vbInformation
End Sub
For...Each
over the paragraphs, use aFor...Next
loop counting back from the last paragraph: if you've already got the text in your dictionary you can delete the current one. – Tim Williams