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

  • 本文关键字:代码 检索 excel vba outlook
  • 更新时间 :
  • 英文 :


我无法使用我的代码了解我的 Outlook 上的任务文件夹的内部情况。你们知道这里有什么问题吗?

我看到的问题是它没有进入我的 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

我只是更改了您的代码上的一些内容,但它对我来说工作正常

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

最新更新