Reputation:
I have a little problem with my code in VBA, How can I exclude the other extension file like .txt, .csv, .xlsx, and .xlsm
so I can select only in my For Each Loop
is the .PDF
extension only.
I've already searched about this issue and already tried, But the solution is not applicable in my code.
This is my code:
Option Explicit
Sub GetPDFFile()
Dim mainWs As Worksheet
Dim pdfPath As Variant, excelPath As Variant
Dim fileSystemObject As New fileSystemObject
Dim getFolderPath As Folder
Dim getFile As file
Dim wb As Workbook
Set mainWs = ThisWorkbook.Sheets("Main")
pdfPath = mainWs.Range("C7").Value
excelPath = mainWs.Range("C8").Value
'Set all variable to null
Call SetNothing
If pdfPath = "" Then
Call MsgActionInvalid("Please input PDF File Folder.")
ElseIf excelPath = "" Then
Call MsgActionInvalid("Please input Output Folder Location.")
Else
Set getFolderPath = fileSystemObject.getFolder(pdfPath)
Set wa = CreateObject("word.application")
If cntFiles <> 0 Then
For Each getFile In getFolderPath.Files
`Other code............
Next
End Sub
I'm getting all the files inside the folder that I've selected. So inside the For Each Loop
I'm getting a debug message because the file is not .PDF
.
Any ideas about this guys?
Thanks in advance.
Upvotes: 3
Views: 871
Reputation: 57693
Use the Type property of the File object like getFile.Type
to find out its type. And use a If statement to run your Other code............
only on the desired type of files.
Alternatively use UCase(getFile) Like "*.PDF"
to make sure that it is not case sensitive. Otherwise you only trigger .PDF
but not .Pdf
or .pdf
or .pDf
or whatever.
Which is the same as UCase(Right$(getFile, 4)) = ".PDF"
Upvotes: 1
Reputation: 4099
You should be using Right
to check the file extension. Something like:
For Each getFile In getFolderPath.Files
If Right(getFile, 4) = ".pdf" Then
' have found a PDF extension............
End If
Next
Regards,
Upvotes: 1