Function GetSerialNumber(Optional FilePath As String, Optional FileName As String) As String
Dim DefaultName As String
Dim DefaultPath As String
Dim FSO As Object
Dim SeqNum As Variant
Dim TxtFile As Object
Dim TargetCell As Object
DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
DefaultName = "Invoice"
FilePath = IIf(FilePath = "", DefaultPath, FilePath)
FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
FileName = IIf(FileName = "", DefaultName, FileName)
FileName = FilePath & IIf(InStr(1, FileName, ".") = 0, FileName & ".txt", FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
'Open the file for Reading and Create the file if it doesn't exists
Set TxtFile = FSO.OpenTextFile(FileName, 1, True, 0)
'Read the serial number
If Not TxtFile.AtEndOfStream Then SeqNum = TxtFile.ReadLine
TxtFile.Close
'Update the serial number
Set TxtFile = FSO.OpenTextFile(FileName, 2, False, 0)
SeqNum = Format(IIf(SeqNum = "", "1", Val(SeqNum) + 1), "000000")
TxtFile.WriteLine SeqNum
TxtFile.Close
GetSerialNumber = SeqNum
With ThisWorkbook.Sheets
With Cells(7, 11)
If IsEmpty(.Value) Then
.Value = Date
.NumberFormat = "dd mmm yyyy"
End If
End With
Set FSO = Nothing
Set TxtFile = Nothing
End With
End Function
Function MinusOne(Optional FilePath As String, Optional FileName As String) As String
Dim DefaultName As String
Dim DefaultPath As String
Dim FSO As Object
Dim SeqNum As Variant
Dim TxtFile As Object
Dim TargetCell As Object
DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
DefaultName = "Invoice"
FilePath = IIf(FilePath = "", DefaultPath, FilePath)
FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
FileName = IIf(FileName = "", DefaultName, FileName)
FileName = FilePath & IIf(InStr(1, FileName, ".") = 0, FileName & ".txt", FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
'Open the file for Reading and Create the file if it doesn't exists
Set TxtFile = FSO.OpenTextFile(FileName, 1, True, 0)
'Read the serial number
If Not TxtFile.AtEndOfStream Then SeqNum = TxtFile.ReadLine
TxtFile.Close
'Update the serial number
Set TxtFile = FSO.OpenTextFile(FileName, 2, False, 0)
SeqNum = Format(IIf(SeqNum = "", "1", Val(SeqNum) - 1), "000000")
TxtFile.WriteLine SeqNum
TxtFile.Close
MinusOne = SeqNum
With ThisWorkbook.Sheets
With Cells(7, 11)
If IsEmpty(.Value) Then
.Value = Date
.NumberFormat = "dd mmm yyyy"
End If
End With
Set FSO = Nothing
Set TxtFile = Nothing
End With
End Function
Function CheckIFVoid(Optional FilePath As String, Optional FileName As String) As String
Dim MyFileCount As Integer
Dim DefaultPath As String
Dim TargetCell As Object
Dim Execute As Variant
DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
Set TargetCell = Sheets(1).Range("K5")
With Application.FileSearch
.NewSearch
.LookIn = DefaultPath
.FileName = TargetCell
MyFileCount = 0
If .Execute() > 0 Then
Sheets(1).Range("K5") = MinusOne()
End If
If .Execute() = 0 Then
MsgBox ("nothing found")
End If
End With
End Function
Sub Auto_Open()
If Cells(5, 11).Value = "" Then
Sheets(1).Range("K5") = GetSerialNumber()
End If
If Cells(5, 11).Value >= 0 Then
Sheets(1).Range("K5") = CheckIFVoid
End If
End Sub
Bookmarks