将VBA脚本转换为在Linux机器上作为VBS文件窗口或某种类型的脚本运行

时间:2013-08-28 23:50:30

标签: vba vbscript

我编写了以下代码来检查Web服务器/应用程序是否正在运行,然后将结果记录到文件中。我使用Excel VBA和FSO Scripting Runtime完成了这项工作。我想将其转换为vbs脚本文件,以便在Windows服务器上运行,或者转换为可以在linux服务器上运行的不同内容。代码的任何部分都不依赖于Excel,我只是使用VBA代码库。我试图将副本复制到vbs文本文件并执行它,但我一直收到错误。我需要做些什么才能让它在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

1 个答案:

答案 0 :(得分:1)

在肯的骄傲中,我能够通过谷歌的帮助解决我的错误。谢谢肯。

这是我的重写代码,可用作vbs脚本

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()