【发布时间】:2023-04-05 10:20:02
【问题描述】:
我不是 VBA 专家,但一位朋友编写了一个宏,当从列表 (MoveList) 向某人发送电子邮件时,它会自动将电子邮件从“已发送邮件”移动到另一个文件夹。
直到今天,这一切正常 - 我检查了 Outlook 中的宏设置(更改为运行所有宏),但它仍然无法正常工作。
有什么想法吗? (我很难粘贴宏,它一直在抱怨格式,所以我在这里上传了它)
Option Explicit
Dim objXL As Object
Dim objWB As Object
Dim objWS As Object
Dim objRange As Object
Private Function InList(ByVal ToList As String, ByVal DistList As Outlook.DistListItem) As Boolean
###################################################
## this function checks if the To list contains #
## Any of the names in the supplied Distribution #
## list using a string compare #
###################################################
Dim i As Integer
Dim test As String
InList = False
For i = 1 To DistList.MemberCount # check if each name is in the to list
test = DistList.GetMember(i).Name
If InStr(1, ToList, test) Then
InList = True # if name is in the to list then set function to true
End If
Next i
End Function
Private Function TwoMonths() As String
###################################################
## this function returns the date 2 months before #
## today. This does not return the time elelment #
## #
###################################################
Dim today As String
Dim day As Integer
Dim month As Integer
Dim year As Integer
today = Now #now returns todays date in the format dd/mm/yyyy hh:mm:ss
day = Left(today, 2)
month = Mid(today, 4, 2)
year = Mid(today, 7, 4)
If month < 2 Then # checks if 2 months ago is in previous year and corrects for this
year = year - 1
month = 10 + month
Else
month = month - 2
End If
TwoMonths = day & "/" & month & "/" & year
End Function
Sub MoveEmails() #(ByVal MoveFrom As String, ByVal MoveTo As String, Distributionlist As String)
####################################################
## This subroutine will move any mail that is sent #
## any person in the distribution list MoveList in #
## the last 2 months from the Sent folders #
####################################################
Dim DefaultInbox As Outlook.Folder
Dim folDefaultSentItems As Outlook.Folder
Dim folDestFolder As Outlook.Folder
Dim DefaultContacts As Outlook.Folder
Dim dlContactList As Outlook.DistListItem
Dim TopFolder As Outlook.Folder
Dim itSentEmails As Outlook.Items
Dim myItem As Object
Dim i As Long
Dim counter As Integer
Dim filterCriteria As String
Dim filteredItemsCollection As Outlook.Items
Dim Last2Months As String
Dim imail
Dim mynamespace
Set mynamespace = Application.GetNamespace("MAPI")
Set DefaultInbox = mynamespace.Folders("my email@email.com") # Change for your primary inbox name
Set DefaultContacts = mynamespace.GetDefaultFolder(olFolderContacts)
Set folDefaultSentItems = DefaultInbox.Folders("Sent Items") #selects "Sent Items" folder to move from
Set TopFolder = mynamespace.Folders("Misc") # Change for your Second inbox name
Set folDestFolder = TopFolder.Folders("Sent (Other)") # Set destination folder
Set dlContactList = DefaultContacts.Items("MoveList") # Selects the distribution list to use for check
Set itSentEmails = folDefaultSentItems.Items # select all items in "Sent Items"
# the next section restricts search to only items sent in the last 2 months
# This is to limit the number of emails checked. Assumes that
# this macro is run at a frequency less than 2 months
Last2Months = TwoMonths
filterCriteria = "[ReceivedTime] > """ & Last2Months & " 12:00 AM"""
Set filteredItemsCollection = itSentEmails.Restrict(filterCriteria)
#loop until all emails are checked
i = 1
While i <= filteredItemsCollection.Count
#loop until all emails are checked
# check if it is a mail item
If filteredItemsCollection(i).Class = olMail Then
# check if to list contains one of the emails in the distribution list
If InList(filteredItemsCollection(i).To, dlContactList) Then
# If it is in the list move the email to the destination folder
filteredItemsCollection(i).Move folDestFolder
# Reset the restricted list. When the email list is moved it changes the indexing
# in the restricted list so the index loop needs to be decramented and the restriction
# list reset. (Error cataching)
Set filteredItemsCollection = itSentEmails.Restrict(filterCriteria)
i = i - 1
End If
End If
i = i + 1 # incrament index reference
Wend
End Sub
【问题讨论】:
-
欢迎来到 StackOverflow!脚本是否显示任何错误?区域设置(例如日期格式)是否可能已更改?
-
哈希不是 VBA 中的注释指示符,因此无法编译。这让我有点困惑。 Outlook 中有一个名为
ThisOutlookSession的模块。里面有什么?我假设您正在使用ItemSend事件来触发所有其他代码并且发生了一些事情。 -
@Lyth 没有设置改变,一切仍然是相同的格式(dd/MM/yyyy 和 h:mm:ss tt
-
@DickKusleika 我只有“Micosoft Outlook 对象”,并且在包含宏的“ThisOutlookSession”中,这不应该是这样吗?即使我手动运行宏它也不会移动电子邮件,我将如何检查 ItemSend 是否损坏?
标签: vba date email outlook sendmail