【问题标题】:How to get the selected e-mails in Outlook via Excel?如何通过 Excel 在 Outlook 中获取选定的电子邮件?
【发布时间】:2022-10-08 18:21:37
【问题描述】:

我有代码Excel- 它引用Outlook文件夹并循环项目,一切OK。

代码选择文件夹项目的每个项目并通过参数选择需要的项目。

但我希望此代码与 Outlook 选择一起使用 - 用户在 Outlook 中选择邮件并按下 Excel 上的按钮。

当我尝试直接将变量确定为 Outlook.Selection 时

Public myOlSel As Outlook.Selection

它返回一个错误,因为我需要将设置切换到 Outlook 对象模型 - 但我正在为普通用户编写一个工具,普通用户无法打开任何模型,他只能按下按钮启动宏:)

我将变量 myOlSel 确定为对象并尝试将其设置为 Outlook 应用程序对象的选择

Set myOlSel = olApp.Selection

而且它也不起作用。我猜它会发生,因为 VBA Excel 和 Outlook 都在对象模型中选择了引用相同的语法。

Sub CheckEmailsSelected()
Set olApp = GetObject(, "Outlook.Application")
Set olNameSpace = olApp.Session '.GetNameSpace("MAPI")
Dim att As Object
If Param3 = 1 Then
Set myOlSel = olApp.Selection

For Each mItem In myOlSel.Items
If mItem.Unread = True Then
    If DateValue(mItem.LastModificationTime) >= DateValue(Now) Then
        If mItem.Attachments.Count > 0 Then
        
      
 count4 = count4 + 1

        Set att = mItem.Attachments
        For i = 1 To att.Count
        If Right(att.Item(i).Filename, 4) = "xlsx" Or Right(att.Item(i).Filename, 3) = "xls" Then
        count5 = count5 + 1
        ReDim Preserve Stat(10, count5)
        Stat(1, count5) = mItem.LastModificationTime
        Stat(2, count5) = mItem.Companies
        Stat(3, count5) = mItem.Subject
        Stat(4, count5) = mItem.Sender
        Stat(5, count5) = mItem.SenderEmailAddress
        Stat(6, count5) = att.Item(i).Filename
        If Right(att.Item(i).Filename, 4) = "xlsx" Then Stat(7, count5) = Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xlsx" Else Stat(7, count5) = Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xls"
        Stat(8, count5) = mItem.Unread
        Stat(10, count5) = mItem.EntryID
        
        If Right(att.Item(i).Filename, 4) = "xlsx" Then att.Item(i).SaveAsFile Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xlsx" Else: att.Item(i).SaveAsFile Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xls"
        
        End If
        Next i
        
        End If
    End If
End If
Next mItem

End If



End Sub

如何从 Excel 中获取 Outlook 选择?

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    尝试这个:

    Sub CheckEmailsSelected()
        Dim olApp As Object, m As Object
        Set olApp = GetObject(, "Outlook.Application")
        
        Set myOlSel = olApp.ActiveExplorer.Selection
        For Each m In myOlSel
            If m.Class = 43 Then ' olMail = 43
                Debug.Print m.Subject
            End If
        Next
    End Sub
    

    【讨论】:

    • 工作,谢谢,我的错误是-我尝试使用已使用的 .Items 方法进行选择,但它不支持它
    猜你喜欢
    • 1970-01-01
    • 2012-12-10
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多