excel - 我无法用我的代码检索我的前景中的内容
问题描述
我无法使用我的代码进入 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
推荐阅读
- excel - Windows 中是否有完整的 International(xlCountryCode) 列表?
- python - 如何将 CSV 中的数据读入 Python 中的元组?
- c++ - 使用 FT_Glyph_StrokeBorder 后 FT_Glyph_To_Bitmap 因 Raster_Overflow 失败
- angular - 有没有办法在业力覆盖范围内显示错过的覆盖范围
- javascript - Node.js 同步排队大量任务
- c++ - 在网格中找到最短路径的算法
- java - 实现一个具有动态生成属性的可排序类
- c++ - 致命错误:特征/密集:VSC 中没有这样的文件或目录
- django - Django 发出信号如何正确连接发送者和接收者
- php - 安装 laravel bootstrap 时的 npm 警告