Forum

VBA to match PDF fi...
 
Notifications
Clear all

VBA to match PDF file in folder and hyperlink to spreadsheet

5 Posts
2 Users
0 Reactions
141 Views
(@mittn)
Posts: 29
Eminent Member
Topic starter
 

I currently have this code below and it does nothing.

I have concated file names in column A.
example "OPS-GRO15-PR-HAC-001"

In my folder as per below, I have all the PDF file for each document number in column "A", but they may have a revision number on the end of the doc number
example OPS-GRO15-PR-HAC-001_r1

I need the code to look for the matching file name without the revision number on the end as this changes all the time and to link the PDF file to correct corresponding document number in column "A". hope this makes sense.

I don't know much about VBA, so I don't know what I need to do to fix the below code.

Sub AddHypaerlinks()

Dim lastRow As Long
Dim myPath As String, fileName As String

myPath = "\fs01Data$EngineeringDocument ControlSA - Eromanga BasinGrowlerPDF" 'SET TO WHERE THE FILES ARE LOCATED
lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow

fileName = myPath & Range("A" & i).Value & "*.pdf"

If Len(Dir(fileName)) 0 Then 'IF THE FILE EXISTS THEN

ActiveSheet.Hyperlinks.Add Range("I" & i), myPath & Dir(fileName)

End If

Next

End Sub

 
Posted : 25/01/2023 12:16 am
(@james1989)
Posts: 5
Active Member
 

Hi,

Before getting into the macro ... it would seem to me you need to handle the revision string ...

=SUBSTITUTE(yourText,RIGHT(yourText,3),"")

Hope this will help

 
Posted : 28/01/2023 5:36 am
(@mittn)
Posts: 29
Eminent Member
Topic starter
 

sorry, where do I put this?

 
Posted : 03/02/2023 9:28 pm
(@james1989)
Posts: 5
Active Member
 

Sorry ... my answer should have been clearer ...

Sub AddHyperlinks()
Dim lastRow As Long
Dim myPath As String, fileName As String
' example "OPS-GRO15-PR-HAC-001" in Column A

myPath = "\fs01Data$EngineeringDocument ControlSA - Eromanga BasinGrowlerPDF" 'SET TO WHERE THE FILES ARE LOCATED
lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow
fileName = myPath & Replace(Range("A" & i).Value, Right(Range("A" & i).Value, 3), "") & "*.pdf"
If Len(Dir(fileName)) > 0 Then ' IF THE FILE EXISTS THEN
ActiveSheet.Hyperlinks.Add Range("I" & i), myPath & Dir(fileName)
End If
Next i
End Sub

 
Posted : 06/02/2023 6:28 am
(@mittn)
Posts: 29
Eminent Member
Topic starter
 

Hi Sorry for the late reply, I added the code above and it appears not to do anything.

 
Posted : 15/02/2023 7:30 pm
Share: