Unable to set item datetime (as PT_SYSTIME) using PropertyAccessor in Outlook VSTO

♀尐吖头ヾ 提交于 2019-12-23 23:18:47

问题


Setting a datetime column in Outlook to a c# DateTime value with the following code

documentItem.PropertyAccessor.SetProperty(
    "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/TestDate/0x0000001F",
    documentItem.PropertyAccessor.LocalTimeToUTC(DateTime.Now));

converts the columnvalue to type PT_APPTIME, which can't be displayed, sorted etc. in an Outlook-Tableview.

All my tries to set the value as a PT_SYSTIME (eg. creating my own PROPVARIANT struct, Marshal.StructureToPtr, ...) failed with various exceptions.

Is there a way to set a datetime value without using redemption libraries, which is overkill for this purpose?


回答1:


I've had the same problem some time ago; after much trial and error with the PropertyAccessor I've resorted to passing the MAPIOBJECT property of the MailItem to a method in a C++/CLI extension library project since it's the easiest way to combine managed objects and unmanaged header files (i.e. the Outlook 2010 MAPI API headers) to set the property using the MAPI interfaces directly.

I first used Marshal::GetIUnknownForObject to get an IUnknown* pointer from the MAPIOBJECT, then used QueryInterface on it with the IID_IMessage and IID_IMAPIProp ids to get the respective interfaces and in turn used IMAPIProp's GetIDsFromNames, SetProps and SaveChanges to set the property.

(I got the general idea from this codeproject article and adapted it to my needs...)

Considering how Outlook treats PT_APPTIME values in view columns (i.e. being totally unable to do anything with them at all and instead just showing a blank value) I doubt the PropertyAccessor behaviour with regards to dates could be called anything other than a bug; it really shouldn't be neccessary to go to those lengths for such a (seemingly) simple operation... :(




回答2:


Is TestDate is the name of the user property? Keep in mind that OOM does not understand the type (0x0000001F) in a property and it will treat "TestDate/0x0000001F" as the name of the property.

Try to use UserProperties.Add instead.



来源:https://stackoverflow.com/questions/33629920/unable-to-set-item-datetime-as-pt-systime-using-propertyaccessor-in-outlook-vs

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