D
D
denismix2019-02-06 23:12:42
Visual Basic
denismix, 2019-02-06 23:12:42

How to make comments to letters in Outlook through a form created in VBA?

In fact, how to create a comment and attach it to each letter by creating a custom field is described in detail for example here http://evo.engineering/quick-add-comments...
But, reading this comment is very difficult if it is long enough.
There is an idea to make a macro with an embedded form that will read the comment from the user field "Comment" and show it in the TextBox.
Those. By writing the code under the spoiler, when the macro is activated, we can easily display the contents of the user field "Comment" from the current selected message in the TextBox UserForm:

spoiler
Private Sub UserForm_Activate()
Dim myItem As Object

 'Инициализируем текущее выбранное сообщение
 Set myItem = Outlook.Application.ActiveExplorer.Selection(1)
 On Error Resume Next

 TextBox1.Value = myItem.UserProperties.Add("Комментарий", olText, True).Value
End Sub

But it is necessary to re-activate the macro each time selecting a new letter or make the "Update" button, which is not always convenient.
Question.
What event should be used to read the next comment in the UserForm when a new letter is selected, even with the mouse, even with the arrows?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
denismix, 2019-02-07
@denismix

Found the answer:
Add a procedure tied to the Application_ItemLoad event to the VBA Project section "Microsoft Outlook Objects / ThisOutlookSession".
This assumes that the custom "Comment" field has already been manually added to the message fields.
Do not forget to catch errors that occur when starting Outlook, which occur due to the fact that the script is already running, but ActiveExplorer has not yet started and there is no selected message yet.
The code works only when the text of the letter has loaded for viewing in the compact view (and this happens every time you select the next letter), so the system and the processor do not load.

Private Sub Application_ItemLoad(ByVal Item As Object)
Dim myItem As Object
 
 On Error Resume Next

 Set myItem = Outlook.Application.ActiveExplorer.Selection(1)
 If UserForm1.Visible Then
  UserForm1.TextBox1.Value = myItem.UserProperties.Add("Комментарий", olText, True).Value
 End If
 
 Err.Clear
 
End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question