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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro runs at the first session 1

Status
Not open for further replies.

HenningSimonsen

Technical User
Sep 5, 2006
2
SE
Why are my macros running at the first session?

I work with mulitiple screens. When I play a macro at the second screen - it will run at the first.

Which changes have to be made, when I want the macro to run at the active screen? (I have tryed to rename Sess0 to Sess1)

Any help is welcome :)

This is an example of some of my code:

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 = 100 ' 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)

' This section of code contains the recorded events
Sess0.Screen.Sendkeys("<Pf9>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.Sendkeys("<Home><Tab><Tab><Enter>")
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
 
As I recall, the Active Session is the last opened session. So in your code,
Set Sess0 = System.ActiveSession
will actually just set the macro to work on the last opened session.

This may be Extra version dependent and I don't do a lot of multi session work, so take this with a grain of salt.

However, when I've worked with them in the past, I had to go through each session name until I found the one I wanted, then set the Sess0 object to it.

Alternately if your macro will be specific to a session, you could just refer to it in the code.

calculus
 
What I found that works for me.
Set Sess0=System.ActiveWindow
This will use the window that the macro fired from.

 
Thanks for your replies.
I have tryed to use the ActiveWindow-code, but it still runs at the first screen...
I even tried to reload the application - but it still dos not work with Set Sess0=System.ActiveWindow

I realy hope that we can solve the problem, I can't believe that it is ment to work that way.
 
This is basically what calculas is suggesting. You go through each session until you find the one with the name you want then set that to Sess0
Code:
For i = 1 to Sys.Sessions.Count
  If Not Sys.Sessions(i) Is Nothing Then
    If Sys.Sessions(i).Name = "NameYourLookingFor" Then
      Set Sess0 = Sys.Session(i)
      Exit For
    Else
      MsgBox Sys.Sessions(i).Name
    End If
  End If
Next
 
Had played with this for another post but user wanted to do this with a VB form not a dialog box

Code:
Declare Function FileDlgFunction(identifier$, action, suppvalue)
Dim Sys As Object
Dim Session As Object

Sub main
  Set Sys = CreateObject("Extra.System")
  Dim SessionNames() As String

  For SessionCnt = 0 to Sys.Sessions.Count
    Redim preserve SessionNames(SessionCnt)
    SessionNames(Sessions) = Sys.Sessions.Item(SessionCnt).Name
  Next
    
  Begin Dialog UserDialog 133, 66, 171, 65, "E! Basic Dialog Box", .FileDlgFunction

    Text  3, 3, 34, 9, "Open Sessions", .Text2
    ListBox  3, 14, 83, 39, SessionNames(), .ListBox1
    OKButton  105, 6, 54, 14
    CancelButton  105, 26, 54, 14
  End Dialog

  Dim mydialog as UserDialog
  On Error Resume Next
  Dialog mydialog

  Session.screen.putstring "Hello",1,1

End Sub

Function FileDlgFunction(identifier$, action, suppvalue)
  Dim whichsession as integer
  Select Case action
    Case 1                        'Dialog Box initialized

    Case 2                        'button or control value changed
      if identifier = "ListBox1" then whichSession = val(suppvalue)
      if identifier = "OK" then
        Set Session = Sys.Sessions.Item(SelectedSession)
      end if
    Case 3                        'text or combo box changed

    Case 4                        'control focus changed
     
    Case 5                        'idle
     
  End Select
End Function

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


 
It has been 6 months since you first asked this question. Did you find an answer or a work-around?

I came here with the same question, and couldn't find a satisfactory answer. Then I found a solution. Not particularly tidy, but I think it is going to work.

I set up a "Startup Macro File" that checks to see how long the Session.Name value is. If it is "short" then I create a new file name based on the one that started this session, but I append the date and time as part of the file name. Then I do a "SaveAs" and and "Open" on this new name. That makes all of the sessions distinct in the eyes of AttachmateExtra even though they are equivalent sessions for my purposes.

I will need to set up a process to clean up the many temporary files I will be creating, but that can wait a few days.

Here is my code to make the sessions unique:

Code:
Sub Main

' Exit Sub   ' unComment to keep macro from doing anything 
             ' when I need to keep the base Session around 
             ' long enough to modify it
[b][COLOR=red]
   < I omit the code to set up the objects >
   < re-insert the code you need here      >
[/color][/b]
   If len(Sess0.name) < 16  Then
        ' -------------------------------------
        ' ' Date$ returns mm-dd-yyyy
        ' ' Time$ returns hh:mm:ss
        suffix = mid(Date$,1,2) + mid(Date$,4,2) + "_" + mid(Time$,1,2) + mid(Time$,4,2) + mid(Time$,7,2)
        ' ' result is "mmdd_hhmmss"

        ' ' My session names currently have a two-part structure
        ' ' I drop the trailing part here
        brkChar = InStr(1,Sess0.Name,"_")
        if brkChar > 0 then
            prefix = mid(Sess0.Name,1,brkChar)
         else
            prefix = Sess0.Name + "_"
        end if ' brkChar>0    

        MyNewName = prefix + suffix + ".edp"

        MyPath = "C:\Program Files\Attachmate\E!E2K\Sessions\gbTemp\"
        ' ' I put all of the temp files in a unique sub-directory

        sess0.saveas (MyPath + MyNewName)
        Sys.Sessions.open (MyPath + MyNewName)

    End If  ' Len < 16


End Sub

 
I forgot to mention, Attachmate will not create a directory with this code, so I created the directory from Windows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top