'Suppress Userproperty printing with outlook mail items

I need to suppress the printing of outlook userproperties programmatically added to a mail item. I had seen the following question that has a solution for dot.net here Suppressing Outlook Field Printing but i'm having trouble translating the code to delphi. My main problem is the invokemember line i'm guessing i need to use userproperty.invoke somehow in delphi but i'm clueless on how i should use the parameters that the invoke methode requires. Can someone help me translate the solution from that question to delphi code ?



Solution 1:[1]

Thanks with the help of the people from addin-express i have a working solution... that seems to work for outlook 2016 still have to test other outlook versions. The problem was that i did not know what parameters to use for the invoke function.

I'm posting my function here

function TAddInModule.RemoveUserPropertyPrintFlag( 
  var aUserProperty: UserProperty): Boolean; 
const 
  propID: integer = 107; 
  removePrinterFlag: integer = $4; 
var 
  res: OleVariant; 
  disp : TDispParams; 
  flags: Integer; 

  dispIDs: array[0..0] of TDispID; 
  args: array [0..0] of TVariantArg; 
begin 
   Result := False; 
   disp.cNamedArgs:= 0; 
   disp.cArgs:= 0; 
   if aUserProperty.Invoke(propID, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, disp, @res, nil, nil) = S_OK then 
   begin 
     if TVarData(res).VType = varInteger then 
     begin 
       flags := TVarData(res).VInteger; 
       args[0].vt := VT_INT; 
       args[0].intVal := flags and (not removePrinterFlag); 
       disp.cArgs := 1; 
       disp.cNamedArgs := 1; 
       dispIDs[0]:= DISPID_PROPERTYPUT; 
       disp.rgdispidNamedArgs := @dispIDs; 
       disp.rgvarg := @args; 
       Result:= aUserProperty.Invoke(propID, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, disp, nil, nil, nil) = S_OK; 
     end; 
   end; 
end; 

The translated code to delphi from the answer of the other stackoverflow should be something like this (not tested):

function TAddInModule.SuppressUserPropertyPrinting(mailItem: _MailItem) : HResult;  
const  
  propID: integer = 107;  
  removePrinterFlag: integer = $4;  
var  
  props: UserProperties;  
  prop: UserProperty;  
  i: integer;  
  res: OleVariant;  
  disp : TDispParams;  
  flags: Integer;  
  dispIDs: array[0..0] of TDispID;  

  args: array [0..0] of TVariantArg;  
begin  
  props := mailItem.UserProperties;  
  if props.Count > 0 then begin  
    for i := 1 to props.Count do begin  
       prop := props.Item(i);  

       disp.cNamedArgs:= 0;  
       disp.cArgs:= 0;  
       Result:= prop.Invoke(propID, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, disp, @res, nil, nil);  

       if TVarData(res).VType = varInteger then begin  
         flags := TVarData(res).VInteger;  

         args[0].vt := VT_INT;  
         args[0].intVal := flags and (not removePrinterFlag);  
         disp.cArgs := 1;  
         disp.cNamedArgs := 1;  
         dispIDs[0]:= DISPID_PROPERTYPUT;  
         disp.rgdispidNamedArgs := @dispIDs; 
         disp.rgvarg := @args; 
         Result:= prop.Invoke(propID, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, disp, nil, nil, nil);  
       end;  

       prop := nil;  
    end;  
  end;  
  props := nil;  
end; 

Solution 2:[2]

You will need to use IDispatch.Invoke() in Delphi. Disp id is 107 and the value must be a variant of type varInteger and the value of 4. There are quite a few examples of calling IDispatch.Invoke in the VCL source code.

If using Redemption (I am its author) is an option, it explicitly exposes the RDOUserProperty.Printable property.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2