2015-03-08 53 views
-1

两台计算机都运行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 

回答

0

当所有的邮件进入Gmail设置的Gmail帐户收件箱时,我的代码正在查看Outlook数据文件“收件箱”。一旦我通过收件箱“规则”将邮件重定向到“数据文件收件箱”,代码工作得非常好。 Daily_Report子例程正在被正确调用并且正在正确使用该应用程序。另外,我可能可以重定向我的代码以查看Gmail收件箱,但不知道如何轻松地将其作为编程的业余爱好者。任何建议替代将不胜感激。

0

您需要使用应用程序属性中的代码:

Function GetOutlookApp() As Outlook.Application 
' returns reference to native Application object 
    Set GetOutlookApp = Application 
End Function 

而且我建议你在调试步骤一步方式的代码。没有人可以帮助你,直到指定你在有问题的机器上得到确切的错误。

我不是一个程序员

当前网站是开发人员,这就是为什么我建议至少学习的基本知识。请参阅Getting Started with VBA in Outlook 2010

确保Daily_Report子版被正确调用。

+0

当我在outlook 2010上运行VBA时,根本就没有任何错误或问题,就像我在帖子中所说的那样。什么都没发生。即使我删除了所有其他代码,也没有任何反应。它像2013年的梦想一样运行。提醒会引发火灾,它会尽其所能。两个发布年份之间肯定有一些区别。此外,当提醒被触发时,特定的代码可以很好地调用其他子例程,但是当我运行“每日报告” - 在2013机器上工作得很好的子例程时,它什么也不做。一步一步地运行什么都不做。没有错误。 – Dlockhart104 2015-03-09 23:04:02

+0

刚刚发现我的代码没有在收件箱中看到任何项目,即使我在收件箱中有两个未读项目。我不知道为什么......继续搜索。感谢你成为如此棒的人物尤金。你真的让这个世界变得更美好。 – Dlockhart104 2015-03-10 00:54:15

相关问题