2014-07-22 111 views
0

我有一個從蓮花筆記下載附件的代碼。問題是每次運行它下載所有的附件。我怎樣才能讓條件不下載以前下載的附件?如何使用vb腳本從蓮花筆記下載附件

Option Explicit 
Sub Save_Attachments_Remove_Emails() 

Const stPath As String = "c:\Attachments\" 
Const EMBED_ATTACHMENT As Long = 1454 
Const RICHTEXT As Long = 1 

Dim noSession As Object 
Dim noDatabase As Object 
Dim noView As Object 
Dim noDocument As Object 
Dim noRemoveDocument As Object 
Dim noNextDocument As Object 

'Embedded objects are of the datatype Variant. 
Dim vaItem As Variant 
Dim vaAttachment As Variant 

'Instantiate the Notes session. 
Set noSession = CreateObject("Notes.NotesSession") 

'Instantiate the actual Notes database. 
'(Here is the personal e-mail database used and since it's a 
'local database no reference is made to any server.) 
Set noDatabase = noSession.GETDATABASE("CAT-DH-23.apd.cat.com/Servers/Caterpillar", "mail\pamsmine.nsf") 
' Please use this Open Function if the server is not referenced and GETDATABASE 
' opens the db file if the file is in local system. 
'Call noDatabase.Open("", "C:\notes\test.nsf") 

'Folders are views in Lotus Notes and in this example the Inbox 
'is used. 
Set noView = noDatabase.GetView("($Inbox)") 

'Get the first document in the defined view. 
Set noDocument = noView.GetFirstDocument 

'Iterate through all the e-mails in the view Inbox. 
Do Until noDocument Is Nothing 
Set noNextDocument = noView.GetNextDocument(noDocument) 
'Check if the document has an attachment or not. 
If noDocument.HasEmbedded Then 
    Set vaItem = noDocument.GetFirstItem("Body") 
    If vaItem.Type = RICHTEXT Then 
    For Each vaAttachment In vaItem.EmbeddedObjects 
    If vaAttachment.Type = EMBED_ATTACHMENT Then 
     'Save the attached file into the new folder. 
     vaAttachment.ExtractFile stPath & vaAttachment.Name 
     'Set the e-mail object which will be deleted. 
     Set noRemoveDocument = noDocument 
     End If 
    Next vaAttachment 
    End If 
End If 
Set noDocument = noNextDocument 
'Delete the e-mails which have an attached file. 
' If Not noRemoveDocument Is Nothing Then 
' noRemoveDocument.Remove (True) 
' Set noRemoveDocument = Nothing 
'End If 
Loop 

'Release objects from memory. 
Set noRemoveDocument = Nothing 
Set noNextDocument = Nothing 
Set noDocument = Nothing 
Set noView = Nothing 
Set noDatabase = Nothing 
Set noSession = Nothing 

MsgBox "All the attachments in the Inbox have successfully been saved" & vbCrLf & _ 
    "and the associated e-mails have successfully been deleted.", vbInformation 

End Sub 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

End Sub 
+0

你如何知道如果附件已經下載呢? Notes郵件數據庫中沒有標誌來指定是否下載附加文檔。一種方法是測試文件是否已經存在,但是這會給不同電子郵件附加的具有相同名稱的不同文件帶來錯誤。另一種方法是爲每個包含附件的電子郵件創建一個文件夾。 –

+0

我們不能將處理的郵件標記爲已讀。然後只有未讀郵件將被處理。這只是一個想法。請建議,如果這是可能的?還建議我們如何全天候運行這個腳本。 – user3859736

+0

還有一點@CST-Link,將不會有相同名稱的文件。現在請建議我如何實現這一目標? – user3859736

回答

0

如果所有的附件被統一命名和目標路徑只包含下載的文件,你可以很容易地檢查,如果附件不存在(即不下載),使用VBA的Dir功能:http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.90).aspx (我從來沒有測試VB.Net的行爲,只有VBA,但他們看起來很相似)。

您可能想寫:

' Previous code [...] 
' If is an attachment ... 
If vaAttachment.Type = EMBED_ATTACHMENT Then 
     ' ...check if was downloaded. 
     If Dir(stPath & vaAttachment.Name) = "" Then 
       'If not, save the attached file into the folder. 
       vaAttachment.ExtractFile stPath & vaAttachment.Name 

       'Set the e-mail object which will be deleted. 
       Set noRemoveDocument = noDocument 
     End If 
End If 
' Following code [...] 
+0

這工作......非常感謝.... – user3859736

+0

只有一個路障@ CST鏈接。如果有人從文件夾中刪除了電子郵件,那麼它會再次下載。有沒有解決這個問題的方法? – user3859736

+0

@ user3859736好吧,我能想到的唯一方法就是用一個零字節的文件替換下載的文件,它具有相同的名稱和相同的擴展名,然後將該屬性更改爲隱藏,以便它不會顯示在文件夾中(檢查「Dir 'help also)但是這需要大量的手動「同步」......但是,爲了存檔的目的,最好不要讓.nsf數據庫中的文件,而是定期下載它們。 –