+ Reply to Thread
Results 1 to 2 of 2

VBA function to scan directory and return file matching key word

Hybrid View

BFall VBA function to scan... 02-05-2019, 01:07 PM
CK76 Re: NEED HELP! Trying to make... 02-05-2019, 01:50 PM
  1. #1
    Registered User
    Join Date
    02-05-2019
    Location
    New Hampshire, USA
    MS-Off Ver
    2016
    Posts
    1

    VBA function to scan directory and return file matching key word

    I'm new to VBA programming and I've been stuck on trying to make this VBA function for a couple days now.

    We are a start-up company and due to multiple people filling multiple rolls the file naming structure of our prints is not completely uniform with the exception of a couple key words always being the name of a folder or the file itself.
    I would like to make a hyperlink that the computers at each workstation can click to automatically open up the corresponding pdf file.

    I have written up a skeleton frame of what the code should look like and if might be able to lend me a hand with this I would be greatly appreciative!




    Function TargetHyperlink(CustomerName, RevNumber, PartNumber)
    
    'Starting String
    'Words encased by *s (*example*) are independant of workstation and will be inserted manually accordingly
    Start = "C:\Users\*User Name*\*Station Folder*\*Server Folder For Station*\Customers\"
    
    
    
    'Open subfolder independant of Rev Number
    ' ****** Code needs to go to the current directory scan the subfolders and open the sub folder that contains the word "Rev" ******
    PartFolder = Start + CustomerName + "\Parts\" + PartNumber + "\"
    SubFolder = 0 'Scan for folder that contains the word "Rev" and returns the whole folder name
    
    
    
    TargetDirectory = PartFolder + SubFolder + "\Print\"
    
    
    
    'Open PDF that contains the word "Critical"
    ListOfFiles = 0 '*import list of files
    For i = 0 To Length(ListOfFiles)
        If InStr(LCase(ListOfFiles(i)), "critical") > 0 Then
            If InStr(ListOfFiles(i), ".pdf") > 0 Then
                TargetFile = myString
            End If
        End If
    Next
    
    
    
    TargetHyperlink = TargetDirectory + TargetFile
    End Function




    skeleton frame.PNG

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: NEED HELP! Trying to make VBA function to scan directory and return file matching key

    Use Dir() function.
    Ex: For Subfolders that contain "Rev".
    Dim coll As New Collection
    Dim SubFolder As String
    SubFolder = Dir(PartFolder, vbDirectory)
    
    Do While Len(SubFold) > 0
        If InStr(1, SubFold, "Rev", vbTextCompare) And _
            (GetAttr(PartFolder & SubFolder) And vbDirectory) = vbDirectory Then
            coll.Add PartFolder & SubFolder & "\"
        End If
        SubFold = Dir()
    Loop
    If you need further help, I'd recommend uploading sample workbook.

    To upload file, use "Go Advanced" button and follow "Manage Attachments" hyperlink. It will launch new tab/window for managing uploads.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Recursively Scan And Open All CSV Files In Directory
    By jo15765 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2018, 06:01 AM
  2. [SOLVED] Return the directory/folder of the active file
    By Tony Valko in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-13-2014, 09:03 PM
  3. Excel 2007 VBA to scan a child directory - retrieve TOC details from each .doc
    By Paula.Scorchio in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-29-2012, 02:11 AM
  4. Saving file in current directory after accessing file in another directory
    By vuxsa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2011, 11:37 AM
  5. Return full file path for files in directory
    By jeffreybrown in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2011, 12:42 PM
  6. Replies: 3
    Last Post: 06-11-2009, 07:08 PM
  7. Scan a Directory
    By jjresti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-09-2005, 08:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1