Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public lngAgents As Long
Public lngOutputCol As Long
Public Sub Swarm_Method(shtData As Worksheet, sProperty_RangeAddr, Optional boolOneAgentOnly As Boolean)
Dim lngCurRec As Long
Dim lngCurAgt As Long
Dim rngOutput As Range
'Setup variables
With shtData.Range(sProperty_RangeAddr)
lngRecords = .End(xlDown).Row - .Row + 1
vAddresses = .Resize(lngRecords)
End With
lngAgents = [SwarmSize]
lngOutputCol = Range(sProperty_RangeAddr).Offset(, 1).Column
'Create VBScript Agent Swarm
For lngCurRec = 1 To UBound(vAddresses)
DoEvents
Set rngOutput = Range(sProperty_RangeAddr).Offset(lngCurRec - 1, 1)
If Len(rngOutput) = 0 Then
DoEvents
CreateVBScriptAgentAndLaunch rngOutput
lngCurAgt = lngCurAgt + 1
If boolOneAgentOnly Or lngCurAgt = lngAgents Then Exit For
Sleep 5
End If
Next lngCurRec
' Terminate objects
Set rngOutput = Nothing
End Sub
Public Sub CreateVBScriptAgentAndLaunch(rngOutput As Range)
Dim sFileName As String
Dim intFileNum As Integer
Dim shellWin As New ShellWindows
Dim s As String
Dim lngRow As Long, sCol As String, sPropAddr As String, LastCheckpoint, Account As String
Dim lngAgentNumber As Long
' Setup variables
lngRow = rngOutput.Row
sCol = rngOutput.Column
LastCheckpoint = rngOutput.Resize(, 1).Address
Account = rngOutput.Resize(, 2).Address
sPropAddr = rngOutput.Offset(, -1)
DoEvents
' Set semaphore
lngAgentNumber = lngRow Mod lngAgents
rngOutput = "Agent_" & lngAgentNumber
DoEvents
' Create string for contents of VBScript file
DoEvents
s = s & "Dim oXML, oXL, curRow, outputCol, propAddress, sHTML, i" & vbCrLf
's = s & "Dim vResults(7)" & vbCrLf
s = s & vbCrLf
' Setup variables" & vbCrLf
s = s & "curRow = " & lngRow & vbCrLf
s = s & "outputCol = """ & sCol & """" & vbCrLf
s = s & "propAddress = """ & sPropAddr & """" & vbCrLf
s = s & "Set oXL = GetObject(, ""Excel.Application"")" & vbCrLf
s = s & "Set oXML = WScript.CreateObject(""MSXML2.XMLHTTP"")" & vbCrLf
s = s & vbCrLf
' Navigate to property page
s = s & "oXML.Open ""GET"", ""http://npts2.apis.dhl.com:6010/npts/ShipmentDataFetchServlet?action=8&shipDetFromSumm="" & propAddress, false" & vbCrLf
s = s & "Wscript.Sleep 50" & vbCrLf
s = s & "oXML.send" & vbCrLf
s = s & "Wscript.Sleep 50" & vbCrLf
s = s & vbCrLf
' Get html
s = s & "sHTML = oXML.responseText" & vbCrLf
s = s & vbCrLf
' Get html
s = s & "IEhtml2 = Split(sHTML, vbCrLf)" & vbCrLf
s = s & "On Error Resume Next" & vbCrLf
' Parse fields
'------------------------------------------------------------------------------------------------------------------------
'Remarks
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Remarks"") Then" & vbCrLf
s = s & "Remark = IEhtml2(a + 27)" & vbCrLf
s = s & "Remark = Replace(Remark, chr(34), """")" & vbCrLf
s = s & "Remark = Replace(Remark, "" "", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""<"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""="", """")" & vbCrLf
s = s & "Remark = Replace(Remark, "">"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""&"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, "";"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""/a"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""/td"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""("", """")" & vbCrLf
s = s & "Remark = Replace(Remark, "")"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""'"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""tdclassgrayTdNormalheight21nbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'Piece
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""addZero"") Then" & vbCrLf
s = s & "Piece = IEhtml2(a)" & vbCrLf
s = s & "Piece = Replace(Piece, chr(34), """")" & vbCrLf
s = s & "Piece = Replace(Piece, "" "", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""<"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""="", """")" & vbCrLf
s = s & "Piece = Replace(Piece, "">"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""&"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, "";"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""/a"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""/td"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""("", """")" & vbCrLf
s = s & "Piece = Replace(Piece, "")"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""'"", """")" & vbCrLf
s = s & "Piece = Replace(Piece, propAddress, """")" & vbCrLf
s = s & "Piece = Replace(Piece, ""tdclasswhiteTdNormalheight21aligncenternbspahrefjavascript:pieceClickedaddZero"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'------------------------------------------------------------------------------------------------------------------------
'Last Checkpoint
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Last Checkpoint Summary"") Then" & vbCrLf
s = s & "Remark = IEhtml2(a + 2)" & vbCrLf
s = s & "Remark = Replace(Remark, chr(34), """")" & vbCrLf
s = s & "Remark = Replace(Remark, "" "", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""<"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""="", """")" & vbCrLf
s = s & "Remark = Replace(Remark, "">"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""&"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, "";"", """")" & vbCrLf
s = s & "Remark = Replace(Remark, ""tdclassgrayTdNormalheight21nowrapnbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'------------------------------------------------------------------------------------------------------------------------
'Account
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Account"") Then" & vbCrLf
s = s & "Account = IEhtml2(a + 9)" & vbCrLf
s = s & "Account = Replace(Account, chr(34), """")" & vbCrLf
s = s & "Account = Replace(Account, "" "", """")" & vbCrLf
s = s & "Account = Replace(Account, ""<"", """")" & vbCrLf
s = s & "Account = Replace(Account, ""="", """")" & vbCrLf
s = s & "Account = Replace(Account, "">"", """")" & vbCrLf
s = s & "Account = Replace(Account, ""&"", """")" & vbCrLf
s = s & "Account = Replace(Account, "";"", """")" & vbCrLf
s = s & "Account = Replace(Account, ""/td"", """")" & vbCrLf
s = s & "Account = Replace(Account, ""tdclassgrayTdNormalheight21nowrapnbsp"", """")" & vbCrLf
s = s & "Account = Replace(Account, ""tdclasswhiteTdNormalheight21nbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'weight
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""addZero"") Then" & vbCrLf
s = s & "weight = IEhtml2(a + 2)" & vbCrLf
s = s & "weight = Replace(weight, chr(34), """")" & vbCrLf
s = s & "weight = Replace(weight, "" "", """")" & vbCrLf
s = s & "weight = Replace(weight, ""<"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""="", """")" & vbCrLf
s = s & "weight = Replace(weight, "">"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""&"", """")" & vbCrLf
s = s & "weight = Replace(weight, "";"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""/a"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""/td"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""("", """")" & vbCrLf
s = s & "weight = Replace(weight, "")"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""'"", """")" & vbCrLf
s = s & "weight = Replace(weight, ""tdclasswhiteTdNormalheight21aligncenternbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'LastComment
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Remarks"") Then" & vbCrLf
s = s & "LastComment = IEhtml2(a + 27)" & vbCrLf
s = s & "LastComment = Replace(LastComment, chr(34), """")" & vbCrLf
's = s & "LastComment = Replace(LastComment, "" "", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""<"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""="", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, "">"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""&"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, "";"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""/a"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""/td"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""("", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, "")"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, ""'"", """")" & vbCrLf
s = s & "LastComment = Replace(LastComment, "" td classgrayTdNormal height21nbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'Org + Location
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Orig"") Then" & vbCrLf
s = s & "ORG = IEhtml2(a + 17)" & vbCrLf
s = s & "ORG = Replace(ORG, chr(34), """")" & vbCrLf
s = s & "ORG = Replace(ORG, "" "", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""<"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""="", """")" & vbCrLf
s = s & "ORG = Replace(ORG, "">"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""&"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, "";"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""/a"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""/td"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""("", """")" & vbCrLf
s = s & "ORG = Replace(ORG, "")"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""'"", """")" & vbCrLf
s = s & "ORG = Replace(ORG, ""tdclasswhiteTdNormalheight21aligncenternbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'Dest
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Orig"") Then" & vbCrLf
s = s & "Dest = IEhtml2(a + 19)" & vbCrLf
s = s & "Dest = Replace(Dest, chr(34), """")" & vbCrLf
s = s & "Dest = Replace(Dest, "" "", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""<"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""="", """")" & vbCrLf
s = s & "Dest = Replace(Dest, "">"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""&"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, "";"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""/a"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""/td"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""("", """")" & vbCrLf
s = s & "Dest = Replace(Dest, "")"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""'"", """")" & vbCrLf
s = s & "Dest = Replace(Dest, ""tdclasswhiteTdNormalheight21aligncenternbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'-----------------------------------------------------------------------------------------------------------------------
'ProductCode
s = s & "For a = 1 To UBound(IEhtml2)" & vbCrLf
s = s & "If InStr(1, IEhtml2(a), ""Orig"") Then" & vbCrLf
s = s & "ProductCode = IEhtml2(a + 29)" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, chr(34), """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, "" "", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""<"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""="", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, "">"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""&"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, "";"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""/a"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""/td"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""("", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, "")"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""'"", """")" & vbCrLf
s = s & "ProductCode = Replace(ProductCode, ""tdclasswhiteTdNormalheight21aligncenternbsp"", """")" & vbCrLf
s = s & "End If" & vbCrL
s = s & vbCrLf
s = s & "Next" & vbCrL
s = s & vbCrLf
'Write results to Excel sheet
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""E"" & curRow) = Account" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""F"" & curRow) = Piece" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""G"" & curRow) = weight" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""H"" & curRow) = ORG" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""I"" & curRow) = Dest" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""J"" & curRow) = ProductCode" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""K"" & curRow) = Remark" & vbCrLf
s = s & "oXL.workbooks(""" & ThisWorkbook.Name & """).sheets(""Demo"").Range(""L"" & curRow) = LastComment" & vbCrLf
'Write VBScript file to disk
sFileName = ActiveWorkbook.Path & "\SwarmAgent_" & lngAgentNumber & ".vbs"
intFileNum = FreeFile
Open sFileName For Output As intFileNum
Print #intFileNum, s
Close intFileNum
DoEvents
'Run VBScript file
Set wshShell = CreateObject("Wscript.Shell")
wshShell.Run """" & sFileName & """"
DoEvents
Set wshShell = Nothing
End Sub