Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wrote the below code to check if a webserver/application was running and then log the result to a file. I did this using Excel VBA with FSO Scripting Runtime. I would like to convert this to a vbs script file to run on a windows server or to something different I can run on a linux server. No part of the code is excel dependant, I was just using the VBA code base. I tried to copy the copy to a vbs text file and execute it but I kept getting errors. What do I have to do to get this to run outside of excel VBA?

Option Explicit

Public Sub IE_Check()
Dim i As Long
Dim g As Long
Dim x As Long
Dim IE As Object
Dim objElement As Object
Dim objElement2 As Object
Dim Colobj1 As Object
Dim Colobj2 As Object
Dim Colobj3 As Object
Dim FindText1 As String
Dim FindText2 As String
Dim FindText3 As String
Dim TTime As String


Dim fso As New FileSystemObject

' Declare a TextStream.
Dim stream As TextStream

' Create a TextStream.
Set stream = fso.OpenTextFile("C:\log.txt", 2, True)

TTime = Time


Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

IE.Navigate "http://yahoo.com"



Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop





  FindText1 = InStr(1, IE.Document.body.innerhtml, "Internet Explorer cannot display the webpage")

       If FindText1 > 0 Then



stream.WriteLine ("Webserver_down;" & TTime)

       GoTo webserver_down
         End If


webserver_up:

Set Colobj2 = IE.Document.getElementsByTagName("input")

 g = 0
While g < Colobj2.Length

    If Colobj2(g).Name = "Uid" Then

        Colobj2(g).Value = "test"

    Else
        If Colobj2(g).Type = "submit" Then

            Set objElement = Colobj2(g)

        End If
    End If
 g = g + 1
 Wend


    objElement.Click

  Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
 Loop

FindText3 = InStr(1, IE.Document.body.innerhtml, "mstrWebAdmin")

If FindText3 > 0 Then

    stream.WriteLine ("IServer_down;" & TTime)
    Else

stream.WriteLine ("All_GOOD;" & TTime)
End If


webserver_down:

IE.Quit

' Clean up
Set IE = Nothing
Set objElement = Nothing
Set Colobj1 = Nothing
Set objElement2 = Nothing
Set Colobj2 = Nothing
Set Colobj3 = Nothing


stream.Close


End Sub
share|improve this question
    
Every single thing that VBA does is Windows-based. There is no FileSystemObject, TextStream, or InternetExplorer.Application on Linux; those are all Windows-specific ActiveX objects, and require Windows COM support to function. No Windows, no Windows COM support. VBA is "Visual Basic for Applications", which is automation and code support for Microsoft Office products. VBS is "Visual Basic Scripting", and VB is a Microsoft product. If you want similar functionality on Linux, you're going to have to take a totally different approach. –  Ken White Aug 29 '13 at 0:13
    
Noted on Linux, is it possible to get this to run as a vbscript on windows outside of excel? –  KP_EDIT Aug 29 '13 at 0:30
    
I was addressing the "on a Linux box" in your question title. You said you tried and "kept getting errors". Why don't you post what you tried, include the errors you're getting, and take it from there? SO doesn't typically do well with code-rewriting questions. "Here's some code. Please rewrite it for me in this other language" type questions usually get closed unless the poster includes some effort they've made to do the rewrite themselves and explain the problem they're having with that effort. –  Ken White Aug 29 '13 at 0:51
    
Noted, I will rework the question. Thanks. –  KP_EDIT Aug 29 '13 at 3:13

1 Answer 1

At Ken's proding, I was able to plod through my error with google's help. Thanks Ken.

Here is my reworked code that works as a vbs script

Option Explicit

Public Sub IE_Check()
Dim i
Dim g
Dim x
Dim IE
Dim objElement
Dim objElement2
Dim Colobj1
Dim Colobj2
Dim Colobj3
Dim FindText1
Dim FindText2
Dim FindText3
Dim TTime

Dim dteWait
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
' Declare a TextStream.
Dim stream
' Create a TextStream.
Set stream = fso.OpenTextFile("C:\log.txt", 8, True)

TTime = Time


Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

IE.Navigate "http://yahoo.com"



While IE.ReadyState < 4
   FindText1 = 0
Wend




FindText1 = InStr(1, IE.Document.body.innerhtml, "Internet Explorer cannot display the webpage")

    If FindText1 > 0 Then



stream.WriteLine ("Webserver_down;" & TTime)
 stream.Close
  IE.Quit
 Wscript.Quit
         End If



 Set Colobj2 = IE.Document.getElementsByTagName("input")

g = 0
 While g < Colobj2.Length

     If Colobj2(g).Name = "Uid" Then

        Colobj2(g).Value = "test"

    Else
        If Colobj2(g).Type = "submit" Then

            Set objElement = Colobj2(g)

        End If
     End If
 g = g + 1
 Wend


    objElement.Click

While IE.ReadyState < 4
   FindText1 = 0
Wend


FindText3 = InStr(1, IE.Document.body.innerhtml, "mstrWebAdmin")

If FindText3 > 0 Then

    stream.WriteLine ("IServer_down;" & TTime)
    Else

stream.WriteLine ("All_GOOD;" & TTime)
End If



IE.Quit



stream.Close

End sub
call IE_Check()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.