4

What I'd need guidance specifically:

To have an indication of a native VBA method available on a new mail compose that would be triggered at any written word / phrase (or as often as possible), or a guidance on how to create an observable of a dynamic form property.

Purpose:

One Outlook functionality that could be interesting to have is to know its readability values as the mail is composed. I know they can be obtained by doing the spell checker, but I'd like to avoid the burden of doing the spellcheck to get the result - I'd like to see numbers going up and down as the mail is written.

Problem:

I kind of created the function I'd need but I failed to find a method that could trigger it at every word written. I'd assume it'd be something like WordEditor_Change, HTMLBody_Change or something alike. It'd be similar to the Worksheet_Change we have in Excel, where values can be obtained as the Excel sheet is edited.

I tried to set an observable of WordEditor.words.count but also failed miserably.

What I have so far:

WithEvents myMail As Outlook.MailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    
    Set myMail = Item
    
End Sub

Sub checkStatistics()
    
    Dim objInsp As Outlook.Inspector

    Set objInsp = myMail.GetInspector
    
    'Enum Outlook: https://msdn.microsoft.com/es-es/VBA/Outlook-VBA/articles/olobjectclass-enumeration-outlook
    If objInsp.EditorType = olEditorWord Then ' outlook 2013
        
        'Doc obj: https://msdn.microsoft.com/en-us/vba/word-vba/articles/document-object-word
        Set objdoc = objInsp.WordEditor
         
        Dim var As ClassHandlesEvent
        Dim tst As classWithEvent
        
        Set var = New ClassHandlesEvent
        Set tst = New classWithEvent
        var.EventVariable = tst
        tst.value = objdoc.Words.Count
         
         MsgBox objdoc.ReadabilityStatistics(9) & ": " & objdoc.ReadabilityStatistics(9).value & vbCrLf & "(Ideal values above 60)"
         MsgBox objdoc.ReadabilityStatistics(8) & ": " & objdoc.ReadabilityStatistics(8).value & vbCrLf & "(Ideal values above 60)"
         
    End If

    Set objdoc = Nothing
    Set objInsp = Nothing
    
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tiago Cardoso
  • 2,057
  • 1
  • 14
  • 33
  • Yes you can trigger vba macro on opening mail item- but I would not use `Application_ItemLoad` see example here- https://stackoverflow.com/a/40015124/4539709 – 0m3r Mar 26 '18 at 03:56
  • Thanks @ashleedawg - reviewing my post, it was poorly structured. I was not asking anyone to provide me a piece of code, instead I was asking for specific methods that could help me achieve what I need. I've rewritten the question, hope it's clear now. – Tiago Cardoso Mar 26 '18 at 14:36
  • Thanks @0m3r, as you can see on my code, I'm able to create the inspector and use it on a similar fashion provided by you (+1!). But my problem is that I'd need a specific method that'd be called as the body mail changes (i.e. a macro that'd be continuously called, rather than a one-off call when a new mail is created). Would that make sense? Would you see any other approach for the same objective? – Tiago Cardoso Mar 26 '18 at 14:39
  • Are you aware that there are various [free] plug-ins for Office (including Outlook) that will already do this and more? ([Here's one](https://www.grammarly.com/office-addin/windows) you've probably heard of.) – ashleedawg Apr 03 '18 at 01:57
  • Thanks @ashleedawg, my problem is that most of them requires installation and my working environment (sadly) has blocked the installation of any external plugins. That's why I looked for an "in-house" built solution. – Tiago Cardoso Apr 03 '18 at 09:31
  • You can use reminder to run your macro in regular intervals. Take a look at this https://www.slipstick.com/developer/code-samples/running-outlook-macros-schedule/ – Tehscript Apr 03 '18 at 15:49
  • 1
    @TiagoCardoso - ah yes, the fun ol' locked-down work environment where "system securty" ends up being more hinderance than helpful... I've had workplaces like that too. :-( (Here's a related [recent post](https://stackoverflow.com/a/49420347/8112776) of mine.) Did you get your solution figured out? – ashleedawg Apr 04 '18 at 00:48
  • have you considered a key press event...youd have to add other things like a paste...etc...but thats an instead – Ctznkane525 Apr 06 '18 at 01:07
  • I guess, below these events can help Reply: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.itemevents_10_event.reply.aspx PropertyChange: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.itemevents_10_event.propertychange.aspx You can tie Body property to the above. I`m looking forward to the solution you are expecting as it would relate to one of the issues I have to deal with – Hemant Pathak Apr 06 '18 at 21:22

1 Answers1

2

The below code that you execute

Set objdoc = objInsp.WordEditor

Gives you a WordDocument so now you have a WordVBA question instead of a OutlookVBA question. So you want an onchange event on changes to the document

I got lot of threads inside and outside of SO, which confirms that such a event doesn't exists

http://www.vbaexpress.com/forum/showthread.php?15718-Is-there-a-text-change-event-for-Word

Is there a way to trigger "track changes" through VBA in Excel?

Detecting when data is added to a document, eg. a character or white space

http://www.vbaexpress.com/forum/showthread.php?40690-MS-WORD-2k7-Table-content-change-event

So you will need to use what you have now, which is to check the content on email send

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265