Trebor8484
Board Regular
- Joined
- Oct 27, 2018
- Messages
- 69
- Office Version
- 2013
- Platform
- Windows
Hi,
The below code will loop through a folder of files and look for pdf's.
Each filename will contain a unique ID either 6 digits or 5 digits in length. What I would like to do is extract that numerical value, i.e. sample234567.pdf and assign my UniqueID variable into a string value 234567
There should only ever be a single numerical value in the filename so there may be an easier way to accomplish this rather than specifically testing the length of the numerical part of the string.
Thanks
The below code will loop through a folder of files and look for pdf's.
Each filename will contain a unique ID either 6 digits or 5 digits in length. What I would like to do is extract that numerical value, i.e. sample234567.pdf and assign my UniqueID variable into a string value 234567
There should only ever be a single numerical value in the filename so there may be an easier way to accomplish this rather than specifically testing the length of the numerical part of the string.
VBA Code:
Sub SelectFolder()
Dim fldr As FileDialog
Dim MyFolder As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim UniqueID As String
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> 0 Then
MyFolder = .SelectedItems(1)
Else
MsgBox "User Cancelled"
Exit Sub
End If
End With
Set objFolder = objFSO.GetFolder(MyFolder)
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
If Right(objFile.Name, 3) = "pdf" Then
End If
Next objFile
End Sub
Thanks