【问题标题】:Copy emails from Outlook folder to system folder将电子邮件从 Outlook 文件夹复制到系统文件夹
【发布时间】:2020-06-26 15:28:55
【问题描述】:

我在 Outlook 中使用以下 VBA 代码将选定的电子邮件复制到系统文件夹。我需要修改代码以复制 Outlook 中特定文件夹中的所有电子邮件,而不是选择电子邮件。

' General Declarations
Option Explicit

' Public declarations
' Public Enum olSaveAsTypeEnum
  'olSaveAsTxt = 0
  'olSaveAsRTF = 1
  'olSaveAsMsg = 3
'End Enum

Sub UATExport_MailasMSG()
' Routine will take all selected mails and export them as .MSG files to the
' directory defined by
' Error Handling
On Error Resume Next

' Varaiable Declarations
Dim objItem As Outlook.MailItem
Dim strExportFolder As String: strExportFolder = "I:\Documents\Dscan\"
Dim strExportFileName As String
Dim strExportPath As String
Dim objRegex As Object
Dim OldName As String, NewName As String

' Initiate regex search
Set objRegex = CreateObject("VBScript.RegExp")
With objRegex
.Pattern = "(\s|\\|/|<|>|\|\|\?|:)"
.Global = True
.IgnoreCase = True
End With


' Check if any objects are selected.
If Application.ActiveExplorer.Selection.Count = 0 Then
   MsgBox ("No item has been selected.")
Else
    ' Cycle all selected objects.
    For Each objItem In Application.ActiveExplorer.Selection
        ' If the currently selected item is a mail item we can proceed
        If TypeOf objItem Is Outlook.MailItem Then
            ' Export to the predefined folder.
            strExportFileName = objRegex.Replace(objItem.Subject, "_")
            strExportPath = strExportFolder & strExportFileName & ".txt"
            objItem.SaveAs strExportPath, olSaveAsTxt
            'MsgBox ("Email saved to: " & strExportPath)
            OldName = Dir(strExportPath)
    NewName = Left(strExportPath, Len(strExportPath) - Len(OldName)) & _
              Left(OldName, Len(OldName) - 4) & "DircanReportfor asmsmrwerwdb1u" & _
              CStr(Format(FileDateTime(strExportPath), "ddmmyyhhmmss")) & ".txt"
     Name strExportPath As NewName


' declaration to go with the others
Dim strEmailBodybackup As String

' this will go in your for loop
' Save the body so that we can restore it after.
strEmailBodybackup = objItem.Body

' Edit the body of the mail to suit needs.
objItem.Body = Replace(objItem.Body, "To", "Tscanfile", , 1, vbTextCompare)

' Process the export like in your question

' Restore the body of the original mail
objItem.Body = strEmailBodybackup
        Else
            ' This is not an email item.
        End If
    Next 'objItem
End If

' Clear routine memory
Set objItem = Nothing
Set objRegex = Nothing

End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    Folder 类提供Items 属性,该属性返回一个Items 集合对象作为指定文件夹中Outlook 项目的集合。请注意,Items 集合的索引从 1 开始,并且不保证 Items 集合对象中的项目按任何特定顺序排列。因此,您可以使用 Items 集合遍历文件夹中的所有项目。

    如果您需要找到与条件相对应的特定项目集,可以使用 Items 类的 Find/FindNextRestrict 方法。

    Sort 方法按指定属性对项目集合进行排序。例如:

    Sub SortByDueDate() 
      Dim myNameSpace As Outlook.NameSpace 
      Dim myFolder As Outlook.Folder 
      Dim myItem As Outlook.TaskItem 
      Dim myItems As Outlook.Items  
    
      Set myNameSpace = Application.GetNamespace("MAPI") 
      Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks) 
      Set myItems = myFolder.Items 
      myItems.Sort "[DueDate]", False 
      For Each myItem In myItems 
        MsgBox myItem.Subject & "-- " & myItem.DueDate 
      Next myItem 
    End Sub
    

    【讨论】:

    • 谢谢尤金!!。我已经尝试过了,但它抛出了一些错误。我想我错误地修改了代码。请你修改我上面的代码以及你的解决方案。
    • 你在说什么错误?您是否尝试调试代码?
    • 是的。我已经尝试过调试。我对 VB 编程很陌生。您是否可以修改我上面的代码以及您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多