VBA to insert a row/data when a new pdf has been added to a folder

Bramble

New Member
Joined
Aug 8, 2013
Messages
33
Hello everybody!

I'm currently using the below code to add a file from a folder location and hyperlink it to an excel sheet.

Code:
Option Compare TextOption Explicit
 
Function Excludes(Ext As String) As Boolean
     'Function purpose:  To exclude listed file extensions from hyperlink listing
     
    Dim X, NumPos As Long
     
     'Enter/adjust file extensions to EXCLUDE from listing here:
    X = Array("exe", "bat", "dll", "zip", "xlsm")
     
    On Error Resume Next
    NumPos = Application.WorksheetFunction.Match(Ext, X, 0)
    If NumPos > 0 Then Excludes = True
    On Error GoTo 0
     
End Function
 
Sub HyperlinkFileList()
     'Macro purpose:  To create a hyperlinked list of all files in a user
     'specified directory, including file size and date last modified
     'NOTE:  The 'TextToDisplay' property (of the Hyperlink object) was added
     'in Excel 2000.  This code tests the Excel version and does not use the
     'Texttodisplay property if using XL 97.
     
    Dim fso As Object, _
    ShellApp As Object, _
    File As Object, _
    SubFolder As Object, _
    Directory As String, _
    Problem As Boolean, _
    ExcelVer As Integer
    Dim OpenPosition As Integer
    Dim ClosePosition As Integer
    Dim i As Integer
    Dim j As Integer
        
     
     'Turn off screen flashing
    Application.ScreenUpdating = False
     
     'Create objects to get a listing of all files in the directory
    Set fso = CreateObject("Scripting.FileSystemObject")
     
     'Prompt user to select a directory
    Do
        Problem = False
        Set ShellApp = CreateObject("Shell.Application"). _
        Browseforfolder(0, "Please choose a folder", 0, "c:\\") 'change this to specific job folder
         
        On Error Resume Next
         'Evaluate if directory is valid
        Directory = ShellApp.self.Path
        Set SubFolder = fso.GetFolder(Directory).Files
        If Err.Number <> 0 Then
            If MsgBox("You did not choose a valid directory!" & vbCrLf & _
            "Would you like to try again?", vbYesNoCancel, _
            "Directory Required") <> vbYes Then Exit Sub
            Problem = True
        End If
        On Error GoTo 0
    Loop Until Problem = False


     'Adds each file, details and hyperlinks to the list
    For Each File In SubFolder
        If Not Excludes(Right(File.Path, 3)) = True Then
            With Worksheets("MASTER")
                 'If Excel 2000 or greater, add hyperlink with file name
                 'displayed.  If earlier, add hyperlink with full path displayed
                If Val(Application.Version) > 8 Then 'Using XL2000+
                    .Hyperlinks.Add _
                    anchor:=Worksheets("MASTER").Range("A65536").End(xlUp).Offset(1, 0), _
                    Address:=File.Path, _
                    TextToDisplay:=Split(File.Name, "-")(0)
                Else 'Using XL97
                    .Hyperlinks.Add _
                    anchor:=Worksheets("MASTER").Range("A65536").End(xlUp).Offset(1, 0), _
                    Address:=File.Path
                End If
                 'Add date last modified, and size in KB
                With .Range("A65536").End(xlUp)
                    .Offset(0, 4) = File.DateCreated    'file created
                    With .Offset(0, 2)
                    
OpenPosition = InStr(File.Name, "-")
ClosePosition = InStr(File.Name, ".pdf")
On Error Resume Next
                        .Value = Mid(File.Name, OpenPosition + 1, ClosePosition - OpenPosition - 1) 'naming value for the description
                        With .Offset(0, -1)
                            .Value = Split(File.Name, " ")(0)


                        End With
                    End With
                End With
            End With
        End If
    Next
     
End Sub


In short, it adds files, then hyperlinks them to Column A, adds parts of file name in columns B, C, and E that can be used by a user to search for specific pdf's. From the file information, the user can manually add comments into Column I for future information. Everything works except when it comes to the commenting part; once another file is added to the folder, the comments to the pdf don't align. I'm hoping that there would be a way to insert a row and add the new files when they have been added, thus the rest of the information in Column I would align with the pdf.

I've looked for a week or so and tweaked code that I've found here and on different websites to see if I can get it to work, but have not been successful. I'm hoping that someone has a better idea, or can add to this code!

Thank you in advance!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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