Vb.net 如何使用vb脚本从lotus notes下载附件

Vb.net 如何使用vb脚本从lotus notes下载附件,vb.net,vba,excel,vbscript,Vb.net,Vba,Excel,Vbscript,我有一个从LotusNotes下载附件的代码。问题是每次运行时,它都会下载所有附件。我如何给出不下载以前下载的附件的条件 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 Di

我有一个从LotusNotes下载附件的代码。问题是每次运行时,它都会下载所有附件。我如何给出不下载以前下载的附件的条件

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

如果所有附件的名称都是唯一的,并且目标路径仅包含下载的文件,则可以使用VBA的
Dir
函数(我从未测试过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 [...]

您如何知道附件是否已下载?Notes邮件数据库中没有用于指定是否下载了附加文档的标志。一种方法是测试该文件是否已经存在,但这会给附加到不同电子邮件的同名不同文件带来错误。另一种方法是为每封包含附件的电子邮件创建一个文件夹。我们不能将已处理的邮件标记为已读吗。然后只处理未读邮件。这只是一个想法。请建议这是否可行?还建议我们如何全天候运行此脚本。再加上一点@CST链接,将不会有同名文件。现在请建议我如何做到这一点?补充一个答案,它对你有用吗?@CST-Link将在今天检查并肯定会让你知道…只有一个障碍@CST-Link。如果有人从文件夹中删除了电子邮件,那么它将再次下载。有解决方法吗?@user3859736嗯,我能想到的唯一一件事是用一个具有相同名称和相同扩展名的零字节文件替换下载的文件,然后将属性更改为hidden,这样它就不会显示在文件夹中(同时检查
Dir
help),但这需要大量手动“同步”。。。但是,为了存档的目的,让文件进入.nsf数据库不是更好,而不是定期下载它们吗?好的,我们可以提供一个条件,只下载压缩的附件。(.zip格式)。我允许.nsf数据库中的文件,但一旦我从C:\Attachments中删除,它将再次下载。@user3859736 1)若要仅下载zip文件,请测试附件名
vaAttachment.name
是否以
“.zip”
结尾。2)您必须将有关下载文件的信息保留在某个位置,所以你不再下载它们了。最简单的方法是:将所有文件保存在文件夹中并检查它们是否存在,或者从电子邮件中删除附件。更复杂的方法需要一些努力(例如,跟踪带有下载附件的最后一封电子邮件,每次从上一封电子邮件恢复,保存实际最后一封电子邮件的“索引”),www.elance.com或www.odesk.com等网站可能会有所帮助。