Public invoiceList As String
Public VRFolderPath As String 'There is another function which runs first and sets VRFolderPath
Public currentDateAndTime As Date '(There is another function which runs first and sets currentDateAndTime
Sub getUnpaidInvoicesForPreviousMonths(ByVal currentMonth As String, ByVal Client As String)
Dim currentWorkBook As Workbook
Dim folderToLookup As String
Dim mySplit As Variant
Dim i As Long
Dim itemString As String
Dim lastSep As String, extensionSepLocation As Integer
Dim month As String
invoiceList = ""
'Lookup Unpaid Invoices for the last 12 months
For i = -11 To -1
If Day(currentDateAndTime) < 15 Then
month = Format(DateAdd("m", i - 1, currentDateAndTime), "mmmm")
Else
month = Format(DateAdd("m", i, currentDateAndTime), "mmmm")
End If
folderToLookup = Replace(VRFolderPath, "/OneDrive", "/OneDrive/" & month & "/Unpaid")
folderToLookup = MacScript("tell text 1 thru -2 of " & Chr(34) & folderToLookup & Chr(34) & " to return quoted form of it's POSIX path")
addInvoicesInFolderToArray folderToLookup, Client
Next i
'Populate unpaid invoices for last 11 months
If invoiceList <> "" Then
With Application
.ScreenUpdating = False
End With
Sheets("Client Statement").Select
Range("C27:I34").ClearContents
mySplit = Split(invoiceList, Chr(13))
For i = LBound(mySplit) To UBound(mySplit)
On Error Resume Next
itemString = mySplit(i)
lastSep = InStrRev(itemString, Application.PathSeparator, , 1)
extensionSepLocation = InStrRev(itemString, ".", , 1)
If i <= 7 Then
Sheets("Client Statement").Cells(i + 27, 3).Value = Left(Mid(itemString, lastSep + 1, Len(itemString)), 5)
Sheets("Client Statement").Cells(i + 27, 4).Value = DateSerial(Year(Now), Left(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2), Right(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2))
Sheets("Client Statement").Cells(i + 27, 5).Value = Right(Left(itemString, InStrRev(itemString, ".") - 1), Len(Left(itemString, InStrRev(itemString, ".") - 1)) - InStrRev(itemString, " "))
Else
Sheets("Client Statement").Cells((i - 8) + 27, 7).Value = Left(Mid(itemString, lastSep + 1, Len(itemString)), 5)
Sheets("Client Statement").Cells((i - 8) + 27, 8).Value = DateSerial(Year(Now), Left(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2), Right(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2))
Sheets("Client Statement").Cells((i - 8) + 27, 9).Value = Right(Left(itemString, InStrRev(itemString, ".") - 1), Len(Left(itemString, InStrRev(itemString, ".") - 1)) - InStrRev(itemString, " "))
End If
Set currentWorkBook = Application.Workbooks.Open(itemString)
With currentWorkBook
.Activate
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Left(itemString, extensionSepLocation) & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
.Close savechanges:=False
End With
On Error GoTo 0
Next i
With Application
.ScreenUpdating = True
End With
Else
MsgBox "There are no unpaid invoices for this client for the previous month of " & currentMonth & "."
With Application
.ScreenUpdating = True
End With
End If
End Sub
Bookmarks