Ok, so this is what I've come up with, it uses the 'Temp' folder to add a "Dat", "Bat" & "Txt" file which, once processed, are deleted.
Option Explicit
Public Function UserName()
UserName = Environ$("UserName")
End Function
Function above is to find the username associated with the PC...
Private Sub IDLoad()
Application.ScreenUpdating = False
Sheets("FileOpen").Select
Range("E1").Clear
Range("E1").FormulaR1C1 = "=UserName()"
Range("E1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("G1").Select
Dim TextFile As Variant
Dim TempFldr As String, UN As String, workbook_subname As String, batfile As String, scriptfile As String, filescript As String, batyfile As String
Dim FileOpen_wb As Workbook
Dim ws As Worksheet
TempFldr = Environ("Temp") & "\"
If Sheets("FileOpen").Range("E1").text = "" Then
UN = "No User"
Else
UN = Sheets("FileOpen").Range("E1").text
End If
workbook_subname = "IDCheck"
scriptfile = TempFldr + workbook_subname + " - Script.dat"
filescript = TempFldr + workbook_subname + " - Script1.dat"
batfile = TempFldr + workbook_subname + " - upload.bat"
batyfile = TempFldr + workbook_subname + " - upload1.bat"
Set TextFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(filescript, True)
TextFile.writeline "idcheck@mywebsite.co.uk" 'username
TextFile.writeline "password" 'password
TextFile.writeline "cd GK" 'directory on FTP site
TextFile.writeline "text"
TextFile.writeline "get GKBlacklist.txt " & TempFldr & "GKBlacklist.txt"
TextFile.writeline "bye"
TextFile.Close
Set TextFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(batyfile, True)
TextFile.writeline "ftp -s:" + Chr(34) + filescript + Chr(34) + " www.mywebsite.co.uk"
TextFile.writeline "del " + Chr(34) + filescript + Chr(34)
TextFile.writeline "del " + Chr(34) + batyfile + Chr(34)
TextFile.Close
'Upload
Shell pathname:=Chr(34) + batyfile + Chr(34), windowstyle:=vbHide 'vbMinimizedNoFocus
'Export Cell Value to textfile
Set TextFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(TempFldr & UN & ".txt", True)
TextFile.writeline Sheets("FileOpen").Range("E1").text
TextFile.Close
'Create FTP files...
Set TextFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(scriptfile, True)
TextFile.writeline "idcheck@mywebsite.co.uk" 'username
TextFile.writeline "password" 'password
TextFile.writeline "cd GK" 'directory on FTP site
TextFile.writeline "text"
TextFile.writeline "get GKBlacklist.txt " & TempFldr & "GKBlacklist.txt"
TextFile.writeline "put " + Chr(34) + TempFldr & UN & ".txt" + Chr(34) 'file to be uploaded
TextFile.writeline "bye"
TextFile.Close
'Check if file is GKBlacklisted
Worksheets("FileOpen").Activate
Dim myFile As String, Data As String
myFile = TempFldr & "GKBlacklist.txt"
Dim r As Integer
If Dir(myFile) = "" Then
GoTo 1
End If
Open myFile For Input As #1
r = 0
Do Until EOF(1)
Line Input #1, Data
Worksheets("FileOpen").Range("F1").Offset(r, 0) = Data
r = r + 1
Loop
Close #1
1:
'Create script Files
Set TextFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(batfile, True)
TextFile.writeline "ftp -s:" + Chr(34) + scriptfile + Chr(34) + " www.mywebsite.co.uk"
TextFile.writeline "del " + Chr(34) + TempFldr & UN & ".txt" + Chr(34)
TextFile.writeline "del " + Chr(34) + scriptfile + Chr(34)
TextFile.writeline "del " + Chr(34) + batfile + Chr(34)
TextFile.Close
Shell pathname:=Chr(34) + batfile + Chr(34), windowstyle:=vbHide 'vbMinimizedNoFocus
Application.Run "BLV"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Below is the sub for checking the cell value and returning a message box if the username has been 'Blacklisted'
Private Sub BLV()
Application.ScreenUpdating = False
Dim i As Integer, intValueToFind As String
intValueToFind = Environ$("UserName")
For i = 1 To 30
If Cells(i, 6).Value = intValueToFind Then 'Remember to change row number!
Application.Visible = False
MsgBox "You are using an unlicensed version of this report." & vbCrLf & "Please contact my company to renew your license...", vbCritical + vbOKOnly, "My Company Ltd"
Application.DisplayAlerts = False
ActiveWorkbook.Close
Exit Sub
End If
Next i
Sheets("Data").Select
Range("F9").Select
Application.ScreenUpdating = True
End Sub
I have this called on both the Open and Close events of the worksheet in case an internet connection is not available when the workbook is opened,