VBA to open a PDF in another location.

IannW

New Member
Joined
Aug 25, 2018
Messages
18
Hi All

I have the following code that searches a network folder for a PDF doc based on the number in a cell and opens it if one exists:

Sub OpenPDF_3()

Dim strPath As String
Dim strFileName As String

strPath = "\\172.27.154.13\D$\Excel Dashboard\Inventory"
If Right(strPath, 1) <> "" Then
strPath = strPath & ""
End If

strFileName = Worksheets("Home").Range("AW15").Value & " New Store Inventory" & ".pdf"

If Len(Dir(strPath & strFileName)) > 0 Then
ActiveWorkbook.FollowHyperlink strPath & strFileName
Else
MsgBox "There is no Inventory available for this store!", vbExclamation
End If

End Sub

This works great if the pdf is named '333 New Store Inventory', 333 being the number that i enter in cell AW15 before clicking the button that runs this code.

However, if the doc is saved as '0333 New Store Inventory' because we often use 4 digits, it wont open.

How can I ignore any leading zeros and have it open weather its called 333 or 0333.

Probably simple to a vba genius but not a newbie like me.

Thanks in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm not a code expert, there may be better ways of doing this, and this might not work correctly for all your different scenarios, but it seems to work for the scenario you have described . . .

strFileName = Worksheets("Home").Range("AW15").Value + 0 & " New Store Inventory" & ".pdf"
 
Upvote 0
Just tried it, doesn't seem to work.

Sub OpenPDF_3()


Dim strPath As String
Dim strFileName As String

strPath = "\\172.27.154.13\D$\Excel Dashboard\Inventory"
If Right(strPath, 1) <> "" Then
strPath = strPath & ""
End If

strFileName = Worksheets("Home").Range("AW15").Value + 0 & " New Store Inventory" & ".pdf"

If Len(Dir(strPath & strFileName)) > 0 Then
ActiveWorkbook.FollowHyperlink strPath & strFileName
Else
MsgBox "There is no Inventory available for this store!", vbExclamation
End If

End Sub
 
Upvote 0
Sorry, I did it the wrong way round, I was assuming the file would always be called "333 New Store . . ."

But you mean something different don't you ?

The file may be called "0333 New Store . . . "
and the user may input 333 into AW15.

Leave it with me.
 
Upvote 0
Sorry, this one is beyond my limited VBA skills.

I can think of how the code would be structured, but can't quite get the exact syntax right.

Any code experts out there who can help ?
 
Upvote 0
333 New Store.... works great with my original code, but really we have always used a 4 digit code as some stores are named as 9453 for example. People complete an excel spreadsheet and click a button which converts to file to a pdf and saves it to the folder mentioned in the strpath. The cell with the store number is formatted to 0000, so always saves as a 4 digit, even if they enter 333, it saves as 0333, but my code above that calls the pdf doesn't like the 0. I could go through and change them all to 3 digits but would really like to leave at 4.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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