mazluta
mazluta

Reputation: 99

how to catch the selected attachment items from contex menu inside outlook attachment menu

i develop in Delphi 10.3 (RIO), writen outlook add-in using add-in-express ver 9.x i want to chach just "selected" files from attachment list.

i add button to adxRibbonContextMenu object, when i click on the right button with the mouse on one (selected) or more files from the attachment list, i want to be able to get just the selected one (one or more).

i sow some solution in VB or C# - how to chatc AttachmentSelection object. but i can not see how to translate it to Delphi.

i tried something like :

Var
  AttachmentSelection: OleVariant;
  ExploreeSelection: OleVariant;
  ISelection: Selection;
  i,j: integer;
  IExpl: _Explorer;
  VIntf: OleVariant;
  Intf: Outlook2000._MailItem;
begin
  AttachmentSelection := OleVariant(RibbonControl.Context);
  ShowMessage(IntToStr(AttachmentSelection.Class));
  if AttachmentSelection.Class = 169 then // olAttachmentSelection = 169
  begin
    for i := 1 to AttachmentSelection.Count do begin
      ShowMessage(AttachmentSelection.Item(i).FileName);
    end;
  end
  else
  if AttachmentSelection.Class = 34 then
  begin // olExplorer = 34

    IExpl := OutlookApp.ActiveExplorer();
    ISelection := nil;
    if Assigned(IExpl) then
    try
      try
          ISelection := IExpl.Selection;
      except
          // "The Explorer has been closed and cannot be used for further operations"
        ISelection := nil;
      end;
    finally
      IExpl := nil;
    end;

    if Assigned(ISelection) then
    Try
      for i := 1 to ISelection.Count do
      begin
          //ShowMessage(ISelection.item(i));

        VIntf := ISelection.Item(i);
        ShowMessage(IntToStr(VIntf.Class));

        if ((VIntf.Class = olAttachment) or (VIntf.Class = olAttachments)) Then
        begin
          ShowMessage('olAttachment or olAttachments');
        end
        else
        if (VIntf.Class = olMail)  Then
        begin
          ShowMessage('olMail');
          IDispatch(Intf) := Self.OutlookApp.ActiveExplorer.Selection.Item(i);
          for j := 1 to Intf.Attachments.count do
          begin // this is not the selected list. it just "ALL THE LIST"

          end;
        end;

        VIntf := Unassigned;
      end;
    finally
      ISelection := nil;
    end;
  end
  else
  begin
    //ShowMessage('Not an AttachmentSelection');
  end;
end;

but this give me the all list and i can't find any property of "selected" in the attachment item. i look at IMIBO (extended mapi) and didn't find any solution there also.

did some one able to get this list?

in C# the code is : From https://www.add-in-express.com/forum/read.php?FID=5&TID=13763

object context = control.Context; 
if (context is Outlook._Explorer) 
{ 
    MessageBox.Show("EXPLORER"); 
    Outlook._Explorer window = context as Outlook._Explorer; 
    MessageBox.Show("Count: " + window.AttachmentSelection.Count); 
    GetAttachmentsInfo(item, window.AttachmentSelection); 
    Marshal.ReleaseComObject(window.AttachmentSelection); 
} 
else if (context is Outlook._Inspector) 
{ 
    MessageBox.Show("INSPECTOR"); 
    Outlook._Inspector window = context as Outlook._Inspector; 
    MessageBox.Show("Count: " + window.AttachmentSelection.Count); 
    GetAttachmentsInfo(item, window.AttachmentSelection); 
    Marshal.ReleaseComObject(window.AttachmentSelection); 
} 
else if (context is Outlook._AttachmentSelection) 
{ 
    MessageBox.Show("ATTACHMENTSELECTION"); 
    Outlook._AttachmentSelection attSelection = context as Outlook._AttachmentSelection; 
    MessageBox.Show("Count: " + attSelection.Count); 
    GetAttachmentsInfo(item, attSelection); 
    Marshal.ReleaseComObject(attSelection); 
} 

thanks.

Upvotes: 0

Views: 281

Answers (3)

mazluta
mazluta

Reputation: 99

hi fpiette / Dmitry / All

i tried this. but i alwes return just the first selected item (mybe not the selected ??)

procedure TAddInModule.SaveAttachmentsControls0Click(Sender: TObject;
  const RibbonControl: IRibbonControl);
