none
UserForm List Box - Open word Docs RRS feed

  • Question

  • Hello friends,

    I have looked everywhere to help me with this problem, but am still stuck :(
    I have a userform listbox.
    I am trying to list word documents in this listbox that points to a folder full of word docs

    Then select a file and open it.

    This is what i have so far
    Do i need a file scripting object or something and where do i put it to open these documents.



     Sub UserForm1()

        Dim i   As Long, oDoc As Document
        
          With Me.ListBox1
            For i = 0 To .ListCount - 1

                If .Selected(i) = True Then
                   
                oDoc= .List(i)
                    Set oDoc = Docs.Open
                   
                    Exit For      
    End If
            Next
        End With

    I have looked on all the forums to help me but still no luck :(

    thank you if you can help

    pw
    Saturday, June 4, 2016 1:09 PM

Answers

  • This will populate your listbox with files paths

    Sub Test()
      Dim fso As New Scripting.FileSystemObject  'Reference Microsoft Scripting Runtime
      Dim fldr As Folder
      Dim f As File
      
      Set fldr = fso.GetFolder("C:\Myfiles")
      For Each f In fldr.Files
        MyListBox.AddItem f
      Next f
    End Sub

    To open a Word doc do like you showed before.

    • Marked as answer by Dan_CS Sunday, June 5, 2016 1:55 AM
    Saturday, June 4, 2016 6:19 PM

All replies

  • I assume the listbox has the full pathname to the file.

    Set oDoc = Documents.Open(.List(i))

    Saturday, June 4, 2016 2:53 PM
  • Hello MM,

    thanks for your reply.

    That is one of the problems,

    I'm not sure how to point to the folder that has the word documents :(

    Documents.Open FileName:="C:\MyFiles\"   ?

    pw

    Saturday, June 4, 2016 3:19 PM
  • Do you just have name of the file in the listbox and not the full path?  Give details.

    You can do this

    Set oDoc = Documents.Open("C:\MyFiles\" & .List(i))

    Saturday, June 4, 2016 3:52 PM
  • Hello MM,

    I created a list box on the userform.

    It is empty now.

    Next  I am trying to populate it with word files.

    So I need to point it to my folder.

    Later when I select a file - I should be able to open it?

    thanks very much for helping.

    :)

    Saturday, June 4, 2016 4:22 PM
  • This will populate your listbox with files paths

    Sub Test()
      Dim fso As New Scripting.FileSystemObject  'Reference Microsoft Scripting Runtime
      Dim fldr As Folder
      Dim f As File
      
      Set fldr = fso.GetFolder("C:\Myfiles")
      For Each f In fldr.Files
        MyListBox.AddItem f
      Next f
    End Sub

    To open a Word doc do like you showed before.

    • Marked as answer by Dan_CS Sunday, June 5, 2016 1:55 AM
    Saturday, June 4, 2016 6:19 PM
  • Thank you soooooooooo much -  this code worked to populate the list box !!!

    Last little thing to open the document - i have done this

    Sub OpenDoc()
    Dim i   As Long, oDoc As Document
        With Me.ListBox1
        For i = 0 To .ListCount - 1
        
        If .Selected(i) = True Then
        Set oDoc = Documents.Open(.List(i))
               
         oDoc = .List(i)
                    
                    Exit For
    End If
            Next
        End With

    End Sub

    The file did not open.

    thank you for helping me :)

    Saturday, June 4, 2016 6:54 PM
  •  If you are in Word it should open up.
    Saturday, June 4, 2016 9:22 PM
  • Hi MM,

    I must have done something not right , I couldn't open the word document.

    But thanks for all your help anyway,

    you have been great

    :)

    Sunday, June 5, 2016 1:56 AM
  • Can we assume that you are running this code in Word VBA? That being the case the userform code could be something like

    Option Explicit

    Private Sub CommandButton1_Click()
    Dim i As Long
    For i = 0 To Me.MyListBox.ListCount - 1
        If Me.MyListBox.Selected(i) Then
            Documents.Open Me.MyListBox.List(i)
        End If
    Next i
    Unload Me
    End Sub

    Private Sub UserForm_Initialize()
    Dim fso As Object
    Dim fldr As Object
    Dim f As File
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fldr = fso.GetFolder("C:\Path\")
        For Each f In fldr.Files
            Me.MyListBox.AddItem f
        Next f
    lbl_Exit:
        Set fso = Nothing
        Set fldr = Nothing
        Exit Sub
    End Sub

    Should open the selected documents. There is no need to apply a variable name to the documents (unless you are going to apply additional processing via the macro, before opening the next one). Only the last one to open can be oDoc


    Graham Mayor - Word MVP
    www.gmayor.com


    Sunday, June 5, 2016 6:07 AM
  • Hello Graham,

    this did the final trick :)

    thank you for helping as well.

    Great day to you

    :)

    Sunday, June 5, 2016 1:15 PM