Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to write scripts.

Status
Not open for further replies.

rebie

Technical User
Jan 29, 2007
9
US
OK, I think I'd like to start using VBA to create my Attachmate scripts, but I'm a little lost in getting started.

I've read calculus' FAQ "How do I use VB(A) to manipulate attachmate (6.5+)? FAQ99-4069", but there's not enough context there to help me. Where do these statements go? Inside the Sub Main()? Outside? And if I want to port a script I wrote natively in Attachmate, which statements need to be reomoved from the Attachmate version?

Does someone have a simple VBA script they can share as a real example?

Thanks,
rebie
 
Post a simple EB script your looking to convert and I'll switch it for you and repost. It'll probably make more sense to you that way.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Here's some old code I used to use called "VBA Extra Base".

Code:
Sub SetSub()
'Extra Objects
    Set System = New ExtraSystem: If (System Is Nothing) Then Exit Sub
    Set Sessions = System.Sessions: If (Sessions Is Nothing) Then Exit Sub
    Set Sess0 = System.ActiveSession: If (Sess0 Is Nothing) Then Exit Sub
    Set MyScn = Sess0.Screen
'--------------------------------------------------------------------------------

For X = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
    DoEvents



NextX:
Next X
End Sub

calculus
 
Here's as simplest as I got right now...

Thanks much.

'--------------------------------------------------------------------------------
' This macro was created by the Macro Recorder.
' Session Document: "C:\Program Files\Attachmate\E!E2K\Sessions\KPNW\Bluegate.EDP"
' Date: Monday, January 29, 2007 08:36:13
'
'--------------------------------------------------------------------------------

' Global variable declarations
Global g_HostSettleTime%
Global g_szPassword$

Sub Main()
'--------------------------------------------------------------------------------
' Get the main system object
Dim Sessions As Object
Dim System As Object
Set System = CreateObject("EXTRA.System") ' Gets the system object
If (System is Nothing) Then
Msgbox "Could not create the EXTRA System object. Stopping macro playback."
STOP
End If
Set Sessions = System.Sessions

If (Sessions is Nothing) Then
Msgbox "Could not create the Sessions collection object. Stopping macro playback."
STOP
End If
'--------------------------------------------------------------------------------
' Set the default wait timeout value
g_HostSettleTime = 1000 ' milliseconds

OldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > OldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If

' Get the necessary Session Object
Dim Sess0 As Object
Set Sess0 = System.ActiveSession
If (Sess0 is Nothing) Then
Msgbox "Could not create the Session object. Stopping macro playback."
STOP
End If
If Not Sess0.Visible Then Sess0.Visible = TRUE
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)

' Declare the Excel Object
Dim appExcel As Object
Dim wbExcel As Object
Dim aSheet As Object
Set AppExcel = CreateObject("Excel.Application")
Set wbExcel = AppExcel.WorkBooks.Open("p:/datasheets/CM_Members.xls")
Set aSheet = wbExcel.Sheets("Sheet1")

' Decalre the variables
Dim MemIdNum As String
Dim MemDigit As Integer
Dim MemExist As String
Dim MemCounter As Integer
Dim MemFound As Integer
Dim MemName As String
MemCounter = 0
MemFound = 0

' Ask for starting member name number
MemDigit = InputBox("Please enter starting number for member name:")
' Check if the user canceled.
If MemDigit = 0 Then
GoTo TheEnd

