Simplifying hyperlink creation with VBA

imega

New Member
Joined
Jul 14, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, could someone please write me a macro for creating a hyperlink to a pdf document from folder "H:\Scan\pdf\". Everyday I'm adding many hyperlinks to pdf documents I scanned and its very tedious. It would be great if I could click a button and it would open a folder with my pdf files. Then I would chose the file I need and it would create the hyperlink in the active cell named after my chosen documents name.
Such macro would save me a lot of clicks :)) Thank you for Your help.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Welcome to the forum
This allows you to browse to a PDF and create a link in the active cell
VBA Code:
Sub InsertLink()
    Dim cel As Range, Addr As String, Disp As String, pdfPath As String
    pdfPath = "H:\Scan\pdf"
    Set cel = ActiveCell
    Addr = GetPDF(pdfPath)
    Disp = Replace(Replace(Addr, pdfPath & "\", ""), ".pdf", "")
    cel.Parent.Hyperlinks.Add Anchor:=cel, Address:=Addr, TextToDisplay:=Disp
End Sub

Private Function GetPDF(pdfPath As String) As String
  With Application.FileDialog(msoFileDialogOpen)
    .ButtonName = "Create Link"
    .initialFilename = pdfPath
    .Filters.Clear
    .Filters.Add "PDF (*.pdf)", "*.pdf", 1
    .Title = "Select PDF to link"
    .AllowMultiSelect = False
    If .Show = -1 Then GetPDF = .SelectedItems(1)
  End With
End Function

Additionally, if this code is placed in the SHEET code window (not a module like Module1), double clicking anywhere in column A triggers the code
(right click on sheet tab\ View Code \ paste the code below into that window)
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A2:A" & Rows.Count)) Is Nothing Then
        Cancel = True
        Call InsertLink
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top