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?
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?
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.