+ Reply to Thread
Results 1 to 11 of 11

Macro to create hyperlinks to files.

  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Macro to create hyperlinks to files.

    Hello, I need help creating a macro that will create a hyperlink to each file in a folder full of documents. The hyperlinks are going to be in column F of a database that catalogs some of the information in these files. In column A is a unique number that will match the first few digits of the file the row pertains to. for example, cell A1 contains the number 147 and the corresponding file begins with 147*********. The numbers will always match and they will never repeat. The path to the folder is c:\files. If the macro could start at the next empty cell in column F to prevent having to create the links that are already created that would be great.

    Thanks!
    Last edited by dcgrove; 02-01-2010 at 04:35 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro to create hyperlinks to files.

    Hi

    So in c:\files you have multiple files of the format 147???.???, and you want a macro to create a hyperlink to those files, starting with the first link in F, then G, then H....

    Is that correct?

    rylo

  3. #3
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Macro to create hyperlinks to files.

    the files in c:\files start at 147????.pdf and will increase in number sequentially. so the next one that is added to the directory will be 148????.pdf and so forth. the number in column A will always be the first in the beginning of the file name. so if a1 is 147 in f1 I would need a link to c:\files\147????.pdf. and in A2 there was "148", I would need the link in F2 to be 148?????.pdf. The numbers in column will not always be in sequential order depending on how the spreadsheet is sorted. If the macro required it to be in order, i could sort the sheet.

    Does that explain it a little better?

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro to create hyperlinks to files.

    Hi

    Try this.

    Please Login or Register  to view this content.
    rylo

  5. #5
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Macro to create hyperlinks to files.

    I will give it a shot in the morning when I get back to work.

    Thanks!

  6. #6
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Macro to create hyperlinks to files.

    That worked great! is there anyway to prevent it from inserting the link if the file does not exist? the way my database is set up, Column A is numbered from 1-1000 and some of the files between 1 and 187 are missing so when I run the macro, it inserts a link for the missing files to the folder all the way down to 1000.

    Also, is there any way to make it start on the next empty cell in row "F" so that it will skip the cells with a link already?

    Thanks!
    Last edited by dcgrove; 02-02-2010 at 11:27 AM.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to create hyperlinks to files.

    This version of the macro above will only create new hyperlinks in column F if there isn't something in that cell already. This will allow you to delete broken links anytime and the next time the macro runs it will always check that blank to see if there is a match now.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 02-02-2010 at 12:08 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  8. #8
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Macro to create hyperlinks to files.

    JBeaucaire, your macro worked for the most part. On rows where the numbers in column A are the similar it is inserting incorrect links. On row 3 for example, the number "3" is in cell A3 but it linked to the file that starts with "30". I have attached a worksheet with some examples. the highlighted examples are incorrect and are the results of running the macro.

    Thanks!
    Attached Files Attached Files

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to create hyperlinks to files.

    The macro is working as requested, it finds a file in the specified directory that starts with the string in column A and creates a hyperlink.

    So, based on the examples I see of your actual filenames, you'll need to resolve the logic you want the macro to use.

    30 does match to 3*.
    100 does match to "1*", "10*", "100*", "100 *", "100, " etc...

    So you'll need to cleanup the matching method you want to employ and present it here so I can assist you in making the macro use that matching logic.

    Be thorough. You can see your file names in total, so work out a match logic that resolves all the variances in your naming structure. Matching to "numbers" like this is always going to be troublesome, in some ways.

  10. #10
    Registered User
    Join Date
    01-31-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Macro to create hyperlinks to files.

    Hi All,

    Am a complete VBA newbie and have totally jumped into the deep end with a work project. I have a wokbook that records an individual id number for each job that i log in Column J. A copy of this is saved on my computer in the File "CAP TEST". What i need is a macro that will automatically search "CAP TEST" find the job id and create a hyperlink from that file to the workbook which is acting as a database.

    I think that the code below will do what i want but i keep getting an error 52 'Bad Filename or number' and i'm not sure how to fix it. The job id id in the form ABC-DEF-HIJ-0123, could this be the issue?

    Am happy to send sheet through if this will help?

    Many Thanks


    Please Login or Register  to view this content.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to create hyperlinks to files.

    Click on DEBUG, what line of code is highlighted?

    In the CTRL-G Immediate Window, type this:

    ? fPath & fName

    What is the exact result? Does it match exactly to the full path and filename? Check the 0s and the 1s and make sure they aren't Os and Is (vowels) and vice versa. Excel is telling you there is a naming error.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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