|
楼主 |
发表于 2021-8-19 12:40
|
显示全部楼层
上面的代码可能误判了图片附件。而且有时候我们可能只需要回复部分的附件。
所以在复制附件代码中进行优化,增加一个选择的界面。
主代码:
- '带附件回复
- Sub ReplyWithAttachments()
- Dim rpl As Outlook.MailItem
- Dim itm As Object
- Set itm = GetCurrentItem()
- If Not itm Is Nothing Then
- Set rpl = itm.Reply
- CopyAttachments itm, rpl
- rpl.Display
- End If
- Set rpl = Nothing
- Set itm = Nothing
- End Sub
- '带附件回复所有
- Sub ReplyToAllWithAttachments()
- Dim rpl As Outlook.MailItem
- Dim itm As Object
- Set itm = GetCurrentItem()
- If Not itm Is Nothing Then
- Set rpl = itm.ReplyAll
- CopyAttachments itm, rpl
- rpl.Display
- End If
- Set rpl = Nothing
- Set itm = Nothing
- End Sub
- '获取主题名称
- Function GetCurrentItem() As Object
- Dim objApp As Outlook.Application
- Set objApp = Application
- On Error Resume Next
- Select Case TypeName(objApp.ActiveWindow)
- Case "Explorer"
- Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) '获取主题名称
- Case "Inspector"
- Set GetCurrentItem = objApp.ActiveInspector.CurrentItem '获取主题名称
- End Select
- Set objApp = Nothing
- End Function
- '复制附件
- Sub CopyAttachments(objSourceItem, objTargetItem)
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
- strPath = fldTemp.path & ""
- If MsgBox("本邮件附件个数:" & objSourceItem.Attachments.Count & ",是否添加所有附件回复?", vbYesNo) = vbYes Then
- For i = 1 To objSourceItem.Attachments.Count
- Set objAtt = objSourceItem.Attachments(i)
- objAtt.SaveAsFile path & objAtt.FileName
- strFile = strPath & objAtt.FileName
- objAtt.SaveAsFile strFile
- objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
- fso.DeleteFile strFile
- Next i
- Else
- UserForm1.Show
- For j = 0 To UserForm1.ListBox2.ListCount - 1
- n = UserForm1.ListBox2.List(j)
- For i = 1 To objSourceItem.Attachments.Count
- Set objAtt = objSourceItem.Attachments(i)
- If objAtt = n Then
- Set objAtt = objSourceItem.Attachments(i)
- objAtt.SaveAsFile path & objAtt.FileName
- strFile = strPath & objAtt.FileName
- objAtt.SaveAsFile strFile
- objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
- fso.DeleteFile strFile
- End If
- Next i
- Next j
- End If
- Set fldTemp = Nothing
- Set fso = Nothing
- End Sub
复制代码 UserForm1窗体代码:
- 'Listbox1项目全选
- Private Sub CheckBox1_Click()
- Dim i, j
- If CheckBox1 = True Then
- For i = 0 To ListBox1.ListCount - 1
- ListBox1.Selected(i) = True
- Next i
- Else
- For j = 0 To ListBox1.ListCount - 1
- ListBox1.Selected(j) = False
- Next j
- End If
- End Sub
- 'Listbox2项目全选
- Private Sub CheckBox3_Click()
- Dim i, j
- If CheckBox3 = True Then
- For i = 0 To ListBox2.ListCount - 1
- ListBox2.Selected(i) = True
- Next i
- Else
- For j = 0 To ListBox2.ListCount - 1
- ListBox2.Selected(j) = False
- Next j
- End If
- End Sub
- '剔除Listbox1图片附件
- Private Sub CheckBox2_Click()
- If CheckBox2 = True Then
- For i = 0 To ListBox1.ListCount - 1
- n = Right(ListBox1.List(i), 4)
- If n = ".jpg" Or n = ".png" Or n = ".jepg" Then
- ListBox1.Selected(i) = False
- Else
- ListBox1.Selected(i) = True
- End If
- Next i
- Else
- For j = 0 To ListBox1.ListCount - 1
- n = Right(ListBox1.List(i), 4)
- If n = ".jpg" Or n = ".png" Or n = ".jepg" Then
- ListBox1.Selected(j) = False
- End If
- Next j
- End If
- End Sub
- '剔除Listbox2图片附件
- Private Sub CheckBox4_Click()
- If CheckBox4 = True Then
- For i = 0 To ListBox2.ListCount - 1
- n = Right(ListBox2.List(i), 4)
- If n = ".jpg" Or n = ".png" Or n = ".jepg" Then
- ListBox2.Selected(i) = True
- End If
- Next i
- Else
- For j = 0 To ListBox2.ListCount - 1
- n = Right(ListBox2.List(j), 4)
- If n = ".jpg" Or n = ".png" Or n = ".jepg" Then
- ListBox2.Selected(j) = False
- End If
- Next j
- End If
- End Sub
- Private Sub CommandButton1_Click()
- UserForm1.hide
- End Sub
- Private Sub CommandButton2_Click()
- UserForm1.hide
- End Sub
- '选择要添加附件
- Private Sub CommandButton3_Click()
- Dim i, j
- A:
- Label3 = ListBox1.ListCount
- Label4 = ListBox2.ListCount
- For j = ListBox1.ListCount - 1 To 0 Step -1
- If ListBox1.Selected(j) = True Then
- ListBox2.AddItem ListBox1.List(j), 0
- ListBox1.RemoveItem (j)
- GoTo A
- End If
- Next j
- End Sub
- '选择要移除附件
- Private Sub CommandButton4_Click()
- A:
- Label3 = ListBox1.ListCount
- Label4 = ListBox2.ListCount
- For j = ListBox2.ListCount - 1 To 0 Step -1
- If ListBox2.Selected(j) = True = True Then
- ListBox1.AddItem ListBox2.List(j), 0
- ListBox2.RemoveItem (j)
- GoTo A
- End If
- Next j
- End Sub
- Private Sub UserForm_Initialize()
- Dim objApp As Outlook.Application
- Set objApp = Application
- On Error Resume Next
- Select Case TypeName(objApp.ActiveWindow)
- Case "Explorer"
- Set Item = objApp.ActiveExplorer.Selection.Item(1) '获取主题名称
- Case "Inspector"
- Set Item = objApp.ActiveInspector.CurrentItem '获取主题名称
- End Select
- Set objApp = Nothing
- Label2 = "邮件主题:" & vbclf & Item
- Dim olAtt As Attachment
- Dim i As Integer
- i = Item.Attachments.Count
- Label3 = i
- Label4 = 0
- With ListBox
- For j = 1 To i
- Set olAtt = Item.Attachments(j)
- ListBox1.AddItem olAtt, 0
- Next j
- End With
- ListBox1.ListIndex = 0
- End Sub
复制代码
|
|