两台计算机都运行64位版本的Win7。这个项目被拼凑在一起,我不是程序员。VBA项目在Outlook 2013中正常工作,但不在运行Outlook 2010的计算机上运行
该项目的功能是自动搜索附件上的电子邮件,设置为每天晚上触发提醒,并只将附件下载到具有由两个“pos”行代码定义的字符串的指定路径。基本上它只是检查文件名是否包含所需的名称/短语。随着每封电子邮件和多年来我所使用的文件略有变化,但始终包含一个声明。如果邮件是unread,它会在每封电子邮件中的所有附件完成后将其标记为已读。
唯一的另一个区别是机器与Outlook 2010确实有一些其他代码运行在它上面。我将这段代码放在了Outlook 2013的机器上,看看它是否有冲突,但它仍然完美运行。
以下代码在Outlook 2013计算机上运行得非常好,但与Outlook 2010无关。项目编译得很好,“运行”但没有下载任何文件,也没有将任何电子邮件标记为未读。
这里是“这Outlook会话”
Private WithEvents MyReminders As Outlook.Reminders
Private Sub Application_Startup()
Set MyReminders = GetOutlookApp.Reminders
End Sub
Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function
Private Sub MyReminders_ReminderFire(ByVal ReminderObject As Reminder)
'On Error GoTo ErrorHandler
If ReminderObject.Caption = "Daily Report" Then
ReminderObject.Dismiss
Daily_Report
End If
If ReminderObject.Caption = "Shutdown Outlook" Then
ReminderObject.Dismiss
Application.Quit
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
的代码,这是我对模块1的代码,这是因为只有在另一台机器上预先存在的代码。我知道它不必在模块中。那就是:
Sub Daily_Report()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
On Error GoTo GetAttachment_err
' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileNameXLS As String
Dim FileNamePDF As String
Dim posXLS As Integer
Dim posPDF As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
' Check each message for attachments
For Each Item In Inbox.Items
' Save any attachments found
If Item.UnRead = True Then
For Each Atmt In Item.Attachments
posXLS = InStr(Atmt.FileName, "FINAL EXCEL")
posPDF = InStr(Atmt.FileName, "Final PDF")
If posXLS <> 0 And (Right(Atmt.FileName, 4) = ".xls") Or posXLS <> 0 And (Right(Atmt.FileName, 5) = ".xlsx") Then
FileNameXLS = "C:\Users\ba\Downloads\Babcok Lab Reports\Babcock Excel\" & Atmt.FileName
Atmt.SaveAsFile FileNameXLS
End If
If posPDF <> 0 And (Right(Atmt.FileName, 4) = ".pdf") Then
FileNamePDF = "C:\Users\ba\Downloads\Babcok Lab Reports\Babcock PDF\" & Atmt.FileName
Atmt.SaveAsFile FileNamePDF
End If
Next Atmt
Item.UnRead = False
End If
Next Item
' Clear memory
GetAttachment_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachment_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume Next
End Sub
当我在outlook 2010上运行VBA时,根本就没有任何错误或问题,就像我在帖子中所说的那样。什么都没发生。即使我删除了所有其他代码,也没有任何反应。它像2013年的梦想一样运行。提醒会引发火灾,它会尽其所能。两个发布年份之间肯定有一些区别。此外,当提醒被触发时,特定的代码可以很好地调用其他子例程,但是当我运行“每日报告” - 在2013机器上工作得很好的子例程时,它什么也不做。一步一步地运行什么都不做。没有错误。 – Dlockhart104 2015-03-09 23:04:02
刚刚发现我的代码没有在收件箱中看到任何项目,即使我在收件箱中有两个未读项目。我不知道为什么......继续搜索。感谢你成为如此棒的人物尤金。你真的让这个世界变得更美好。 – Dlockhart104 2015-03-10 00:54:15