Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

open different files extensions

0

hello

I  try opening   any  file  extension  when  press specific  cell in  column E   .  so  when  write  the  file  name  in column A  will  create automatically  hyperlink  in  column E  and show  OPEN file  .so  far  it  doesn't  open  any  file  extension.

any  idea  please?

Answer
Discuss

Discussion

Ala. 

Column A contains entries like 1, 2 and so on (with no file extensions). Do you really have (and expect to open) pdf files named like that?
John_Ru (rep: 6142) Mar 2, '22 at 10:43 am
the  files  name  in  column A  links  with  column E  . the  column E  create hyperlink   based on  file  name  into  column A  .when press  the cell contains hyperlink into column E should  open it . not  just  open  PDF  also  docx,jpg,gif....
Alaa (rep: 28) Mar 2, '22 at 11:08 am
Okay, I only glanced . Might have chance to look tomorrow 
John_Ru (rep: 6142) Mar 2, '22 at 11:39 am
no problem . take  your  time 
Alaa (rep: 28) Mar 2, '22 at 12:00 pm
Add to Discussion

Answers

0
Selected Answer

Alaa

The problem with your code is that you create a filename stPath using the * wildcard (for one or more characters as the file extension). The VBA command Dir finds a file like that but then you try to write the hyperlink with the wildcard still in place (and there's no such file to open).

To correct that (as in the revised file attached), you just need to make the change in bold below:

Sub UpdatePDFLink(c As Range)
    Dim stPath As String
    Application.EnableEvents = False

    stPath = ActiveWorkbook.Path & "\" & c.Value & ".*"
    c(1, 5).Hyperlinks.Delete
    If c = "" Then
        c(1, 5) = ""
    Else
        If Dir(stPath) <> "" Then
            'recreate stPath with full filename
            stPath = ActiveWorkbook.Path & "\" & Dir(stPath)
            c(1, 5).Hyperlinks.Add c(1, 5), stPath, , , "Open file"
        Else
            c(1, 5) = "File does not exist"
        End If
    End If
    Application.EnableEvents = True
End Sub

That's because the command Dir(stPath) returns a string which is the full filename (e.g. 11.doxc) which can create to working hyperlink (...|11.doxc rather than ...|11.*). Now, if you type a file name (without the extension, whatever it is) in column A, a hyperlink will be added to column E.

Also I've changed the statement "the  file  is not existed" for the more usual (in English) statement "File does not exist". I also tweaked the headings in row 1.

On a minor point, in the Module 1 sub UpdateAllPDFLinks, there is a line:

....
        For Each c In Intersect(.UsedRange, .Range("A:A"))
            Call UpdatePDFLink(c)
        Next c

where the bit in bold uses every used cell in column A (including A1, E1 got over-wriitten as "the  file  is not existed"). I've chnaged that portion for the bit in bold below:

Sub UpdateAllPDFLinks()
    Dim c As Range
    With Worksheets("sh1")
        For Each c In .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
            Call UpdatePDFLink(c)
        Next c
    End With
End Sub

Hope this helps and fixes your problem.

Discuss

Discussion

perfect !  I  appreciate  your help .
Alaa (rep: 28) Mar 3, '22 at 2:32 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login