myItems.sort适用于一个Outlook子文件夹,但dosnt适用于其他子文件夹

时间:2017-11-29 10:09:36

标签: excel vba outlook

任何人都可以解释我在下面的excel vba出错了吗?

该代码适用于一个子文件夹。(提取最新附件)但是当应用于另一个子文件夹时,它会从最旧的电子邮件中提取信息,而不是最新的。

是myFolder.Items.sort'适用的方法适用?

非常感谢任何反馈。

Sub SaveAttachments_RsConfirmation()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long

Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("Rs.Confirmation")


myFolder.Items.Sort "[ReceivedTime]", True

For Each myItem In myFolder.Items
myFolder.Items.Sort "[ReceivedTime]", True
    If myItem.Attachments.Count <> 0 Then
        For Each myAttachment In myItem.Attachments

            I = 1
            myAttachment.SaveAsFile "C:\Del.Gen.v1\Confirmation.Email\" & I & ".txt"
            eSender = myItem.SenderEmailAddress
            dtRecvd = myItem.ReceivedTime
            dtSent = myItem.CreationTime
            sSubj = myItem.Subject
            sMsg = myItem.Body

            Exit For
        Next
    End If
Next



Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A1").Value = eSender
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A2").Value = dtRecvd
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A3").Value = dtSent
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A4").Value = sSubj
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A5").Value = sMsg



Debug.Print eSender
Debug.Print dtRecvd
Debug.Print dtSent
Debug.Print sSubj
Debug.Print sMsg



End Sub

3 个答案:

答案 0 :(得分:0)

我看到你对文件夹进行排序,取出每个项目并再次对整个文件夹进行排序。这可能不是你的意思。

myFolder.Items.Sort "[ReceivedTime]", True           ' sort folder

For Each myItem In myFolder.Items                    ' process each item
    myFolder.Items.Sort "[ReceivedTime]", True       ' sort folder again: remove this line
    If myItem.Attachments.Count <> 0 Then
        For Each myAttachment In myItem.Attachments
            I = 1                                     ' shouldn't this be incremented?

请注意,如果I未递增,则所有附件将被放置(替换)在同一文件中,因此您只能看到一个附件,该附件将是文件夹中最后一封电子邮件中的附件。

我不确定你的问题是什么。首先,我将删除第二个排序命令。重新排序可能会弄乱从集合中获得myItem的顺序。

但是如果你想进入每个子文件夹和子文件夹,等等,你必须开发一个递归程序,对文件夹进行排序,处理每个项目,如果项目是文件夹,下降到子文件夹,排序文件夹等等

答案 1 :(得分:0)

myItems.Sort没问题。

myFolder.Items.Sort尽管您可能已经看过的文档无效。

Sub SaveAttachments_RsConfirmation()

Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder

Dim myItems As Outlook.items

Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

Set myFolder = myFolder.folders("Rs.Confirmation")

' Attempt to sort items in the folder
myFolder.items.Sort "[ReceivedTime]", True
Debug.Print myFolder.items(1).Subject

' False should be no different from True
myFolder.items.Sort "[ReceivedTime]", False
Debug.Print myFolder.items(1).Subject

' Create a collection of items
Set myItems = myFolder.items
myItems.Sort "[ReceivedTime]", True
Debug.Print myItems(1).Subject

' False should sort opposite to True
myItems.Sort "[ReceivedTime]", False
Debug.Print myItems(1).Subject

ExitRoutine:
    Set myOlapp = Nothing
    Set myNameSpace = Nothing
    Set myFolder = Nothing
    Set myItems = Nothing

End Sub

答案 2 :(得分:0)

您正在对一个Items集合进行排序,但最终使用完全不同的对象 - 每次调用MAPIFolder.Items时,您都会获得一个不知道任何其他实例的全新COM对象。读取一次items集合,将其存储在变量中,然后遍历其项目:

    <!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <title>Login</title>


    <!-- Bootstrap Core Css -->
    <link href="plugins/bootstrap/css/bootstrap.css" rel="stylesheet">

    <!-- Waves Effect Css -->
    <link href="plugins/node-waves/waves.css" rel="stylesheet" />

    <!-- Animation Css -->
    <link href="plugins/animate-css/animate.css" rel="stylesheet" />

    <!-- Custom Css -->
    <link href="css/login/style.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

</head>
<body class="login-page">
    <div class="login-box">
        <div class="logo">
            <h1>login</h1></a>
            <a href="index.php"><small>welcome</small></a>
        </div>
        <div class="card">
            <div class="body">
                <!-- Login users -->
                <form id="login_user" name="login_user" method="POST" action="funcs/check.php">
                    <div class="msg">please enter your login info</div>
                    <div class="input-group">

                        <div class="form-line">
                            <input name="logina_username" id="logina_username" type="text" class="form-control" maxlength="50" placeholder="username" autofocus />
                        </div>
                        <span id="name_status" class="status"></span>
                    </div>
                    <div class="input-group">

                        <div class="form-line">
                            <input name="logina_pass" id="logina_pass" type="password" class="form-control" maxlength="50" placeholder="password" />
                        </div>
                    </div>

                    <button class="btn btn-block btn-lg bg-orange waves-effect" type="submit">login</button>

                    <div class="m-t-25 m-b--5 align-center">
                        <a href="index.php" id="a-black">back to Home</a>
                    </div>
                </form>
                <!-- /Register form for users -->
            </div>
        </div>
    </div>

    <!-- Jquery Core Js -->
    <script src="plugins/jquery/jquery.min.js"></script>

    <!-- Bootstrap Core Js -->
    <script src="plugins/bootstrap/js/bootstrap.js"></script>

    <!-- Waves Effect Plugin Js -->
    <script src="plugins/node-waves/waves.js"></script>

    <!-- Validation Plugin Js -->
    <script src="plugins/jquery-validation/jquery.validate.js"></script>
    <!-- Custom Js -->
    <script src="js/admin.js"></script>
    <script src="js/form-validation.js"></script>
</body>

</html>