Hi Team,
following scenario:
I have a source folder, containing PDF files. Location would look like: Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_containing_all_the_pdfs\*.pdf
The naming convention for the pdf files, is Account number_Invoice number: 123456_654321.pdf
A macro is already creating folders, named for the account number. Here in that example: A folder with name 123456 would exist.
The location for that folder would look like this:
Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_with_the_account_folders\123456\
I tried to find a solution to move the pdf files, based on the part of their account number to their respective folders.
I have found a piece of code I thought it would be suitable:
Sub MoveFiles()
Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strMyFolder As String
Application.ScreenUpdating = False
strMyFolder = "Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_containing_all_the_pdfs" '<--Directory path where the relevant PDF files are.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strMyFolder)
For Each objMyFile In objMyFolder.Files
'If the current file's extension is 'pdf' and it has a underscore in its name, then...
If objFSO.GetExtensionName(objMyFile) = "pdf" And InStr(objMyFile.Name, "_") > 0 Then
'check if the directory exists. If it doesn't then...
If objFSO.FolderExists(strMyFolder & "\" & Left(objMyFile.Name, WorksheetFunction.Search("_", objMyFile.Name) - 1)) = False Then
'...inform the user.
MsgBox "Cannot move the file"
'Else...
Else
'...move the pdf to the folder
objFSO.MoveFile (strMyFolder & "\" & objMyFile.Name), strMyFolder & "\" & Left(objMyFile.Name, WorksheetFunction.Search("_", objMyFile.Name) - 1)
End If
End If
Next objMyFile
Set objFSO = Nothing
Set objMyFolder = Nothing
Application.ScreenUpdating = True
End Sub
But it throws the Message box that it "Cannot move the file" and I think that is based on the fact that source and and location of the file are not the same directory.
How can I implement this information in above code or is there even a better solution?
Thank you all for your time!
following scenario:
I have a source folder, containing PDF files. Location would look like: Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_containing_all_the_pdfs\*.pdf
The naming convention for the pdf files, is Account number_Invoice number: 123456_654321.pdf
A macro is already creating folders, named for the account number. Here in that example: A folder with name 123456 would exist.
The location for that folder would look like this:
Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_with_the_account_folders\123456\
I tried to find a solution to move the pdf files, based on the part of their account number to their respective folders.
I have found a piece of code I thought it would be suitable:
Sub MoveFiles()
Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strMyFolder As String
Application.ScreenUpdating = False
strMyFolder = "Z:\aaaa\bbb\ccccc\2022\ddddddd\eeeeeee\ffffff\this_is_the_folder_containing_all_the_pdfs" '<--Directory path where the relevant PDF files are.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strMyFolder)
For Each objMyFile In objMyFolder.Files
'If the current file's extension is 'pdf' and it has a underscore in its name, then...
If objFSO.GetExtensionName(objMyFile) = "pdf" And InStr(objMyFile.Name, "_") > 0 Then
'check if the directory exists. If it doesn't then...
If objFSO.FolderExists(strMyFolder & "\" & Left(objMyFile.Name, WorksheetFunction.Search("_", objMyFile.Name) - 1)) = False Then
'...inform the user.
MsgBox "Cannot move the file"
'Else...
Else
'...move the pdf to the folder
objFSO.MoveFile (strMyFolder & "\" & objMyFile.Name), strMyFolder & "\" & Left(objMyFile.Name, WorksheetFunction.Search("_", objMyFile.Name) - 1)
End If
End If
Next objMyFile
Set objFSO = Nothing
Set objMyFolder = Nothing
Application.ScreenUpdating = True
End Sub
But it throws the Message box that it "Cannot move the file" and I think that is based on the fact that source and and location of the file are not the same directory.
How can I implement this information in above code or is there even a better solution?
Thank you all for your time!