outlook vba find properties of the item the reply is originated from [duplicate]

断了今生、忘了曾经 提交于 2020-05-16 21:59:38

问题


I have user-defined properties in my emails. I would like them to be copied in case of an reply to the new item (from the one, the reply was originated from).

This would not be the problem, but with the code below copied from https://docs.microsoft.com/de-de/office/vba/api/outlook.mailitem.reply%28even%29 I get "VBA Error 91: Object Variable not set" when running Initialize_Handler

Public WithEvents myItem As MailItem 

Sub Initialize_Handler() 
  Set myItem = Application.ActiveInspector.CurrentItem 
End Sub 

Private Sub myItem_Reply(ByVal Response As Object, Cancel As Boolean) 
  ...code for copying user-defined properties...
End Sub

What causes this error? It even comes if "ThisOutlookSession" has only this code in it.

And am I looking the right way for getting values/properties from the original-email-item?

Thanks! Max


回答1:


It seems there is no inspector window opened at the time when the following code runs:

Sub Initialize_Handler() 
  Set myItem = Application.ActiveInspector.CurrentItem 
End Sub 

The ActiveInspector returns Nothing in this case. You can read more about the error you get on the Object variable not set (Error 91) article.




回答2:


on another page i found a solution that works. Here the full (adapted) code:

Option Explicit
Public WithEvents objInspectors As Outlook.Inspectors
Public WithEvents objExplorer As Outlook.Explorer
Public WithEvents olitem As Outlook.mailitem

Private Sub Application_Startup()
 Dim myRecipient As Outlook.Recipient
 Dim folder As Outlook.MAPIFolder

 Set objInspectors = Outlook.Inspectors
 Set objExplorer = Outlook.Application.ActiveExplorer
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    Dim olitem As Object
    Set olitem = Inspector.CurrentItem
    If TypeName(olitem) = "MailItem" Then Set olitem = olitem
End Sub

Private Sub olitem_Forward(ByVal Forward As Object, Cancel As Boolean)
 Call kennzeichn_auto(olitem, Forward)
End Sub
Private Sub olitem_Reply(ByVal response As Object, Cancel As Boolean)
 Call kennzeichn_auto(olitem, response)
End Sub
Private Sub olitem_ReplyAll(ByVal response As Object, Cancel As Boolean)
 Call kennzeichn_auto(olitem, response)
End Sub
Sub kennzeichn_auto(olitem As mailitem, response As mailitem)
 Call AddUserProperty(response, "Kunde", wert_property(olitem, "Kunde"))
 Call AddUserProperty(response, "Bearbeiter", wert_property(olitem, "Bearbeiter"))
 Call AddUserProperty(response, "KundeNo", wert_property(olitem, "KundeNo"))
End Sub


来源:https://stackoverflow.com/questions/61490640/outlook-vba-find-properties-of-the-item-the-reply-is-originated-from

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!