Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

open file from listbox on userform

0

hello 

 I no  know  if  this  way  is  right  to open  files   from  listbox    and  desptite   it  gives  me  error    method or  data  member not  found   in this  word  ".WebBrowser1"   and I 'm  sure  i  select  form  referenc  in tool     microsoft  internet  controls

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim wPath As String, wName As String
    wPath = "C:\Users\OSE\Desktop\11\"
    If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
    wName = ListBox1.List(ListBox1.ListIndex)

    If LCase(Right(wName, 4)) <> ".pdf" Then wName = wName & ".pdf"


    UserForm1.WebBrowser1.Navigate wPath & wName
End Sub

so  if  anybody  has  idea  or  alternitve  code  please  provide me 

Answer
Discuss

Answers

0

Here's the code you need for your user form.

Option Explicit

Private Sub UserForm_Initialize()
    ' 189

    With Sheet1
        ListBox1.RowSource = .Range(.Cells(2, "A"), _
                                    .Cells(.Rows.Count, "A").End(xlUp)).Address
    End With
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    ' 189

    ' wPath must end on backslash ("\")
    Const wPath     As String = "C:\Users\OSE\Desktop\11\"
    Dim wName       As String

    With ListBox1
        If .ListIndex > -1 Then
            wName = ListBox1.Value
            If LCase(Right(wName, 4)) <> ".pdf" Then wName = wName & ".pdf"
            On Error Resume Next
            WebBrowser1.Navigate wPath & wName
        Else
            MsgBox "Please select a file to display.", _
                   vbExclamation, "Invalid request"
        End If
    End With
End Sub

In the attached workbook I added a WebBrowser control to the user form. This control isn't in the Toolbox by default. To add it, right-click on the tools tab of the Toolbox, select "Additonal Controls", scroll down to "Microsoft WebBrowser" and check the item. This will add the control's icon to the Toolbox and you can now select it and add a WebBrowser control to the form like a Label or Textbox.

Discuss

Discussion

Variatus many  thanks  for  your  file  but  I  would  open  the  file  when double click in selected item from listbox  ,  is  it  possible?
leap (rep: 46) Mar 9, '21 at 3:19 am
I modified my answer to respond to a double-click instead of the change event.
Variatus (rep: 4889) Mar 9, '21 at 5:01 am
Variatus  
thanks  but  it  doesn't  open  any  file  when double click  just  show  the  image  of  file   in  Microsoft WebBrowser,     may  you  fix  it , please ?
leap (rep: 46) Mar 10, '21 at 2:38 am
Sorry, I'll not fix it. You asked for it to be displayed in the WebBrowser. Now that the WebBrowser shows it you want something else. I don't understand what you want but I do know that it is another question. So, please ask another question.
Variatus (rep: 4889) Mar 10, '21 at 6:32 pm
Variatus
the topic of my post is open the file and my comment in the beginning talks about the open file !!!!
leap (rep: 46) Mar 11, '21 at 1:26 am
The file is opened and displayed in the WebBrowser as your code specified. Be a little reasonable Leap. You asked for help. You got help. You asked for code. You got code. Now you ask for something else. Ask another question.
This is a Question & Answer forum. There are more people watching this than you and I. They can't learn if you keep changing the goal posts. If you want to receive benefit you must be willing to give benefit, too.
Variatus (rep: 4889) Mar 11, '21 at 8:03 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login