Problem with Getting Treeview node value into a String.

abhay_547

Board Regular
Joined
Sep 12, 2009
Messages
179
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.

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.:)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,223,276
Messages
6,171,140
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top