Hello,
I found a macro that copies Outlook folder structure to a selected folder and all folder items, such as emails, appointments, tasks, etc. are also copied into corresponding folders in the hard disc:
How to copy Outlook folder structure to desktop (windows explorer)?
I have been trying to modify the code in order to replicate the same folder structure in a selected folder, but I only need pdf email attachments in pdf containing certain strings in their name to be saved in the corresponding folders in the drive... no luck so far...
Below my attempt, thanks in advance
I found a macro that copies Outlook folder structure to a selected folder and all folder items, such as emails, appointments, tasks, etc. are also copied into corresponding folders in the hard disc:
How to copy Outlook folder structure to desktop (windows explorer)?
I have been trying to modify the code in order to replicate the same folder structure in a selected folder, but I only need pdf email attachments in pdf containing certain strings in their name to be saved in the corresponding folders in the drive... no luck so far...
Below my attempt, thanks in advance
VBA Code:
Option Explicit
Dim xFSO As Scripting.FileSystemObject
Sub CopyOutlookFldStructureToWinExplorer()
ExportAction "Copy"
End Sub
Sub ExportAction(xAction As String)
Dim xFolder As Outlook.Folder
Dim xFldPath As String
xFldPath = SelectAFolder()
If xFldPath = "" Then
MsgBox "You did not select a folder. Export cancelled."
Else
Set xFSO = New Scripting.FileSystemObject
Set xFolder = Outlook.Application.ActiveExplorer.CurrentFolder
ExportOutlookFolder xFolder, xFldPath
End If
Set xFolder = Nothing
Set xFSO = Nothing
End Sub
Sub ExportOutlookFolder(ByVal OutlookFolder As Outlook.Folder, xFldPath As String)
Dim xSubFld As Outlook.Folder
Dim xItem As Object
Dim xAtt As Outlook.Attachments
Dim xPath As String
Dim xFilePath As String
Dim xSubject As String
Dim xCount As Integer
Dim xFilename As String
On Error Resume Next
xPath = xFldPath & "\" & OutlookFolder.Name
If Dir(xPath, 16) = Empty Then MkDir xPath
For Each xItem In OutlookFolder.Items
If xAtt.Count <> 0 Then
For Each xAtt In xItem
If InStr(xItem.xAtt.DisplayName, ".pdf") Then
If InStr(xItem.DisplayName, "Capital call", vbTextCompare) _
Or InStr(xItem.DisplayName, "Drawdown", vbTextCompare) _
Or InStr(xItem.DisplayName, "Distribution", vbTextCompare) _
Or InStr(xItem.DisplayName, "Notice", vbTextCompare) Then
xFilename = xAtt.DisplayName
xCount = 0
xFilePath = xPath & "\" & xFilename
' If xFSO.FileExists(xFilePath) Then
' xCount = xCount + 1
' xFilename = xSubject & " (" & xCount & ").pdf"
' xFilePath = xPath & "\" & xFilename
' End If
xItem.SaveAs xFilePath
End If
End If
Next
End If
Next
For Each xSubFld In OutlookFolder.Folders
ExportOutlookFolder xSubFld, xPath
Next
Set OutlookFolder = Nothing
Set xItem = Nothing
End Sub
Function SelectAFolder() As String
Dim xSelFolder As Object
Dim xShell As Object
On Error Resume Next
Set xShell = CreateObject("Shell.Application")
Set xSelFolder = xShell.BrowseForFolder(0, "Select a folder", 0, 0)
If Not TypeName(xSelFolder) = "Nothing" Then
SelectAFolder = xSelFolder.self.Path
End If
Set xSelFolder = Nothing
Set xShell = Nothing
End Function