hyperlink duplicates items and print out through folders and subfolders

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have invoices numbers in column H in sheet1 , some of them will repeat the same number and the invoices are existed within folder and subfolder in this directory "C:\Users\DANI\\Desktop\REPORTS\"

the files are PDF . so what I want macro to hyperlink the invoice numbers in column H and when click the cell contains invoice number then should open the file and show message if I want print out the file or not .
thank you
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I might be able to assist.

You say "some of them will repeat the same number." Does that matter? If it does matter how are the repeated invoice numbers affected or processed?

You mention pdf files in "folder and subfolder." Are all files in the REPORTS folder?
 
Upvote 0
Does that matter?
yes
If it does matter how are the repeated invoice numbers affected or processed?
every duplicated invoice will finish until last row (TOTAL) in column A .
each invoice will start from specific row and finish until lastrow(TOTAL) and will start new invoice after TOTAL row and repeat until lastrow(TOTAL)
and so on .
You mention pdf files in "folder and subfolder." Are all files in the REPORTS folder?
yes there are files in REPORTS folder , but there are many folders in REPORTS folder and contains pdf files .
 
Upvote 0
Sorry but I am now even more confused. Without a workbook or data to work with I cannot help. Maybe post a link to a workbook that has the realistic-but-fake data and results desired. Use Dropbox, 1 Drive, etc. to put your file on the Internet. Use the link icon above the message area to post the link.

If that is not possible then use Mr. Excel's excellent addin XL2BB to post portions of a worksheet. See HERE.
 
Upvote 0
Ok ,
see the picture . I made the TOTAL word in last row for column E as in highlight by red , not A as I have ever said,
and you can see repeated invoices numbers in the column H .
rte
EFGHIJK
1IITEMBATCHREF NOINV NOQTYPRICETOTAL
21BTS00INV-001121201440
32BTS01INV-001101101100
43BTS02INV-00152211105
54BTS03INV-001221222684
6TOTALINV-0016329
71BTS01INV-002121201440
82BTS02INV-002101151150
93BTS00INV-0025112560
10TOTALINV-0023150
ورقة1
Cell Formulas
RangeFormula
K7:K9,K2:K5K2=I2*J2
K6K6=SUM(K2:K5)
K10K10=SUM(K7:K9)

I hope this help you.
 
Upvote 0
I apologize for the delay.

there are files in REPORTS folder , but there are many folders in REPORTS folder and contains pdf files
How do I know which folder contains the report for a specific invoice? Are all invoice PDFs in the same folder?

How are files named? Is the one for invoice 1 like this INV-001.pdf?

So I am clear, you want a macro that adds hyperlinks that open to the PDF file for each invoice?

show message if I want print out the file or not
Is this necessary? Once the PDF is open the user may print it if she wants to without the message?
 
Upvote 0
How do I know which folder contains the report for a specific invoice? Are all invoice PDFs in the same folder?
as I said
nd the invoices are existed within folder and subfolder
so not all of files are existed in the same folder , also are existed in subfolders
example :
"C:\Users\DANI\\Desktop\REPORTS\inv-001.pdf"
"C:\Users\DANI\\Desktop\REPORTS\SALES\MAY\inv-002.pdf"
"C:\Users\DANI\\Desktop\REPORTS\SALES\JUNE\inv-002.pdf"

as you see the file is not just existed in the main folder REPORT , so will be in subfolders.
How are files named? Is the one for invoice 1 like this INV-001.pdf?
as in the picture NV-001 but be careful without extension as in picture .
So I am clear, you want a macro that adds hyperlinks that open to the PDF file for each invoice?
yes that's correct.
Is this necessary? Once the PDF is open the user may print it if she wants to without the message?
I'm afraid yes sometimes I don't want print directly, just open .
 
Upvote 0
I was able to write code to put the hyperlinks into the file as you requested. I cannot think of a good way to tell user that she may print the pdf. But that should not be a big issue because, as I said, she can print the pdf once it is open. You might/will have to change three settings in the code so it uses the correct 1. the header label in the column where invoice numbers are located, 2. worksheet (name) and 3. start folder (path).

