Hello,
I am working on one code, where I can save attachment in outlook mail directly to my documents.
I have pasted the code, created folders in document and created same in outlook folder under Customize ribbon and I can see same in Macro tab in outlook.
But when I am executing this code neither it is saving anything nor giving any error msg.
I am little confuse, I guess issue is, it is not able to find my documents path.
Can anyone look into this please?
Thanks
I am working on one code, where I can save attachment in outlook mail directly to my documents.
I have pasted the code, created folders in document and created same in outlook folder under Customize ribbon and I can see same in Macro tab in outlook.
But when I am executing this code neither it is saving anything nor giving any error msg.
I am little confuse, I guess issue is, it is not able to find my documents path.
Can anyone look into this please?
Thanks
Code:
Dim strFolder As String
Public Sub SaveToFolderBob()
strFolder = "Income"
SaveAttachments
End Sub
Public Sub SaveToFolderJim()
strFolder = "Income_2"
SaveAttachments
End Sub
Private Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim strDate As String
On Error Resume Next
' Get the path to your My Documents folder
strFolderpath = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Documents"
Debug.Print strFolderpath
On Error Resume Next
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
Set objOL = Outlook.Application
Set objMsg = objOL.ActiveExplorer.Selection.Item(1)
'format the date the message was sent on
strDate = Format(objMsg.SentOn, "mmmm yyyy")
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
'Create the folders (if not present)
CreateFolders strFolderpath
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Private Function CreateFolders(strPath As String)
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
Dim oFSO As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
vPath = Split(strPath, "\")
If Left(strPath, 2) = "\\" Then
strPath = "\\" & vPath(2) & "\"
For lngPath = 3 To UBound(vPath)
strPath = strPath & vPath(lngPath) & "\"
If Not oFSO.FolderExists(strPath) Then MkDir strPath
Next lngPath
Else
strPath = vPath(0) & "\"
For lngPath = 1 To UBound(vPath)
strPath = strPath & vPath(lngPath) & "\"
If Not oFSO.FolderExists(strPath) Then MkDir strPath
Next lngPath
End If
lbl_Exit:
Set oFSO = Nothing
Exit Function
End Function