Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 992
  • Last Modified:

Covert Visual Basic 6.0 to vbs(VBScript)

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
Dushan De Silva
Asked:
Dushan De Silva
  • 9
  • 7
1 Solution
 
RobSampsonCommented:
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
 
Dushan De SilvaAuthor Commented:
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
 
RobSampsonCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Dushan De SilvaAuthor Commented:
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
 
Dushan De SilvaAuthor Commented:
Do I need to register these dll to windows registry or something else to call from VBScript ?
0
 
RobSampsonCommented:
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
 
RobSampsonCommented:
You could leave the code in an Excel macro, and run the macro when you need to.

Rob.
0
 
Dushan De SilvaAuthor Commented:
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
 
RobSampsonCommented:
Doesn't it work if you compile your VB6.0 code to an EXE?
0
 
Dushan De SilvaAuthor Commented:
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
 
Dushan De SilvaAuthor Commented:
Nope I don't have Visual Studio officially.  :(
0
 
Dushan De SilvaAuthor Commented:
We did most of the stuff with PHP using COMs.
0
 
RobSampsonCommented:
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
 
Dushan De SilvaAuthor Commented:
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
 
RobSampsonCommented:
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
 
Dushan De SilvaAuthor Commented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now