1

I want to perform some text replacements that are just too complicated for Automatic replacement feature of Microsoft outlook. Microsoft has rather extensive documentation on Microsoft Outlook VBA scripting hosted on GitHub.

I started by pressing Alt+F11 which opened Visual Basic Editor. This is what I see in the project explorer:

image description

Please note that I am not VBA programmer, so what I did afterwards probably looks stupid. I figured out that OutlookOutlook namespace represents all Outlook classes, so I just picked one of the classes that sounds like a text field:

Dim WithEvents textField As Outlook.OlkTextBox

And I created a dummy event that should fire before change in text field:

Private Sub textField_BeforeUpdate(Cancel As Boolean)
    MsgBox "Nope!"
    Cancel = True
End Sub

Of course, when start outlook and edit message, the script doesn't get executed. So I suppose that for starter, I need to get a refference to actual text editor. How do I do that?

My question basically is, how to get this code execute when I try to edit any outlook message:

    MsgBox "Nope!"
    Cancel = True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • I suggest you look for documentation/questions on `ActiveExplorer` which returns a collection of the currently selected mail items. This would allow you to select the mail items you wish to edit before starting a macro that processed each in turn. To me this sounds closer to the functionality you seek than your current code. – Tony Dallimore Oct 14 '15 at 16:43
  • Do you know the structure of the text bodies and/or html bodies of the mail items you wish to edit? If not my answer to this question may help: [How to copy Outlook mail message into excel using VBA or Macros](http://stackoverflow.com/a/12146315/973283). This macro creates a workbook containing selected properties of mail items including the contents of their text bodies and html bodies. – Tony Dallimore Oct 14 '15 at 16:44

1 Answers1

0

Try the write event.

Private WithEvents myItem As MailItem 

Private Sub Application_Startup()
    Set myItem = ActiveInspector.CurrentItem 
End Sub

Private Sub myItem_Write(Cancel As Boolean) 
    MsgBox "Nope!"
    Cancel = True
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52