VBA - How to locate and print file

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
I have an excel userform in which users enter data then it is transferred to a sheet called "Database"

One field I have in the database is Column S which is "Certificate". This is a PDF file that needs to be automatically printed.

1626191317919.png


This certificate is stored in the following location: "T:\Engineering\Certs\"

What i want is a button which automatically finds the certificate and prints it. i figured the best way to find the right certificate is by using a unique field in the database which is "works order number". Therefore i would like users to enter Works Order number in Cell "C3" of the "PrintDocuments" sheet. Like below.

1626190884707.png


I will then have a button, which finds this works order number in the database, then prints the certificate from column S by opening the location "T:\Engineering\Certs\" then finds the name of the PDF as it is stored in column S. opens it and prints it. I have made a start to this code, but i'm not sure how to do the bit in the middle, which is to go to the location and print the correct pdf

Any help would be appreciated, see code below:

VBA Code:
Dim iRow As Long 'Variable to hold the starting row and loop through all records in database
Dim sh As Worksheet 'worksheet variable to refer  to where database is stored
Dim myValue As Variant
Dim WorksOrder As String
Dim Found As Boolean


'Get user entered WorksOrder Number

WorksOrder = ThisWorkbook.Sheets("PrintDocuments").Range("C3").Value


'Set worksheet

Set sh = ThisWorkbook.Sheets("Database")

iRow = 2 'row in which data starts from in database

Found = False

Do While sh.Range("A" & iRow).Value <> ""  'loop through until no data is found (last row of database)
 
'works order number is found in column D of database sheet

If WorksOrder = sh.Range("D" & iRow).Value Then

Found = True

'insert pdf code here




Exit Do

End If

iRow = iRow + 1

Loop


If Found = True Then
MsgBox ("Certificate Printed")
Else
MsgBox ("Works Order Number Not Found")
End If

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Your request is very similar to the request in the thread below, you really should have a look at the code that has become available there.

 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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