vba 如何使用vb脚本从lotus notes下载附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24885556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download attachments from lotus notes using vb script
提问by user3859736
I have a code which downloads the attachment from the lotus notes. The problem is that each time it runs it downloads all the attachment. How can I give the condition not to download the previously downloaded attachments?
我有一个从莲花笔记下载附件的代码。问题是每次运行它都会下载所有附件。我怎样才能给条件不下载以前下载的附件?
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
采纳答案by user3859736
If all the attachments are uniquely named, and the destination path contains only downloaded files, you may check easily if the attachment doesn't exist already (i.e. wasn't downloaded), using VBA's Dir
function: http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.90).aspx(I never tested the VB.Net behavior, only VBA, but they seem pretty similar).
如果所有附件都是唯一命名的,并且目标路径仅包含下载的文件,则可以使用 VBA 的Dir
功能轻松检查附件是否已存在(即未下载):http: //msdn.microsoft.com /en-us/library/dk008ty4( v=vs.90).aspx(我从未测试过 VB.Net 行为,只测试过 VBA,但它们看起来非常相似)。
You may want to write:
你可能想写:
' 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 [...]