var
  Context             : OleVariant;
  Explorer            : OleVariant;
  AttachmentSelection : _AttachmentSelection;
  count, i            : Integer;
  AttFileName         : String;
  FileName            : String;
  JustFileName        : String;
  attList             : TStringList;
  FileLength          : Integer;
  TakeIt              : Boolean;
begin
  // click on the menu entry of the attachment contextmenu


//if (context is OutlookApp.Explorers) Then
//begin
//    MessageBox.Show("EXPLORER");
//    Outlook._Explorer window = context as Outlook._Explorer;
//    MessageBox.Show("Count: " + window.AttachmentSelection.Count);
//    GetAttachmentsInfo(item, window.AttachmentSelection);
//    Marshal.ReleaseComObject(window.AttachmentSelection);
//end
//else if (context is Outlook._Inspector)
//{
//    MessageBox.Show("INSPECTOR");
//    Outlook._Inspector window = context as Outlook._Inspector;
//    MessageBox.Show("Count: " + window.AttachmentSelection.Count);
//    GetAttachmentsInfo(item, window.AttachmentSelection);
//    Marshal.ReleaseComObject(window.AttachmentSelection);
//}
//else if (context is Outlook._AttachmentSelection)
//{
//    MessageBox.Show("ATTACHMENTSELECTION");
//    Outlook._AttachmentSelection attSelection = context as Outlook._AttachmentSelection;
//    MessageBox.Show("Count: " + attSelection.Count);
//    GetAttachmentsInfo(item, attSelection);
//    Marshal.ReleaseComObject(attSelection);
//}


  AttachmentSelection := nil;
  Context := OleVariant(RibbonControl.Context);
  if context.Class = 169 then // olAttachmentSelection = 169
  begin
    IDispatch(AttachmentSelection) := Context;
  end
  else
  if context.Class = 34 then // olExplorer = 34
  begin
    Explorer := OutlookApp.ActiveExplorer();
    //Explorer := Outlook2010.OutlookApplication.ActiveExplorer;
    IDispatch(AttachmentSelection) := Explorer.AttachmentSelection;
  end;

  if Assigned(AttachmentSelection) then
  begin
    attList := TStringList.Create;
    Try
      attList.Clear;
      count := AttachmentSelection.Count;
      for I := 1 to count do
      begin
        AttFileName := AttachmentSelection.Item(I).FileName;
        //FileName    := GlobalTempDir + '\AttFile_' + IntToStr(I) + '.' + J_ExtFileName(AttFileName);
        TakeIt := True;
        if CheckIfAttachCanbeDroped(AttFileName) Then
        begin
          If (UpperCase(Copy(AttFileName,1,7)) = UpperCase('Image00')) And
             (GetMyFileSize(AttFileName) <= SMALL_FILE_SIZE) Then
            TakeIt := False;
        end;

        If Takeit Then
        begin
          JustFileName := CleanFileName(AttFileName);
          if Length(JustFilename) > MAX_FILE_NAME_LENGTH Then
          begin
            JustFileName    := Copy(J_FirstFileName(JustFileName),1,MAX_FILE_NAME_LENGTH) + '.' + J_ExtFileName(JustFileName);
          end;
          FileName     := GlobalTempDir + '\' +  JustFileName;
          if Length(Filename) > MAX_WINFILENAME_LENGTH Then
          begin
            FileLength := MAX_WINFILENAME_LENGTH - Length(j_PathFileName(Filename)) - Length(J_ExtFileName(Filename)) - 1 {for dot .};
            FileName   := GlobalTempDir + '\' + Copy(J_FirstFileName(JustFileName),1,FileLength) + '.' + J_ExtFileName(FileName);
          end;

          AttachmentSelection.Item(I).SaveAsFile(FileName);
          attList.Add(FileName);
        end;
      end;

      If attList.Count > 0 Then
        IndexAttachmentList(attList);
    Finally
      attList.Free;
    End;
  end;
end;

Upvotes: -1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66341

Use Explorer.AttachmentSelection collection. AttachmentSelection.Class property is 169 (OlObjectClass.olAttachmentSelection).

Upvotes: 1

fpiette
fpiette

Reputation: 12322

AttachmentSelection is defined in Outlook2010 while you use Outlook2000. Just change you uses clause from Outlook2000 to Outlook2010 and aslo everywhere you mention Outlook2000.

Upvotes: 1

Related Questions