2015-03-08 57 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

相關問題