none
File Browser that can open wordfiles not just images in vb.net ? RRS feed

  • Question

  • So i tried this code and unfortunately i can only open images i want to open wordfiles too tnx  and pls correct my terminologies if im wrong im a newbie

    this is the link on the code i tried https://www.youtube.com/watch?v=gqqtjdK60HU

    /Code  Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
            Try
                FolderBrowserDialog1.ShowDialog()
                For Each item As String In My.Computer.FileSystem.GetFiles(FolderBrowserDialog1.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly)
                    Icons.Images.Add(Drawing.Icon.ExtractAssociatedIcon(item))
                    ListView1.Items.Add(My.Computer.FileSystem.GetName(item), Icons.Images.Count - 1)

                    ListBox1.Items.Add(item)
                Next
     Catch ex As Exception

            End Try
        End Sub

        Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
            ListBox1.SelectedIndex = ListView1.SelectedIndices(0)
            Dim selectedItem As String = ListBox1.SelectedItem
            Process.Start(selectedItem)
        End Sub
    End Class


    Wednesday, March 8, 2017 3:44 PM

Answers

  • Hi ValeyForge,

    Hi ValeyForge,

    Based on your discerption, I suggest you to use OpenFileDialog Calss to get it. If you want to open txt file , please use openfileDialog.Filter property. I do sample below the you can refer to

    Code sample:

    Private openfile As OpenFileDialog
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Try
    
                openfile = New OpenFileDialog()
    
                openfile.Filter = "Word Document (*.docx)|*.docx|txt files (*.txt)|*.txt|All files (*.*)|*.* "
    
                openfile.Title = "Select files"
    
                openfile.CheckFileExists = True
    
                openfile.Multiselect = True
    
                openfile.RestoreDirectory = False
    
                If openfile.ShowDialog = DialogResult.OK Then
    
                    For i As Integer = 0 To openfile.SafeFileNames.Count() - 1
    
                        SelectFiles.Items.Add(openfile.SafeFileNames(i))
    
                        SelectPath.Items.Add(openfile.FileNames(i))
    
                    Next
    
    
                End If
    
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            End Try
    
        End Sub
    
    
        Private Sub SelectFiles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SelectFiles.SelectedIndexChanged
    
            SelectPath.SelectedIndex = SelectFiles.SelectedIndices(0)
    
            Dim selectedItem As String = SelectPath.SelectedItem
    
            Process.Start(selectedItem)
    
        End Sub
    

    The screenshot

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 10, 2017 7:15 AM
    Moderator

All replies

  • Hi ValeyForge,

    Hi ValeyForge,

    Based on your discerption, I suggest you to use OpenFileDialog Calss to get it. If you want to open txt file , please use openfileDialog.Filter property. I do sample below the you can refer to

    Code sample:

    Private openfile As OpenFileDialog
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Try
    
                openfile = New OpenFileDialog()
    
                openfile.Filter = "Word Document (*.docx)|*.docx|txt files (*.txt)|*.txt|All files (*.*)|*.* "
    
                openfile.Title = "Select files"
    
                openfile.CheckFileExists = True
    
                openfile.Multiselect = True
    
                openfile.RestoreDirectory = False
    
                If openfile.ShowDialog = DialogResult.OK Then
    
                    For i As Integer = 0 To openfile.SafeFileNames.Count() - 1
    
                        SelectFiles.Items.Add(openfile.SafeFileNames(i))
    
                        SelectPath.Items.Add(openfile.FileNames(i))
    
                    Next
    
    
                End If
    
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            End Try
    
        End Sub
    
    
        Private Sub SelectFiles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SelectFiles.SelectedIndexChanged
    
            SelectPath.SelectedIndex = SelectFiles.SelectedIndices(0)
    
            Dim selectedItem As String = SelectPath.SelectedItem
    
            Process.Start(selectedItem)
    
        End Sub
    

    The screenshot

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 10, 2017 7:15 AM
    Moderator
  • So i tried this code and unfortunately i can only open images i want to open wordfiles too tnx  and pls correct my terminologies if im wrong im a newbie

    If the file is a .doc  or .docx and if those extensions are associated with WORD then your statement

            Process.Start(selectedItem)

    will start WORD with that file opened. If that's not happening (asuming the OS filename associations are set correctly) then it's likely because GetName is not providing the full path to the file. You need to use Path.Combine with FolderBrowserDialog1.SelectedPath to recreate the full file pathname.
    https://msdn.microsoft.com/en-us/library/dd784047(v=vs.110).aspx

    Friday, March 10, 2017 9:53 AM
  • You don't use a File Dialog you use a Folder Dialog, 

    Your question is the same as somebody who wants to ride a racing bike who asks where the ignition switch is.  


    Success
    Cor

    Friday, March 10, 2017 11:09 AM
  • Thanks
    Saturday, March 11, 2017 4:40 AM