none
Retreive the file name and the title of the pdf document. RRS feed

  • Question

  • Hi All,

    Plz help.........

    I have to create a vba code such that if i specify the path of a folder (the folder will only consist of pdf files) then it has to return me the name and the titles of all the pdf files in that folder in a word document.

    I guess this can be done with the help of vba DIR function , but I am not sure how to do this.

    Can anyone help me with this code.And i have a very silly question " what is the vba code for having a browse button which if i click it has to show the upload dialog box where if i select a file it ll show the path of the text box near by.......

    Thanks in advance.........


    Veeru

    Thursday, March 15, 2012 5:43 PM

Answers

  • Assuming that you run the code from within Word:

    Sub ListPDF()
        Dim strPath As String
        Dim strFile As String
        Dim doc As Document
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "No folder selected.", vbCritical
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        Set doc = Documents.Add
        strFile = Dir(strPath & "*.pdf")
        Do While strFile <> ""
            doc.Content.InsertAfter strFile
            doc.Content.InsertParagraphAfter
            strFile = Dir
        Loop
    End Sub


    Regards, Hans Vogelaar

    Thursday, March 15, 2012 10:07 PM
  • Here is the Excel version:

    Sub ListPDF()
        Dim strPath As String
        Dim strFile As String
        Dim wsh As Worksheet
        Dim lngRow As Long
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "No folder selected.", vbCritical
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        Set wsh = Worksheets.Add
        strFile = Dir(strPath & "*.pdf")
        Do While strFile <> ""
            lngRow = lngRow + 1
            wsh.Cells(lngRow, 1) = strFile
            strFile = Dir
        Loop
    End Sub


    Regards, Hans Vogelaar

    Sunday, March 18, 2012 9:05 PM

All replies

  • Assuming that you run the code from within Word:

    Sub ListPDF()
        Dim strPath As String
        Dim strFile As String
        Dim doc As Document
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "No folder selected.", vbCritical
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        Set doc = Documents.Add
        strFile = Dir(strPath & "*.pdf")
        Do While strFile <> ""
            doc.Content.InsertAfter strFile
            doc.Content.InsertParagraphAfter
            strFile = Dir
        Loop
    End Sub


    Regards, Hans Vogelaar

    Thursday, March 15, 2012 10:07 PM
  • Hi Hans thanks .........That was relly helpful but inorder to furnish it in excel....can u modify the same code by assuming it as opening from an excel spreadsheet............

    Thanks in advance


    Veeru

    Sunday, March 18, 2012 6:10 PM
  • Here is the Excel version:

    Sub ListPDF()
        Dim strPath As String
        Dim strFile As String
        Dim wsh As Worksheet
        Dim lngRow As Long
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "No folder selected.", vbCritical
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        Set wsh = Worksheets.Add
        strFile = Dir(strPath & "*.pdf")
        Do While strFile <> ""
            lngRow = lngRow + 1
            wsh.Cells(lngRow, 1) = strFile
            strFile = Dir
        Loop
    End Sub


    Regards, Hans Vogelaar

    Sunday, March 18, 2012 9:05 PM