+ Reply to Thread
Results 1 to 28 of 28

Search a folder with sub folders to find and open a PDF based on partial file name.

  1. #1
    Registered User
    Join Date
    08-19-2015
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    Search a folder with sub folders to find and open a PDF based on partial file name.

    Hello everyone! Learning to use VBA and still a novice!

    I have a job list where each row is information about the job. Column B is the job number always in the same format of letters and numbers (example: 2015-AAAA-001). The file related to it will always be a .pdf and will always start with job number (example 2015-AAAA-001 - Document1.pdf). I am trying to create a macro that allows the user to either us an Application.Input off of a command button and then click the job#, or if it would automatically hyperlink to the file. I am not sure which would be faster/more realistic to code. The master directory could be C:\Master and have subfolders with subfolders that would have to be searched through.

    I have tried to code this different ways and have failed every time! If anyone can help it would be greatly appreciated!

    Thank you!

  2. #2
    Registered User
    Join Date
    02-06-2013
    Location
    Ezinge, The Netherlands
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    65

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi Auruin,

    Can you provide what you achieved so far, then we can look further!

    Kind regards,

    Tom
    If happy with the answer, press 'Add Reputation'

  3. #3
    Registered User
    Join Date
    08-19-2015
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Thanks for reaching out Tom!

    I have a function to open the PDF, and I have the input box collecting the data properly. The rest of the code is something I found on another part of the forums and was trying to modify it to work. So far it does not compile or work! Any help is appreciated.


    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    02-06-2013
    Location
    Ezinge, The Netherlands
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    65

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Alright.... where to start?

    Your question is not fully clear to me: does the macro need to read the filefolders for the pdf's and create a list with urls to the files?

    Or: do you provide a sheet with filenames and you use the sheet as some kind of dashboard?

  5. #5
    Registered User
    Join Date
    08-19-2015
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    I would like the macro to work off a control box inside the excel workbook. When the control box is clicked it will open up the input data box allowing the user to click on the entry from column B containing the job number. It should then find the pdf files based off of the partial name and open.

    So you have the job #'s listed in column B. All are formated as 4 numbers - 4 letters - 3 numbers. example is 2015-AAAA-001. All of the corresponding fPDFS files to that job number start with the job number. For example 2015-AAAA-001 - Document1.pdf. The macro would have to search the folder and sub folders to find these files based on the job # and open the pdfs.

    Please let me know if I am not expressing it properly and I will try to clarify better!

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Try something like this. Change the root path to suit.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  7. #7
    Registered User
    Join Date
    08-19-2015
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    This works perfectly and is so much simpler than what i was doing! Thank you so much!
    Last edited by Auruin; 08-26-2015 at 10:45 AM.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Quote Originally Posted by Auruin View Post
    This works perfectly and is so much simpler than what i was doing! Thank you so much!
    You're welcome. Thanks for the feedback.

  9. #9
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi guys,

    That's an amazing code from AlphaFrog.

    Sorry to steal the thread now after some time, but I've been trying to convert it into a code that would open only a folder based on input, to no avail.

    (example: strRoot remains the same, when the folder is variable and consists of a number and a name; so I need to input a number like 123456 and it should open strRoot\123456 - Frank Zappa)

    AlphaFrog do you know what piece of code should be changed and into what? I assume something in the function but I can't get my head arount it

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Quote Originally Posted by cloudberr View Post
    Hi guys,

    That's an amazing code from AlphaFrog.

    Sorry to steal the thread now after some time, but I've been trying to convert it into a code that would open only a folder based on input, to no avail.

    (example: strRoot remains the same, when the folder is variable and consists of a number and a name; so I need to input a number like 123456 and it should open strRoot\123456 - Frank Zappa)

    AlphaFrog do you know what piece of code should be changed and into what? I assume something in the function but I can't get my head arount it
    Not sure what you mean by "open only a folder". The original code searches for a partial PDF file name within a root folder and its subfolders. It sounds like in your case you know the name of the subfolder. So I'm not sure what to search for.

    If you want to search for a partial file name within one known folder, maybe something like this...

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi AlphaFrog

    Sorry for not being clear enough there.

    What I meant is that I know a root folder and in this folder there are a lot of subfolders dedicated to various clients.

    Each subfolder has a name which consists of a number like 123456 and the client's name. So an example would be C:\Test\123456 Frank Zappa\

    And what I'm aiming to do is to simply open that folder, based on the number only (which would be in a cell).

    I hope it makes more sense now..

  12. #12
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi there, I guess no luck with this one?

  13. #13
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Quote Originally Posted by cloudberr View Post

    And what I'm aiming to do is to simply open that folder, based on the number only (which would be in a cell).
    What do you mean by "open a folder"? Do you want a file selector dialog that lists all the files in a specific folder?

    I hope it makes more sense now..

  14. #14
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    No, I literally want to open the folder with the windows explorer

    Basically you run the code, you're prompted for a number referring to a client and a folder opens in the windows explorer containing all the files inside - that's the idea.
    The folder name consists of Number + Name + Surname, but it would need to find the correct folder with the Number part only.

    English is not my native and I'm not that expert at technical lingo so I'm not sure if I'm making it clearer or rather more convoluted.

  15. #15
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Your English is very good. That is the information I was asking for.

    Try this. Change the root folder in the code.

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    You're goddamn reptile magician, AlphaFrog. Big thanks!!

  17. #17
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hey AlphaFrog, I wanted to raise a weird point.

    It all works nicely (you should have seen how my colleagues were extatic when I implemented this today) with a standard button.

    However I wanted to put something nicer on the spreadsheet and used a coloured text box.

    The weird part is that the macro ran OK, but afterwards the CTRL+F shortcut stopped functioning?? What is even weirder that it would come back after activating another sheet.

    This is more of a FYC because I suppose this is some Excel quirk that probably makes zero sense.

  18. #18
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    I've haven't a clue on that one.

  19. #19
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    No worries it's only aesthetics. I'm very glad anyway as you helped a lot!

  20. #20
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi there,

    Sorry to be a nuisance again, but I've got a question about this.

    Sometimes one client will have multiple subfolders in the main folder and they will be called e.g. 12345 John Smith, 12345 John Smith 2, 12345 John Smith 3 and so on.

    Do you think it's possible to capture a list of these folder names (if VBA finds more than one with number 12345) and give a choice to the user asking which one he wants to open (e.g. via a userform)? Or even open all of them for what it's worth?

  21. #21
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    This will open all subfolders that match.

    Please Login or Register  to view this content.
    If you want to make a userform with a listbox and offer options, I'll leave that project up to you.

  22. #22
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Brilliant!

    I got it to work together with a listbox. It was a lot of new stuff for me but I got through with it.

    What I've got is here below, I don't know how much of coding elegance it has, but it works! Thank you AlphaFrog yet again.

    Module:
    Please Login or Register  to view this content.
    Userform:
    Please Login or Register  to view this content.
    Last edited by cloudberr; 04-14-2016 at 05:41 PM.

  23. #23
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    It looks pretty elegant to me. Well done.

    One minor logic change; move this End If so all the counter and array code is within the If-End If code block. This would prevent a possible match with a file instead of a folder which the Dir function could do when using wildcards. That may or may not be applicable in this situation.

    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi Alfafrog,

    Thank you so much for VBA Code.

    Please advise based on Post#6

    It works fine if the "Test" folder is saved in C: drive.
    Just wondering, what if we move this "Test" folder to another location, this macro needs to be updated. Is there anyway I can make it global meaning where ever the Test folder is saved the macro will run?

    Thanks in advance.

    Mashiul

  25. #25
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Quote Originally Posted by mashiulalam View Post
    make it global meaning where ever the Test folder is saved the macro will run?
    Not sure what you're asking. Do you want to be prompted to choose the folder?

  26. #26
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi AlphaFrog,

    Please refer to your #6 post.

    Global means instead of main directory which is a specific location "C:\Test" can it be something like "\\Test" and macro will look for "Test" folder available anywhere in the computer?

    That way if this excel file is saved to USB or DVD it will not give any error.

    Is it little bit clear?

    Mash

  27. #27
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: Search a folder with sub folders to find and open a PDF based on partial file name.

    Hi AlphaFrog,

    Thanks very much.

    Mashiul

+ 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. Copy Files from one folder to different folders based on pdf file name
    By jtammyg in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-23-2014, 11:49 AM
  2. Find, open and work with file based on partial name
    By nteil in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-31-2013, 09:37 AM
  3. [SOLVED] Assigning Variable Based on Partial Name of an Open File
    By lsteinbach in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 05:28 PM
  4. search for a file in a folder based on data, copy and paste it in another folder
    By kboy1289 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 11:47 AM
  5. Find File with partial filename and get path of folder
    By axisambrosia in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2012, 06:51 AM
  6. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  7. Replies: 2
    Last Post: 03-26-2012, 07:12 PM
  8. Lookup & open file based on partial filename
    By Im2bz2p345 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2011, 07:04 PM

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