Outlook.Exe Keeping Handle After Attachment Save VB

瘦欲@ 提交于 2019-12-11 11:23:09

问题


I have some VBA that i use to download all the attachments from an email and save them to a directory.

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

回答1:


To answer the question myself after Siddharth's fantastic help and guidance Outlook isn't holding on to the directory.

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.



来源:https://stackoverflow.com/questions/19975143/outlook-exe-keeping-handle-after-attachment-save-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!