Why I cannot use the wildcard while opening a pdf file?

Ongbey

New Member
Joined
Oct 16, 2018
Messages
29
Office Version
  1. 2013
Hi everyone,

I cannot open a pdf file starting a specific text. Could you please check below code?

--------------------------------------------------------
file="123456ABC"
file = file & "*"
tgtfile = "\\filesrv\d\Serialproduction\" & file & ".pdf"
CreateObject("Shell.Application").Open (tgtfile) & vbNullString
------------------------------------------------------

"Serialproduction" folder consist of following files:
136547.pdf
123456ABC_RevA.pdf
896534L.pdf
896534E.pdf
----------------------------------------------------
I need to open a file starting with "123456ABC". That is all, but I cannot success it in Excel 2013 VBA.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You'll need to loop through the files in the directory and open each file within the loop.
 
Upvote 0
You'll need to loop through the files in the directory and open each file within the loop.
Bu I want to open one file. Generally, one file meet our search. So, only Rev part change such as _RevA, _RevB, _RevC.
 
Upvote 0
So you want to open ONE file but dont know if it ends RevA or RevB or RevC ?
Again, loop through the directory searching the filenames for the pattern you want
Use VBA Instr() to check for "_Rev", if it matches open that file.
 
Upvote 0
Bu I want to open one file. Generally, one file meet our search. So, only Rev part change such as _RevA, _RevB, _RevC.
Just to clarify, if only one file matches the requirements, the loop will only open one file.
So don't concern yourself that a loop is being involved and you only want to open one file.
 
Upvote 0
In that case, could you please write a loop for me? I am not familiar those loop issues. Thanks.
 
Upvote 0
Try this:
VBA Code:
file="123456ABC"
file = file & "*"
tgtfile = Dir("\\filesrv\d\Serialproduction\" & file & ".pdf")

If tgtfile <> vbNullString Then
    CreateObject("Shell.Application").Open ("\\filesrv\d\Serialproduction\" & tgtfile) 
End If
 
Upvote 0
Solution
Try this:
VBA Code:
file="123456ABC"
file = file & "*"
tgtfile = Dir("\\filesrv\d\Serialproduction\" & file & ".pdf")

If tgtfile <> vbNullString Then
    CreateObject("Shell.Application").Open ("\\filesrv\d\Serialproduction\" & tgtfile)
End If
Thank you, it works.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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