The file is HERE.

VBA Code:
' ----------------------------------------------------------------
' Procedure Name: CreateHyperlinks
' Purpose: Add hyperlinks pointing to invoice pdf files for invoice number specified in the cell being processed.
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jim
' Date: 6/22/2023
' ----------------------------------------------------------------

Sub CreateHyperlinks()

'   Path to the folder containing the PDFs.
    Dim sPathAndFolderStart As String
    
'   Path to the folder containing the PDFs.
    Dim sPathAndFolderFound As String
    
'   Name of the file to open.
    Dim sFileName As String

'   Header label for the "invoice number" column (INV NO)
    Dim sHeader As String
    
'   Used to iterate through the rows requiring a hyperlink.
    Dim iRow As Long
    
'   Worksheet containing the invoice number.
    Dim wsInvoices As Worksheet
    
'   Cell into which the link is placed.
    Dim rAnchorCell As Range
    
'   Header label in the column in the table for invoice number.
    sHeader = "INV NO"  '<= Change this if the INV NO header label in the worksheet is changed.
    
'   Set worksheet object to point to the worksheet.
    Set wsInvoices = Worksheets("Sheet1") '<= Change this if the name of the worksheet changes.
   
'   "Start" folder in which files and subfolders are located.
    sPathAndFolderStart = "C:/Users/Jim/Desktop/REPORTS/" '<= Change this to point to the correct folder on your computer.
        
'   Use Find to locate the cell containing the text in string sHeader.
    Set rAnchorCell = wsInvoices.Cells.Find(What:=sHeader, _
            After:=wsInvoices.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=True, _
            SearchFormat:=False)
    
'   If the header label was not found then tell user and exit sub.
    If rAnchorCell Is Nothing _
    Then
        MsgBox "The header with the label " & sHeader & Chr(10) & "was not found in worksheet " & wsInvoices.Name, vbInformation
        Exit Sub
    End If
    
    iRow = 0

'   Iterate through invoice numbers and add the hyperlinks in the INV NO column
    Do
        iRow = iRow + 1
        
'       File name is the value in the anchor cell plus the .pdf extension.
        sFileName = rAnchorCell.Offset(iRow) & ".pdf"
        
'       Locate the file in a folder. Puts path into ByRef var named sPathAndFolderFound.
        Call FindFile(sPathAndFolderStart, sFileName, sPathAndFolderFound)
    
            sPathAndFolderFound = sPathAndFolderFound & "\"
        
            If rAnchorCell.Offset(iRow) <> "" _
             Then
    
                With wsInvoices
            
                    .Hyperlinks.Add _
                        Anchor:=rAnchorCell.Offset(iRow), _
                        Address:=sPathAndFolderFound & sFileName, _
                        TextToDisplay:=rAnchorCell.Offset(iRow).Value, _
                        ScreenTip:="Open invice PDF"
            
                End With
    
            End If
        
    Loop Until rAnchorCell.Offset(iRow + 1) = ""
    
End Sub


VBA Code:
Option Explicit
Dim FileSystem As Object
Dim sHostFolder As String

Function FindFile(psRoot As String, psFileName As String, psFoundFolder)
    
    sHostFolder = psRoot

    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    
    FindFolder FileSystem.GetFolder(sHostFolder), psFileName, psFoundFolder

End Function

Function FindFolder(psFolder, psFileName, psFoundFolder)

    Dim SubFolder

    Dim File
    
    For Each SubFolder In psFolder.SubFolders
        FindFolder SubFolder, psFileName, psFoundFolder
    Next
    
    For Each File In psFolder.Files
        If UCase(File.Name) = UCase(psFileName) Then
            
            psFoundFolder = psFolder.Path
            
            Exit Function
        End If
    Next

End Function
 
Upvote 0
I was able to write code to put the hyperlinks into the file as you requested.
Perfect !
I cannot think of a good way to tell user that she may print the pd
ok
just last thing : may you cancel message when click the invoice , just open file directly ?
 
Upvote 0
I do not understand your request. The hypelinks do "open the file directly." What message are you seeing?
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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