【问题标题】:Copy list of email attachment filenames to the clipboard将电子邮件附件文件名列表复制到剪贴板
【发布时间】:2020-04-12 05:51:07
【问题描述】:

我收到的电子邮件包含 1 到 30 多个附件。我需要在我们的订单系统中重新输入他们的姓名。

我想获取所有附件的文件名列表,并将其显示在消息框中以便我可以复制它,或者将其直接放在剪贴板上。

我找到了我需要的部分。

我有一个循环遍历列表,这没有帮助。

Sub ListAttachments()
Dim a As Attachments
Dim myitem1 As Outlook.mailItem
Dim j As Long

Set myitem1 = ActiveExplorer().Selection.item(1)
Set a = myitem1.Attachments

For j = 1 To myitem1.Attachments.Count
    MsgBox myitem1.Attachments.item(j).DisplayName ' or .Filename
Next j

End Sub

这是我尝试的另一个,但它只是将名字放在剪贴板上:

Sub CopyToClipboardTest()
Dim M As Outlook.mailItem
Dim Buf As MSForms.DataObject

    Set M = ActiveExplorer().Selection.item(1)
    Set Buf = New MSForms.DataObject
    Buf.SetText M.Attachments.item(1).FileName
    Buf.PutInClipboard

End Sub

【问题讨论】:

  • 作为一个快速修复,为什么不在您的第一段代码中将MsgBox 替换为Debug.Print。这会将列表输出到即时窗口,您可以从中复制和粘贴。
  • 这是为我办公室里的多人准备的,我不希望他们必须打开 VBA 来获取列表。现在我们的工作是将电子邮件保存为文本文件(文件另存为),然后打开它以获取附件列表。

标签: vba outlook clipboard attachment


【解决方案1】:

debug.print 给了我一个想法!我最终得到了以下内容,将其复制到剪贴板并将其放入消息框中。谢谢托尼。

Sub ListAttachments()

'declare somethings
Dim a As Attachments
Dim myitem1 As Outlook.mailItem
Dim j As Long
Dim myText As String
Set myitem1 = ActiveExplorer().Selection.item(1)
Set a = myitem1.Attachments

'this isn't needed but it makes it look nicer
myText = "Files:" & vbLf

'cycles through each attachment, including images in the email, 
'and adds them to the 'myText' with a line break after each
For j = 1 To myitem1.Attachments.Count
    myText = myText & vbLf & myitem1.Attachments.item(j).DisplayName ' or .Filename
Next j

'These four lines are only needed for copying to the clipboard, but they require FM20.dll be 'on'
'they can be removed if you don't want to enable fm20.dll or if the message box is fine.
'remember, in windows, you can press ctrl+c to copy the text in a message box
Dim Buf As MSForms.DataObject
Set Buf = New MSForms.DataObject
Buf.SetText myText
Buf.PutInClipboard

'displays the final message box that I can copy and use elsewhere.
MsgBox myText

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多