Outlook.Exe 在附件保存 VB 后保持句柄 [英] Outlook.Exe Keeping Handle After Attachment Save VB
问题描述
我有一些 VBA,用于从电子邮件下载所有附件并将它们保存到目录中.
I have some VBA that i use to download all the attachments from an email and save them to a directory.
这给我带来了一些问题,因为 Outlook 中的句柄保留在文件夹中,因此无法正确删除.
This is causing me some problems because the handle from Outlook is remaining on the folder and as a result it fails to delete correctly.
我认为我的代码非常简单,不应该在脚本完成后保留文件夹.
I thought my code is pretty fool proof and shouldn't be keeping a hold on the folder after the completion of the script.
谁能指出我做错了什么:/
Can someone please point out to me what I have done wrong :/
Sub SaveCustDetails(myItem As Outlook.MailItem)
'On Error Resume Next
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myAttachment As Outlook.Attachment
Dim I As Long
Dim strBranch As String
Dim strPolRef As String
Dim strBody As String
Dim strBrLoc As Integer
Dim strPrLoc As Integer
Dim strFolderName As String
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
'Set myFolder = myFolder.Folders("Crash Alerts")
'Places the Body in a string
strBody = myItem.Body
'Finds the Branch Number
strBrLoc = InStr(1, strBody, "Branch:")
strBranch = Mid(strBody, strBrLoc + 8, 1)
'Finds the Policy Reference
strPrLoc = InStr(1, strBody, "Reference:")
strPolRef = Mid(strBody, strPrLoc + 11, 10)
'Concatenate The Branch Number and PolRef
strFolderName = strBranch & "-" & strPolRef
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
strAttachmentName = myAttachment.DisplayName
strFindOBracket = InStr(4, strAttachmentName, "(") 'Finds the Bracket
If strFindOBracket <> 0 Then
strAttachment = Trim(Mid(strAttachmentName, 1, strFindOBracket - 1)) & ".pdf"
Else
strAttachment = myAttachment.DisplayName
End If
FilePath = "C:\Processing\HTML Email\" & strFolderName & "\"
If Len(Dir(FilePath, vbDirectory)) = 0 Then
MkDir FilePath
End If
If strAttachment = "Covernote.pdf" Then
myAttachment.SaveAsFile FilePath & "Covernote1.pdf"
Else
myAttachment.SaveAsFile FilePath & strAttachment
End If
I = I + 1
Next
End If
'Next
Set myOlapp = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myAttachment = Nothing
Set myItem = Nothing
End Sub
推荐答案
在 Siddharth 出色的帮助和指导之后,我自己回答这个问题 Outlook 没有保留目录.
To answer the question myself after Siddharth's fantastic help and guidance Outlook isn't holding on to the directory.
目录本身就是一个ghost目录,删除后依然存在.这导致我的循环机制崩溃.解决方案是悉达多提供给我的代码:
The directory itself is a ghost directory that remains when it is deleted. This was causing my looping mechanism to fall over. The solution was code that Siddharth had provided me with:
On Error Resume Next
Kill FilePath & "*.*"
DoEvents
On Error GoTo 0
RmDir FilePath
DoEvents
'This line then polls explorer again to confirm the deletion and
'removes the ghost folder.
Debug.Print Len(Dir(FilePath, vbDirectory))
同样,悉达多提供的帮助绝对是太棒了,我会为他能提供的任何帮助竖起大拇指.
Again the help provided by Siddharth was absolutely fantastic I would give a thumbs up to any help he can provide.
这篇关于Outlook.Exe 在附件保存 VB 后保持句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!