Solved

Covert Visual Basic 6.0 to vbs(VBScript)

Posted on 2011-02-24
16
968 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:Dushan911
  • 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:Dushan911
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
 
LVL 17

Author Comment

by:Dushan911
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:Dushan911
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:Dushan911
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:Dushan911
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:Dushan911
ID: 34977076
Nope I don't have Visual Studio officially.  :(
0
 
LVL 17

Author Comment

by:Dushan911
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:Dushan911
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:Dushan911
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now