Else
' Begin the work
Sess0.Screen.Sendkeys("<Tab>10msmc<Enter>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)

For x = 1 to aSheet.UsedRange.Rows.Count
MemIdNum = aSheet.Cells(x,1).text
Sess0.Screen.Sendkeys("<Pf5>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.PutString MemIdNum
Sess0.Screen.Sendkeys("<Enter>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)

' Check for existing member.
MemExist = Sess0.Screen.GetString(06,19,32)

If Trim(MemExist) <> "" Then
'MsgBox "This HRN is already in use!"
With aSheet
.Cells(x,3).value = MemExist
End With
MemFound = MemFound + 1
MemExist = ""
Sess0.Screen.Sendkeys("<Pf5>")

Else

Sess0.Screen.WaitForString "MBRS1030A -- No matches found.",21,02

Sess0.Screen.Sendkeys("<BackTab>a<Enter>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.Sendkeys("<Pf5>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.WaitForString "MBRS1992A -- This is a manual HRN.",21,02

Sess0.Screen.Sendkeys("nwets,nwmbr")
Sess0.Screen.PutString MemDigit
Sess0.Screen.Sendkeys("<Tab><Tab><Tab>06061935<Tab><Tab><Tab>m1000 Anywhere St<Tab><Tab><Tab>Anywhere<Tab>ca94022<Enter>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.WaitForString "MBRS0011I -- Update successfully completed.",21,02

MemName = Sess0.Screen.GetString(06,19,32)

With aSheet
.Cells(x,2).value = MemName
End With

MemCounter = MemCounter + 1
MemDigit = MemDigit + 1

End If
Next

Sess0.Screen.Sendkeys("<Home>")
Sess0.Screen.Sendkeys("msmn<Enter>")

MsgBox "Number of members created is " & MemCounter & " and Number of used HRN's is " & MemFound
With aSheet
.Cells(x + 1,1).value = "Number of members created is " & MemCounter & " and Number of used HRN's is " & MemFound
End With

End If
TheEnd:
appExcel.displayalerts = false
appExcel.Save
appExcel.displayalerts = true
appExcel.Quit

End Sub
 
Should get you started
Code:
Sub Main()

'Declare Variables
    Dim MemIdNum As String
    Dim MemDigit As Integer
    Dim MemExist As String
    Dim MemCounter As Integer
    Dim MemFound As Integer
    Dim MemName As String

'Declare Extra Objects
    Dim exSystem As Object
    Dim exSessions As Object
    Dim exScreen As Object
    
    Set exSystem = CreateObject("EXTRA.System")    ' Gets the system object
    Set exSessions = exSystem.Sessions
    Set exScreen = exSystem.ActiveSession.Screen

' Declare the Excel Object
    Dim wbExcel As Object
    Dim aSheet As Object
    
    Set wbExcel = Application.Workbooks.Open("p:/datasheets/CM_Members.xls")
    Set aSheet = Application.Sheets("Sheet1")
    
'Initialize Variables
    MemCounter = 0
    MemFound = 0

' Ask for starting member name number
    MemDigit = InputBox("Please enter starting number for member name:")
' Check if the user canceled.
    If MemDigit = Null Then
        Call QuickExit
    Else
        ' Begin the work
        exScreen.SendKeys ("<Tab>10msmc<Enter>")
        exScreen.WaitHostQuiet (1000)

        For x = 1 To aSheet.UsedRange.Rows.Count
            MemIdNum = aSheet.Cells(x, 1).Text
            exScreen.SendKeys ("<Pf5>")
            exScreen.WaitHostQuiet (1000)
            exScreen.PutString MemIdNum
            exScreen.SendKeys ("<Enter>")
            exScreen.WaitHostQuiet (1000)

' Check for existing member.
            MemExist = exScreen.GetString(6, 19, 32)
            
            If Trim(MemExist) <> "" Then
                'MsgBox "This HRN is already in use!"
                With aSheet
                    .Cells(x, 3).Value = MemExist
                End With
                MemFound = MemFound + 1
                MemExist = ""
                exScreen.SendKeys ("<Pf5>")
                
            Else
            
                exScreen.WaitForString "MBRS1030A -- No matches found.", 21, 2

                exScreen.SendKeys ("<BackTab>a<Enter>")
                exScreen.WaitHostQuiet (1000)
                exScreen.SendKeys ("<Pf5>")
                exScreen.WaitHostQuiet (1000)
                exScreen.WaitForString "MBRS1992A -- This is a manual HRN.", 21, 2

                exScreen.SendKeys ("nwets,nwmbr")
                exScreen.PutString MemDigit
                exScreen.SendKeys ("<Tab><Tab><Tab>06061935<Tab><Tab><Tab>m1000 Anywhere St<Tab><Tab><Tab>Anywhere<Tab>ca94022<Enter>")
                exScreen.WaitHostQuiet (1000)
                exScreen.WaitForString "MBRS0011I -- Update successfully completed.", 21, 2

                MemName = exScreen.GetString(6, 19, 32)

                With aSheet
                    .Cells(x, 2).Value = MemName
                End With

                MemCounter = MemCounter + 1
                MemDigit = MemDigit + 1
            
            End If
        Next

        exScreen.SendKeys ("<Home>")
        exScreen.SendKeys ("msmn<Enter>")
            
        MsgBox "Number of members created is " & MemCounter & " and Number of used HRN's is " & MemFound
        With aSheet
            .Cells(x + 1, 1).Value = "Number of members created is " & MemCounter & " and Number of used HRN's is " & MemFound
        End With
        
        End If
End Sub

Sub QuickExit()
    Application.DisplayAlerts = False
    Application.Save
    Application.DisplayAlerts = True
    Application.Quit
End Sub

I'd switch from
exScreen.SendKeys ("<Tab><Tab><Tab>06061935<Tab><Tab><Tab>m1000 Anywhere St<Tab><Tab><Tab>Anywhere<Tab>ca94022<Enter>")

to
exScreen.PutString "06061935", row, column
ect. as the tabs can get messed up if the user moves the cursor on you.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top