【问题标题】:how to catch the selected attachment items from contex menu inside outlook attachment menu如何从 Outlook 附件菜单中的上下文菜单中捕获选定的附件项目
【发布时间】:2021-01-15 21:58:02
【问题描述】:

我在 Delphi 10.3 (RIO) 中开发,使用 add-in-express ver 9.x 编写 Outlook 插件 我只想从附件列表中“选择”文件。

我将按钮添加到 adxRibbonContextMenu 对象, 当我用鼠标在附件列表中的一个(选定的)或多个文件上单击右键时, 我希望能够只获得选定的一个(一个或多个)。

我在 VB 或 C# 中提供了一些解决方案 - 如何聊天 AttachmentSelection 对象。 但我看不到如何将它翻译成 Delphi。

我试过类似的东西:

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;

但这给了我所有列表,我在附件项中找不到任何“选定”属性。 我查看了 IMIBO(扩展 mapi),也没有找到任何解决方案。

有人能拿到这份清单吗?

在 C# 中,代码是:来自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); 
} 

谢谢。

【问题讨论】:

  • 请编辑您的问题并添加您翻译到 Delphi 的 C# 代码。
  • 嗨 fpiette,谢谢。我编辑了我的 Q,我认为问题是我找不到与 Outlook._AttachmentSelection 等效的东西

标签: delphi outlook email-attachments


【解决方案1】:

AttachmentSelectionOutlook2010 中定义,而您使用Outlook2000。只需将您使用的子句从 Outlook2000 更改为 Outlook2010 以及您提到 Outlook2000 的任何地方。

【讨论】:

  • 你对 outlok2000 的看法是正确的 :),我确实在 Outlook2010 中找到了 _AttachmentSelection。
  • @mazluta 当您确认时,将我的答案标记为已接受。谢谢。
  • 这没有帮助。请看我自己的“答案”。 (我怎样才能重播你的答案?)。我还尝试将 OutlookApplication 连接到 Outlook 并尝试选择附件选择列表,但总是得到第一个。
  • 在您的问题中添加一个 MRE(最小可重现示例)。我没有时间输入所有的东西来测试你的问题。编辑您的问题 .pas 和 .dfm 文件。如果你这样做,我会试试看。
  • 嗨 fpiette,我不知道如何添加 MRE,也找不到在页面中执行此操作的按钮。所以我把它放在:hanibaal.co.il/dwn/sof/mz-priority-dll.zip(完整的项目 - 不大)。
【解决方案2】:

使用Explorer.AttachmentSelection 集合。 AttachmentSelection.Class 属性为 169 (OlObjectClass.olAttachmentSelection)

【讨论】:

    【解决方案3】:

    嗨 fpiette / Dmitry / 全部

    我试过这个。但我总是只返回第一个选定的项目(我可能不是选定的??)

    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;
    

    【讨论】:

    • 你做了这个回答。对你满意吗?我觉得不是。因此,您应该删除它并创建一个新的特定问题或将其作为评论添加到其他答案中,或者使用更多信息编辑原始问题。遵守 StackOverflow 规则很重要...
    • 没有。我只是不能添加评论所以登录:) 我不知道如何回答你的答案
    • 我认为 Delphi 的 add-in-express VCL 不是 VB 或 c#。在office2000中是堆栈
    • 从来没有处理过,但由于您只能选择一个附件进行预览,因此选择集合仅反映该附件。
    • 嗨 dimitry,我可以选择尽可能多的东西,但是当我传递附件列表(来自资源管理器)时,我总是得到 1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多