我无法使用我的代码检索前景中的内容

时间:2019-05-29 10:44:55

标签: excel vba outlook

我无法使用代码进入Outlook的Task文件夹中。你们知道这里有什么问题吗?

我看到的问题是它没有进入我的For循环。

Private Sub CommandButton1_Click()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer


Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Task")


i = 1

For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range(“date_received”).Value Then
Range(“Sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“Sender”).Offset(i, 0).Columns.AutoFit
Range(“Sender”).Offset(i, 0).VerticalAlignment = xlTop
Range(“Body”).Offset(i, 0).Value = OutlookMail.Body
Range(“Body”).Offset(i, 0).Columns.AutoFit
Range(“Body”).Offset(i, 0).VerticalAlignment = xlTop

i = i + 1
End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

我只是更改了代码中的几处内容,但对我来说一切正常

Option Explicit
Private Sub CommandButton1_Click()

    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant
    Dim i As Integer

    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Bienvenidas")

    i = 2

    With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your sheet name, and ALWAYS reference sheets and workbooks.
        For Each OutlookMail In Folder.Items
            If OutlookMail.ReceivedTime >= .Range("C1") Then
                With .Cells(i, 1)
                    .Value = OutlookMail.SenderName
                    .Columns.AutoFit
                    .VerticalAlignment = xlTop
                End With
                With .Cells(i, 2)
                    .Value = OutlookMail.Body
                    .Columns.AutoFit
                    .VerticalAlignment = xlTop
                End With
                i = i + 1
            End If
        Next OutlookMail
    End With

    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing

End Sub

不要使用范围然后进行偏移,而是转到具有发件人,正文和日期范围的单元格,然后直接引用它们。

我唯一遇到的是收件箱文件夹中的“任务”文件夹吗?错误可能来自那里。

编辑:检查收件箱文件夹中所有子文件夹的代码:

Sub Loop_folders_of_inbox()

    Dim ns As Outlook.Namespace
    Dim myfolder As Outlook.Folder
    Dim mysubfolder As Outlook.Folder
    Dim OutlookApp As Outlook.Application

    Set OutlookApp = New Outlook.Application
    Set ns = OutlookApp.GetNamespace("MAPI")
    'Get the default inboxfolder
    Set myfolder = ns.GetDefaultFolder(olFolderInbox)
    'Loop through each folder and display name of the folder
    For Each mysubfolder In myfolder.Folders
        Debug.Print mysubfolder.Name
    Next mysubfolder

End Sub

编辑2:将正文中的每个单词分为不同的列:

Dim arrBody
Dim j As Long

arrBody = Split(OutlookMail.Body, " ") 'words delimited by space
For j = LBound(arrBody) To UBound(arrBody)
    .Cells(i, j + 1) = arrBody(j) 'use j + 1 because arrays built from Space function start at index 0
Next j