简体   繁体   English

使用 VBA 更改 Outlook 2013 电子邮件主题

[英]Changing Outlook 2013 Email Subject Using VBA

I am using the code below to save multiple selected emails in a standard file naming format in a folder, who's path is selected from a text box (textbox1).我正在使用下面的代码将多个选定的电子邮件以标准文件命名格式保存在一个文件夹中,谁的路径是从文本框 (textbox1) 中选择的。 Depending on whether a checkbox (checkbox1) is selected or not will determine whether the emails are deleted after running the code.根据是否选中复选框(checkbox1)将决定运行代码后是否删除电子邮件。 If the the checkbox is not selected then the emails are saved to the folder but not deleted from Outlook.如果未选中该复选框,则电子邮件将保存到文件夹中,但不会从 Outlook 中删除。 If the checkbox is not selected then I want the email subject in Outlook to be changed in order that I know that I have previously saved the email.如果未选中该复选框,那么我希望更改 Outlook 中的电子邮件主题,以便我知道我之前已保存该电子邮件。 The code below pretty much does everything I want except changing the email subject.除了更改电子邮件主题之外,下面的代码几乎可以完成我想要的一切。 If I select only one email all works fine.如果我只选择一封电子邮件,一切正常。 However if I select more than one email then only the subject of the first email gets changed.但是,如果我选择多封电子邮件,则只会更改第一封电子邮件的主题。 Any help appreciated.任何帮助表示赞赏。

 Sub SaveIncoming()
 Dim lngC As Long
 Dim msgItem As Outlook.MailItem
 Dim strPath As String
 Dim FiledSubject As String

 On Error Resume Next
 strPath = UserForm1.TextBox1.Value
 On Error GoTo 0
 If strPath = "" Then Exit Sub
 If Right(strPath, 1) <> "\" Then strPath = strPath & "\"

 If TypeName(Application.ActiveWindow) = "Explorer" Then
 ' save selected messages in Explorer window
 If CBool(ActiveExplorer.Selection.Count) Then
 With ActiveExplorer
 For lngC = 1 To .Selection.Count
 If .Selection(lngC).Class = olMail Then
 MsgSaver3 strPath, .Selection(lngC)

 If UserForm1.CheckBox1.Value = True Then

  .Selection(lngC).Delete

  End If

  If UserForm1.CheckBox1.Value = False Then

 FiledSubject = "[Filed" & " " & Date & "]" & " " & .Selection(lngC).Subject

 .Selection(lngC).Subject = FiledSubject

 End If

 End If
 Next lngC
 End With
 End If
 ElseIf Inspectors.Count Then
 ' save active open message
 If ActiveInspector.CurrentItem.Class = olMail Then
 MsgSaver3 strPath, ActiveInspector.CurrentItem
 End If
 End If
 End Sub

Private Sub MsgSaver3(strPath As String, msgItem As Outlook.MailItem)
  Dim intC As Integer
  Dim intD As Integer
  Dim strMsgSubj As String
  Dim strMsgFrom As String
  strMsgSubj = msgItem.Subject
  strMsgFrom = msgItem.SenderName
  ' Clean out characters from Subject which are not permitted in a file name
  For intC = 1 To Len(strMsgSubj)
  If InStr(1, ":<>""", Mid(strMsgSubj, intC, 1)) > 0 Then
  Mid(strMsgSubj, intC, 1) = "-"
  End If
  Next intC
  For intC = 1 To Len(strMsgSubj)
  If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
  Mid(strMsgSubj, intC, 1) = "_"
  End If
  Next intC

  ' Clean out characters from Sender Name which are not permitted in a           file      name
  For intD = 1 To Len(strMsgFrom)
  If InStr(1, ":<>""", Mid(strMsgFrom, intD, 1)) > 0 Then
  Mid(strMsgFrom, intD, 1) = "-"
  End If
  Next intD
  For intD = 1 To Len(strMsgFrom)
  If InStr(1, "\/|*?", Mid(strMsgFrom, intD, 1)) > 0 Then
  Mid(strMsgFrom, intD, 1) = "_"
  End If
  Next intD
  ' add date to file name
  strMsgSubj = Format(msgItem.SentOn, "yyyy-mm-dd Hh.Nn.Ss") & " "           & "[From " & strMsgFrom & "]" & " " & strMsgSubj & ".msg"
  msgItem.SaveAs strPath & strMsgSubj
  Set msgItem = Nothing
  UserForm1.Hide
  End Sub 

When you delete the remaining items move up so 2 becomes 1. You never process the original item 2.当您删除剩余的项目时,向上移动,因此 2 变为 1。您永远不会处理原始项目 2。

Try replacing尝试更换

For lngC = 1 To .Selection.count

with

For lngC = .Selection.count to 1 step -1

For the same reason a For Each loop does not work when moving or deleting.出于同样的原因,For Each 循环在移动或删除时不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM