Hi All,
I have the below macro which downloads the attachments from a particular folder of outlook. It works perfectly but the only thing is that I need to hardcode the folder name in the below macro which I don't want to do. I thought use the inputbox method so that user can enter the folder name and then macro can download the attachments from that folder which user has entered in inputbox but the user may or may not enter the folder name correctly, so I thought to come up with a userform which will show the Treeview of all outlook folder on the userform and the user will the select the folder from the treeview but I am unsure that how do I use the treeview node selected by the users as a string and use the same in my code in such a way that the code downloads the file from the selected folder.
So far I have the below code which I got from a google search the below code loops through the treeview and checks which node is selected by user. Please help...
I tried to incorporate something like below but it didn't work.
Thanks a lot for your help in advance.
I have the below macro which downloads the attachments from a particular folder of outlook. It works perfectly but the only thing is that I need to hardcode the folder name in the below macro which I don't want to do. I thought use the inputbox method so that user can enter the folder name and then macro can download the attachments from that folder which user has entered in inputbox but the user may or may not enter the folder name correctly, so I thought to come up with a userform which will show the Treeview of all outlook folder on the userform and the user will the select the folder from the treeview but I am unsure that how do I use the treeview node selected by the users as a string and use the same in my code in such a way that the code downloads the file from the selected folder.
Code:
Sub GetAttachments()
On Error GoTo GetAttachments_err
Dim MyMail As MailItem
Dim ns As Namespace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("[B]Datafiles[/B]") ' [B][COLOR="Red"][B]Here is where I want the treeview selected item to be used[/COLOR][/B][/B]
i = 0
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
'For Each Item In SubFolder.Items
' For Each Atmt In Item.Attachments
' FileName = UserForm1.TexBox1.Value & Atmt.FileName
' Atmt.SaveAsFile FileName
' i = i + 1
'Next Atmt
'Next Item
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
' Check filename of each attachment and save if it has "xls" extension
'If Right(Atmt.FileName, 3) = "xls" Then
' This path must exist! Change folder name as necessary.
' FileName = UserForm1.TexBox1.Value & Atmt.FileName
' Atmt.SaveAsFile FileName
'i = i + 1
'End If
Select Case Right(Atmt.FileName, 4)
Case ".xls"
FileName = UserForm1.TexBox1.Value & "\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Case ".ppt"
FileName = UserForm1.TexBox1.Value & "\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Case ".pdf"
FileName = UserForm1.TexBox1.Value & "\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Case Else
Select Case Right(Atmt.FileName, 5)
Case ".xlsx"
FileName = UserForm1.TexBox1.Value & "\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Case ".alnk"
FileName = UserForm1.TexBox1.Value & "\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
End Select
End Select
Next Atmt
Next Item
If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the abc folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub
So far I have the below code which I got from a google search the below code loops through the treeview and checks which node is selected by user. Please help...
Code:
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
Dim n As Node
If Node.Parent Is Nothing Then
Set n = Node.Child
Do Until n Is Nothing
n.Checked = Node.Checked
Set n = n.Next
Loop
End If
End Sub
I tried to incorporate something like below but it didn't work.
Code:
Set SubFolder = Inbox.Folders(TreeView1.SelectedItem.Text)
Thanks a lot for your help in advance.