Why does my VBS script skip a function?

Lenovo / Yoga 3 11"
October 12, 2017 at 23:46:03
Specs: Windows 10, 8 gb
I have a VBS script in an HTA file that looks like this. Though, when I call the drive() function (from outside this code) it for some reason runs driveOpen() before driveWrite(). How can I fix this problem?

Sub Wait(Time)
  Dim wmiQuery, objWMIService, objPing, objStatus
  wmiQuery = "Select * From Win32_PingStatus Where Address = '1.1.1.1' AND Timeout = " & Time
  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Set objPing = objWMIService.ExecQuery(wmiQuery)
  For Each objStatus In objPing
  Next
End Sub

Function drive()
  driveWrite()
  driveOpen()
End Function

Function driveWrite()
  document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>Launching Google Drive...</h2>"
End Function

Function driveOpen()
  Set IE = CreateObject("InternetExplorer.application")
  IE.Visible = False
  IE.Menubar = 0
  IE.Toolbar = 0
  IE.Statusbar = 0
  IE.Left = -7
  IE.Top = 0
  IE.Height = screen.AvailHeight + 7
  IE.Width = screen.AvailWidth + 14
  IE.Navigate "https://login.tyreso.se/wa/auth?authmech=Personal-%20och%20elevinloggning&location=https%3a%2f%2flogin.tyreso.se%2fwa%2fauth%2fsaml%2f%3fSAMLRequest%3dfVLLTgIxFN2b%252BA%252BT7ueFJpKGGYMaIgnqBEYX7krnMhT7srcD%252BveWQYIudHt67nnc3tH1h5LRFhwKowuSJxmJQHPTCN0W5LmexENyXZ6fjZApaem482s9h%252FcO0EdhUiPtHwrSOU0NQ4FUMwVIPaeL8cOMDpKMWme84UaSaHpXEL1pN1ZYZXTbrpkU2hq2ae1bw4CvltJqqdTybQ0kejnGGuxjTRE7mGr0TPsAZflVnA3jQVYPLujlkObZK4mqb6cboQ8N%252Fou1PJCQ3td1FVdPi7oX2IoG3GNgF6Q1ppWQcKP29hVDFNsAr5jEEG%252BMCM6HgLdGY6fALcBtBYfn%252Bawga%252B8t0jTd7XbJSSZlaeeXif90gCZBSBlHUvbLpX0%252F92Or%252F6dnR3dSnvRH6Q%252Bp8vvT9l2md5WRgn9GYynN7tYB86GId13oMTFOMf%252B3W57kPSKaeNVTaafRAhcrAQ2J0vLg%252Bvs6ws18AQ%253D%253D%26RelayState%3dhttps%253A%252F%252Faccounts.google.com%252FCheckCookie%253Fcontinue%253Dhttps%25253A%25252F%25252Fdrive.google.com%25252Fdrive%25252F%2526service%253Dwise%2526hl%253Den%2526checkedDomains%253Dyoutube%2526checkConnection%253Dyoutube%25253A607%25253A1%2526pstMsg%253D1"

  Do Until IE.ReadyState = 4
    Wait(200)
  Loop

  IE.Document.All.Item("username").Value = user
  IE.Document.All.Item("password").Value = pass
  IE.Document.All.Item("Submit1").Click

  Wait(2000)

  Do Until IE.ReadyState = 4
    Wait(200)
  Loop

  url = IE.LocationURL
  If url="https://login.tyreso.se/wa/auth" Then
    X=MsgBox("An error occured. Please stand by and try again.",0+16,"Login")
    document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>An error occured</h2>"
  Else
    IE.Visible = True
    document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>Google Drive has opened sucessfully</h2>"
  End If
End Function


See More: Why does my VBS script skip a function?

Reply ↓  Report •

#1
October 13, 2017 at 01:22:43
Not sure why DriveOpen is called before DriveWrite but one way around would be to call DriveOpen within DriveWrite. as below

Sub Wait(Time)
  Dim wmiQuery, objWMIService, objPing, objStatus
  wmiQuery = "Select * From Win32_PingStatus Where Address = '1.1.1.1' AND Timeout = " & Time
  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Set objPing = objWMIService.ExecQuery(wmiQuery)
  For Each objStatus In objPing
  Next
End Sub

Function drive()
  driveWrite()
End Function

Function driveWrite()
  document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>Launching Google Drive...</h2>"

driveOpen()

End Function

Function driveOpen()
  Set IE = CreateObject("InternetExplorer.application")
  IE.Visible = False
  IE.Menubar = 0
  IE.Toolbar = 0
  IE.Statusbar = 0
  IE.Left = -7
  IE.Top = 0
  IE.Height = screen.AvailHeight + 7
  IE.Width = screen.AvailWidth + 14
  IE.Navigate "https://login.tyreso.se/wa/auth?authmech=Personal-%20och%20elevinloggning&location=https%3a%2f%2flogin.tyreso.se%2fwa%2fauth%2fsaml%2f%3fSAMLRequest%3dfVLLTgIxFN2b%252BA%252BT7ueFJpKGGYMaIgnqBEYX7krnMhT7srcD%252BveWQYIudHt67nnc3tH1h5LRFhwKowuSJxmJQHPTCN0W5LmexENyXZ6fjZApaem482s9h%252FcO0EdhUiPtHwrSOU0NQ4FUMwVIPaeL8cOMDpKMWme84UaSaHpXEL1pN1ZYZXTbrpkU2hq2ae1bw4CvltJqqdTybQ0kejnGGuxjTRE7mGr0TPsAZflVnA3jQVYPLujlkObZK4mqb6cboQ8N%252Fou1PJCQ3td1FVdPi7oX2IoG3GNgF6Q1ppWQcKP29hVDFNsAr5jEEG%252BMCM6HgLdGY6fALcBtBYfn%252Bawga%252B8t0jTd7XbJSSZlaeeXif90gCZBSBlHUvbLpX0%252F92Or%252F6dnR3dSnvRH6Q%252Bp8vvT9l2md5WRgn9GYynN7tYB86GId13oMTFOMf%252B3W57kPSKaeNVTaafRAhcrAQ2J0vLg%252Bvs6ws18AQ%253D%253D%26RelayState%3dhttps%253A%252F%252Faccounts.google.com%252FCheckCookie%253Fcontinue%253Dhttps%25253A%25252F%25252Fdrive.google.com%25252Fdrive%25252F%2526service%253Dwise%2526hl%253Den%2526checkedDomains%253Dyoutube%2526checkConnection%253Dyoutube%25253A607%25253A1%2526pstMsg%253D1"

  Do Until IE.ReadyState = 4
    Wait(200)
  Loop

  IE.Document.All.Item("username").Value = user
  IE.Document.All.Item("password").Value = pass
  IE.Document.All.Item("Submit1").Click

  Wait(2000)

  Do Until IE.ReadyState = 4
    Wait(200)
  Loop

  url = IE.LocationURL
  If url="https://login.tyreso.se/wa/auth" Then
    X=MsgBox("An error occured. Please stand by and try again.",0+16,"Login")
    document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>An error occured</h2>"
  Else
    IE.Visible = True
    document.getElementById("appstatus").innerHTML = "<h2 style='margin-bottom: 0px;'>Google Drive has opened sucessfully</h2>"
  End If
End Function


message edited by AlwaysWillingToLearn


Reply ↓  Report •

#2
October 13, 2017 at 05:56:01
I've tried that, but it still doesn't work... Thanks for the idea though!

Reply ↓  Report •
Related Solutions


Ask Question