Solved

Covert Visual Basic 6.0 to vbs(VBScript)

Posted on 2011-02-24
16
977 Views
Last Modified: 2013-12-26
Hi Experts,
I want to convert attached code to VBScript, which could execute with cscript kind of a command from the command line.
I'm not sure how to import/add reference of dll files like in Visual Basic.

BR Dushan.
'The following lines must be included in your project in order to
'use every data object example

'Make sure that the following line appears at the TOP of the module
'you are working on!
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)



Function DoRequest(obj As Object, Optional lTimeOutMilliseconds As Long = 20000, Optional bRequestNext As Boolean = False) As Boolean
    DoRequest = False

    
    Const SLEEP_VALUE As Long = 200
    Dim bTimeoutCheck As Boolean
    Dim lTimeCount As Long
    Dim state As Integer
    Dim lErrorNumber As Long
    Dim strErrorDescription As String

    ' Handle any automation exception errors generated when calling Request method
    On Error Resume Next

    If (bRequestNext) Then
        obj.RequestNext
    Else
        obj.Request
    End If

    lErrorNumber = Err.Number

    strErrorDescription = Err.Description

    On Error GoTo 0

    If lErrorNumber <> 0 Then
        MsgBox "An automation exception error occurred with the request: " & strErrorDescription
        Exit Function
    End If

    bTimeoutCheck = (lTimeOutMilliseconds > 0)
    lTimeCount = 0
    state = obj.state

    Do Until state <> DfsCmd.DataPending
        ' Check if timeout occurred
        If bTimeoutCheck And lTimeCount >= lTimeOutMilliseconds Then Exit Do

        DoEvents ' Allow system events to be processed while looping
        Sleep SLEEP_VALUE
        state = obj.state
        ' Increment time counter if checking for timeouts
        If bTimeoutCheck Then lTimeCount = lTimeCount + SLEEP_VALUE
    Loop

    Select Case state
        Case DfsCmd.Error
            MsgBox "An error occurred with the request: " & obj.Error
        Case DfsCmd.DataReady
            DoRequest = True

        Case DfsCmd.DataIncomplete
            MsgBox "The data was incomplete at the time of the request. Please try again later!"
        Case DfsCmd.DataMorePending
        '    DoRequest obj, lTimeOutMilliseconds, True
         MsgBox "Request Data More Pending!"
        Case Else
            ' Determine if timeout occurred
            If (lTimeOutMilliseconds > 0) And (lTimeCount >= lTimeOutMilliseconds) Then
                MsgBox "Request timeout occurred!"
            Else

                MsgBox "Unexpected Error in Request"
            End If
    End Select
End Function


Sub Main()
    Dim datamgr As New DfsCmd.DataManager
    Dim misc As DfsIos.DataIosMisc
    Set misc = datamgr.CreateOb("iosmisc")
    
    With misc
    .Login
    .IOSName(0) = "testName"
    .AuthenticationType(0) = testAuth
    .User(0) = "usr"
    .Pass(0) = "pwd"
End With

    
    If Not DoRequest(misc) Then End
    
    Dim strError As String
    Dim i As Integer
    misc.GetIosStatus
    misc.GetCode
    For i = 0 To misc.RowCount("IosName") - 1
        If misc.ErrorDescription(i) <> "" Then
            strError = strError & "Failed to login to " & misc.IOSName(i) & ": " & misc.ErrorDescription(i) & vbCrLf
            MsgBox strError
        Else
            MsgBox "LoginOK," & "AccCode:" & misc.AccCode(i) '& "LoggedIn:"& misc.LoggedIn(i)
        End If
    Next i
    
   ' If strError <> "" Then MsgBox strError Else MsgBox "LoginOK"
End Sub

Open in new window

0
Comment
Question by:Dushan De Silva
  • 9
  • 7
16 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34976947
Hi, there's no guarantee this would even work in VBS, depending on how the objects behave, but try this.

Regards,

Rob.
Call Main


Function DoRequest(obj, lTimeOutMilliseconds, bRequestNext)
    DoRequest = False
    
    Const SLEEP_VALUE = 200
    Dim bTimeoutCheck
    Dim lTimeCount
    Dim state
    Dim lErrorNumber
    Dim strErrorDescription

    ' Handle any automation exception errors generated when calling Request method
    On Error Resume Next

    If (bRequestNext) Then
        obj.RequestNext
    Else
        obj.Request
    End If

    lErrorNumber = Err.Number

    strErrorDescription = Err.Description

    On Error GoTo 0

    If lErrorNumber <> 0 Then
        MsgBox "An automation exception error occurred with the request: " & strErrorDescription
        Exit Function
    End If

    bTimeoutCheck = (lTimeOutMilliseconds > 0)
    lTimeCount = 0
    state = obj.state

    Do Until state <> DfsCmd.DataPending
        ' Check if timeout occurred
        If bTimeoutCheck And lTimeCount >= lTimeOutMilliseconds Then Exit Do

        DoEvents ' Allow system events to be processed while looping
        WScript.Sleep SLEEP_VALUE
        state = obj.state
        ' Increment time counter if checking for timeouts
        If bTimeoutCheck Then lTimeCount = lTimeCount + SLEEP_VALUE
    Loop

    Select Case state
        Case DfsCmd.Error
            MsgBox "An error occurred with the request: " & obj.Error
        Case DfsCmd.DataReady
            DoRequest = True

        Case DfsCmd.DataIncomplete
            MsgBox "The data was incomplete at the time of the request. Please try again later!"
        Case DfsCmd.DataMorePending
        '    DoRequest obj, lTimeOutMilliseconds, True
         MsgBox "Request Data More Pending!"
        Case Else
            ' Determine if timeout occurred
            If (lTimeOutMilliseconds > 0) And (lTimeCount >= lTimeOutMilliseconds) Then
                MsgBox "Request timeout occurred!"
            Else

                MsgBox "Unexpected Error in Request"
            End If
    End Select
End Function


Sub Main()
    Set datamgr = CreatEobject("DfsCmd.DataManager")
    'Set misc As DfsIos.DataIosMisc
    Set misc = datamgr.CreateOb("iosmisc")
    
    With misc
    .Login
    .IOSName(0) = "testName"
    .AuthenticationType(0) = testAuth
    .User(0) = "usr"
    .Pass(0) = "pwd"
End With

    
    If Not DoRequest(misc) Then WScript.Quit
    
    Dim strError
    Dim i
    misc.GetIosStatus
    misc.GetCode
    For i = 0 To misc.RowCount("IosName") - 1
        If misc.ErrorDescription(i) <> "" Then
            strError = strError & "Failed to login to " & misc.IOSName(i) & ": " & misc.ErrorDescription(i) & vbCrLf
            MsgBox strError
        Else
            MsgBox "LoginOK," & "AccCode:" & misc.AccCode(i) '& "LoggedIn:"& misc.LoggedIn(i)
        End If
    Next
    
   ' If strError <> "" Then MsgBox strError Else MsgBox "LoginOK"
End Sub

Open in new window

0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34976962
Thanks!
As you assumed it gives following error. :)
-----------------------------------------------------------------------------------------------------------------
C:\dushan\vb\vbs\test2.vbs(73, 5) Microsoft VBScript runtime error: ActiveX comp
onent can't create object: 'DfsCmd.DataManager'
-----------------------------------------------------------------------------------------------------------------
I think I should do something with DLL files which I used in VB files as references here.  But I don't know how to do it with vbs. Please help me.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34976981
It depends on whether the object can even be created by VBScript.  Object work differently.

If you run ActiveX Helper from here:
http://www.nirsoft.net

and then search for DfsCmd.DataManager does it refer to the same DLL?  If not, search for the name of the DLL and see if you can find the appropriate object name for it.

Regards,

Rob.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977027
Hi Rob,
Thanks!
Required DLLs are on my hand and it's working correctly with Excel Macro Editor(with adding DLL files from reference in the VB Editor), by the time I'm reading above details on given URLs

Best Regards,
Dushan
working.JPG
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977033
Do I need to register these dll to windows registry or something else to call from VBScript ?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34977044
Correct, it should work from VBA as is, as it's more like VB6.  VBScript doesn't have "references" though. The only way to create an object is to use the CreateObject call, with the correct object name.  ActiveXHelper might help you identify the name of the control.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34977045
You could leave the code in an Excel macro, and run the macro when you need to.

Rob.
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977061
But I want to run this on windows schedule task (as background process). To run from the MS Excel , it should run from the Windows session (Remote Desktop or Terminal Service). But I want to run without a session.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34977070
Doesn't it work if you compile your VB6.0 code to an EXE?
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977071
C:\dushan\vb\vbs>"C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" /e test1.xlsm

is doing the task, but we should logging to the system with a remote session.  I want to run without a session.

BR Dushan.
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977076
Nope I don't have Visual Studio officially.  :(
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977086
We did most of the stuff with PHP using COMs.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34977098
OK, well see what you can find with ActiveXHelper for the VBScript object, but it might not be possible to create it.

For the scheduling of the macro, is it possible for you to leave the console logged in (but locked), and schedule the task to run interactively, using
AT 17:00 /INTERACTIVE "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" /e test1.xlsm

Regards,

Rob.
0
 
LVL 17

Author Comment

by:Dushan De Silva
ID: 34977106
Thanks a lot for your quick responses.
Nope the things which we already automated are running without a logging session. So this function also will need to implement in same manner.
Is it not possible to access the DLL files which are on my hand from VBScript?

BR Dushan.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34977145
You kind of can, but it depends on whether the DLL has exposed its methods to the scripting ability.  VB6 can use it via COM functionality.  Another thing you can try is to download VBSEdit from www.vbsedit.com

and then, as you type
Set datamgr = CreateObject("DfsCmd.DataManager")

when you get to the quotes, it should give you a drop down list.  So keep typing Dfs and you'll jump to those objects.  If DfsCmd.DataqManager is not listed, then it doesn't support the scripting interface.

On the other hand, does the Excel macro work when no one is logged in?  I've never tried it....

Regards,

Rob.
0
 
LVL 17

Author Closing Comment

by:Dushan De Silva
ID: 34994359
Thanks a lot! I'm still working with these tools, I may post new question if I need further assistant!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question