【发布时间】: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 选择?
【问题讨论】: