×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Search for more instances of a string
2

Search for more instances of a string

Search for more instances of a string

(OP)
Hello all,

I have information in Extra! that I need to extract. I created an Excel file to be able to manipulate the data which I will extract.

My problem is getting my macro to see more than one instance of the string I'm searching for, as it is probable there will be more than one on each page. With Sess0.Screen.Search, I cannot get it to find more than one.

I assume that I have to write the code so that the macro will search line by line to find the string? If so, can I add something at the beginning of my code to do a general search on the page for the string - if nothing is found, it changes pages - if the string is found, then it goes line by line?

I have included my code. Any suggestions?

CODE

Dim System As Object, Sess0 As Object, MyScreen As Object
Set System = CreateObject("EXTRA.System")
Set Sess0 = System.ActiveSession
Set MyScreen = Sess0.Screen
Dim i As Integer

Set tot_pages = MyScreen.area(1, 66, 1, 68)
ex_line = 2

Do       
        Set rad_find = Sess0.Screen.Search("String needed")
        If rad_find <> "String needed" Then
            Sess0.Screen.SendKeys ("<pf8>")
        Else
            rad_tax_year = Sess0.Screen.GetString(rad_find.Bottom, rad_find.Right - 17, 4)
            Sheets("Chiffres").Cells(ex_line, "a") = rad_tax_year
            If i = 12 Then
                Exit Do
            End If
        End If

        i = i + 1
    Loop Until i = tot_pages

Thanks for your help.
dvirgint

RE: Search for more instances of a string



hi,

1. Think in terms of Screen Paging control, without regard to the data on the page.  The logic must include

  a. how do you detect & respond to an invalid page entry message
  b. how do you detect and respond to a more pages for this entry message
  c. how do you detect and respond to an end of data message

Your screen control logic should not be included in your screen data processing logic.

2. Now you can develop your screen data processing logic, by looping thru rows on the screen OR by reading the entire screen into a variable and looping thru that data.  Either way, this logic is inside and separate from the screen paging logic.

 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string




btw, here's my basic control structure

CODE

    Const COMP = "LAST PAGE DISPLAYED"
    Const MORE = "MORE DETAIL LINES - PRESS ENTER TO CONTINUE"
    Const NONE = "TRAVELER NOT FOUND ON SERIAL SYSTEM"

    With oScrn
        For Each r In wsPartList.[PART_ID]
            sPn = r.Value
            .Area(3, 17, 3, 33).Value = sPn & "               "
            Do
                .MoveRelative 1, 1, 1
                .SendKeys ("<enter>")
                Do Until (.WaitForCursor(3, 17))
                    DoEvents
                Loop
'
'  screen data stuff occurs in here
'
NextPN:
            Loop Until GetField(sIn, "MSG") = COMP Or GetField(sIn, "MSG") = NONE
        Next
    End With
 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Thanks Skip, you always answer quickly and precisely...

I just have no idea what your code does here, it's way beyonf my low level of coding. If you have time, would you mind just telling me briefly what this does?  

RE: Search for more instances of a string



BTW, years ago I developed a generalized approach to screen scraping that writes ALL the data to an Excel Sheet/Table, and that has the following elements.

1. A Screen SPEC table that defines: ScrNam, FIELD, FR, TR, CL, LN, TYP;
  FR, TR - From Thru row where tabluar screen data resides
           If FR=TR then data only on one row
  CL     - Column
  LN     - Field Length
  TYP    - CHAR or NUM
  
2. Functions that use the screen SPEC, parse the screen data and write them to a table.

To set up a new screen, I load the required data in the Screen SPEC table, do some very slight midifications to a READScreenName procedure, including setting up the Screen Paging CONSTANTS, and a Sheet/Table that contains all FieldNames defined for that screen in the Screen SPEC table.  Usually takes about 15 - 20 minutes.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Any chance I could see that?

RE: Search for more instances of a string

(OP)
I've modified the above code to created a loop which is supposed to search line by line for the information I need above. My only problem now is that the macro searches the whole screen instead of the delimited area I've determined. How can I get the macro to search only the area I've told it to?

CODE

Set tot_pages = MyScreen.area(1, 66, 1, 68)
Do
        
        line_number = 5
        Set rad_find = Sess0.Screen.Search("Radiation par   ")
        If rad_find <> "Radiation par   " Then
            Sess0.Screen.SendKeys ("<pf8>")    'This changes the page
            Sess0.Screen.WaitHostQuiet (100)
        Else
            Do
                Set n_5 = MyScreen.area(line_number, 2, line_number, 80)   'This was supposed to tell the macro where to search
        
                Set rad_find = n_5.Search("Radiation par   ")
                If rad_find = "Radiation par   " Then
                    rad_tax_year = n_5.GetString(rad_find.Bottom, rad_find.Right - 17, 4)
                    Sheets("Chiffres").Cells(ex_line, "a") = rad_tax_year
                End If
                line_number = line_number + 1
                MsgBox line_number
            Loop Until line_number = 23
            ex_line = ex_line + 1
            Sess0.Screen.SendKeys ("<pf8>")
            Sess0.Screen.WaitHostQuiet (100)
        End If
        i = i + 1
    Loop Until i = tot_pages

If I use Sess0.Screen instead of what is highlighted, the whole screen is searched. Can someone please tell what code to use to accomplish this?

Thanks
Dvirgint

RE: Search for more instances of a string



How do you know that?

Have you STEPPED THRU your code to observe what is happening?  If you code in Excel VBA, you can also use the Watch Window to observe the values in variables and the state of objects in your code.

FAQ707-4594: How to use the Watch Window as a Power Programming Tool

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
I checked the code step-by-step as suggested, with the help of the Watch, and I can confirm that by using Sess0.Screen.Search the macro searches the whole screen. Each line that was checked had the same result even if the string I was searching for was located on the last line of the page.  Thanks for the article by the way Skip, it was very helpful.

If I try to substitute Sess0.Screen.Seach for n_5.Search or n_5.Screen.Search, I get the error Object doesn't support property or method.

Any suggestions?

RE: Search for more instances of a string


Quote (ExtraHELP):

Description

Returns an Area object with the text specified in the search.

Syntax

Set rc = object.Search(Text[,Row][,Col][,Page])

Element    Description
Set    The Set statement, required for assigning an object reference to a variable.
rc    The object variable for referencing the returned object.
object    The Screen object.

Row    The row where the search begins.
Col    The column where the search begins.

Comments

If the optional parameters are used, Screen is searched from the specified starting position. Otherwise, the entire Screen object is searched.
If Search finds the specified text, the coordinate properties (Left, Top, Right, Bottom) of the returned Area object are set to the starting and ending row and column positions of the text. The Value property of the Area object is set to the text located at those coordinates. If the Screen changes at those coordinates, the Value property of the Area changes.

If Search does not find the specified text, the Area object's Value property is set to an empty string, its Type property is set to xNONE, and its coordinate properties are set to -1.

Copyright 1996 - 1999, Attachmate Corporation. All rights reserved.
Not the AREA OBJECT.

This Method SEARCHES the ENTIRE SCREEN!

This Method RETURNS the AREA OBJECT.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Please correct me if I'm wrong, but this seems to be saying that this will only search the entire screen - is there another method which will allow a part of the screen to be searched only?

RE: Search for more instances of a string



WHY?  

Quote:


AREA Object

Description

Provides access to a defined area of the screen.

Properties   Methods
Application  Copy
Bottom       Cut
Left         Delete
Parent       Paste
Right        Select
Top    
Type    
Value    
Comments

You can create Area objects using methods and properties of the Screen object, such as Area and Selection.
Using the methods and properties of an Area object, you can read from or write to the presentation space, an area in PC memory that stores the screen data of a terminal session. A presentation space includes data from the status line (called Operator Information Area in 3270 sessions). Each presentation space position stores a screen character.
To access the presentation space, you must know the exact number of rows and columns that an emulated terminal provides. For example, if a session emulates a terminal supporting 24 rows by 80 columns, you can reference presentation space positions from row 1, column 1 to row 24, column 80. For VT sessions, you can also specify the page.

Copyright 1996 - 1999, Attachmate Corporation. All rights reserved.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
I need this because I may have more than one instance of the string on a page, and with the Screen object, I get only one of the results, not more.

RE: Search for more instances of a string



You FIND the first.

Then for the next search, supply a ROW that is AFTER that occurrence.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Isn't that what my code above is doing?

RE: Search for more instances of a string

CODE

Set rad_find = n_5.Search("Radiation par   ")
Where have you assigned "The row where the search begins"???

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)

CODE

Set n_5 = MyScreen.area(line_number, 2, line_number, 80)


I thought that this is where I was assigning where it begins?

RE: Search for more instances of a string


That assigns your n_5 object to that Area.

SO WHAT!!!

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
By your answer, I assume then that I haven't assigned where it begins, and so I have to say that I don't know how to do it.

RE: Search for more instances of a string


Your search, according the the Extra VB Help, is not at all related to the n_5 Area ovDect that you have assigned.

It is EVERYTHING about the arguments for the Search Method of the SCREEN OBJECT.  

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
OK, I'm making progress. The macro now finds all instances on the page, however it still goes from line 5 (which I set earler) to line 24. Your earlier post of 10:19 states:

Quote:


You FIND the first.

Then for the next search, supply a ROW that is AFTER that occurrence.

How can I get the macro to start the search on a particular line?

Here is the modified code I'm using.

CODE

Do
        
        line_number = 5
        Set rad_find = Sess0.Screen.Search("Radiation par")
        If rad_find <> "Radiation par" Then
            Sess0.Screen.SendKeys ("<pf8>")
            Sess0.Screen.WaitHostQuiet (100)
        Else
            Do       
                Set rad_find = Sess0.Screen.Search("Radiation par", line_number) 'I added line_number
                If rad_find = "Radiation par" Then
                    rad_tax_year = Sess0.Screen.GetString(rad_find.Bottom, rad_find.Right - 17, 4)
                    Sheets("Chiffres").Cells(ex_line, "a") = rad_tax_year
                End If
                line_number = line_number + 1
                'MsgBox line_number
            Loop Until line_number = 23
            ex_line = ex_line + 1
            Sess0.Screen.SendKeys ("<pf8>")
            Sess0.Screen.WaitHostQuiet (100)
        End If
        i = i + 1
    Loop Until i = tot_pages

Thanks for your patience, Skip!!

RE: Search for more instances of a string



16 Jan 12 9:38  

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string



That is referring to HELP on the Search Method.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

see if this helps you understand

CODE

Sub Main
    Dim Sys As Object, Sess As Object, MyScreen As Object, MyArea As Object
    Set Sys = CreateObject("EXTRA.System")
    Set Sess = Sys.ActiveSession
    Set MyScreen = Sess.Screen

    r = 1
    c = 1
    
    Do
    
        Set MyArea = MyScreen.Search("HELLO",r,c)
        MyScreen.MoveTo MyArea.Bottom, MyArea.left
        row = MyScreen.row
        col = MyScreen.col
        
        if row = 1 and col = 1 then
            msgbox "no more"
            exit sub
        else
            MyScreen.MoveTo MyArea.Bottom, MyArea.right + 1
            r = MyScreen.row
            c = myScreen.col
            
        end if
                
        
    Loop
    
End Sub

RE: Search for more instances of a string

(OP)
Does my code from earlier:

CODE

Set n_5 = MyScreen.area(line_number, 2, line_number, 80)

... set the coordinates for Sess0.Screen.Search?

 

RE: Search for more instances of a string

dvirgint,

if a search is successful, the row/col coordinates are changed, otherwise it goes to 1,1

include the row, col within the search string

RE: Search for more instances of a string

(OP)
Yes vzachin, that's very helpful, thank you. I see what you did, and understand - I'm very visual, so a small example helps a lot!!

RE: Search for more instances of a string


Quote (SkipVought/ExtraVB_Help):


Description

Returns an Area object with the text specified in the search.

Syntax

Set rc = object.Search(Text[,Row][,Col][,Page])

Element    Description
Set    The Set statement, required for assigning an object reference to a variable.
rc    The object variable for referencing the returned object.
object    The Screen object.
Row    The row where the search begins.
Col    The column where the search begins.

Comments

If the optional parameters are used, Screen is searched from the specified starting position. Otherwise, the entire Screen object is searched.
If Search finds the specified text, the coordinate properties (Left, Top, Right, Bottom) of the returned Area object are set to the starting and ending row and column positions of the text.
The Value property of the Area object is set to the text located at those coordinates. If the Screen changes at those coordinates, the Value property of the Area changes.

If Search does not find the specified text, the Area object's Value property is set to an empty string, its Type property is set to xNONE, and its coordinate properties are set to -1.


Copyright 1996 - 1999, Attachmate Corporation. All rights reserved.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
vzachin,

The code you gave for some reason doesn't seem to work. It goes to the first place on the page where the string is found, however after it doesn't go to the next. If there is only one instance on the page, the coordinates stay the same after it has been found, instead of returning to 1, 1.

And I copied your code into a new module to make sure I wasn't missing something.

Am I missing something?

RE: Search for more instances of a string

dvirgint,

without seeing your code, i'm not sure what it's doing. after finding the first string, where did you place the cursor? i'm thinking if your found string is at the end of the row, this may fail.

when you copied my code into a module, did you try it out with the search for "HELLO" and see if that works?

zach

RE: Search for more instances of a string

(OP)
Hi Zach,

Yes, in the new module, I used "HELLO" and that did not work. The cursor did not move. It stayed at the same coordinates (1,60). Any idea why?

And here is the updated code. I tried changing a few things to make it fit with what I needed it for...

CODE

    r = 1
    c = 1
    ex_line = 2
    
    Do
        
        Set rad_find = Sess0.Screen.Search("Radiation par")
        If rad_find <> "Radiation par" Then
            Sess0.Screen.SendKeys ("<pf8>")
            Sess0.Screen.WaitHostQuiet (100)
        Else
            
            Do
                
                Set rad_find = MyScreen.Search("Radiation par")
                MyScreen.MoveTo rad_find.Bottom, rad_find.Left
                Row = MyScreen.Row
                col = MyScreen.col
                MsgBox col
                If rad_find = "Radiation par" Then
                    rad_tax_year = Sess0.Screen.GetString(rad_find.Bottom, rad_find.Right - 17, 4)
                    Sheets("Chiffres").Cells(ex_line, "a") = rad_tax_year
                    ex_line = ex_line + 1
                    MyScreen.MoveTo rad_find.Bottom, rad_find.Right + 1
                    r = MyScreen.Row
                    c = MyScreen.col
                    MsgBox Row
                    MsgBox col
                    Row = Row + 1
                End If
                'r = Row + 1
                
            Loop Until Row = 23
            Sess0.Screen.SendKeys ("<pf8>")
            r = 1
            c = 1
            Sess0.Screen.WaitHostQuiet (100)
        End If
        i = i + 1
    Loop Until i = tot_pages

Thanks again for your help.

RE: Search for more instances of a string


Quote (AGAIN!!!):


Description

Returns an Area object with the text specified in the search.

Syntax

Set rc = object.Search(Text[,Row][,Col][,Page])

Element    Description
Set    The Set statement, required for assigning an object reference to a variable.
rc    The object variable for referencing the returned object.
object    The Screen object.
Row    The row where the search begins.
Col    The column where the search begins.

Comments

If the optional parameters are used, Screen is searched from the specified starting position. Otherwise, the entire Screen object is searched.

If Search finds the specified text, the coordinate properties (Left, Top, Right, Bottom) of the returned Area object are set to the starting and ending row and column positions of the text. The Value property of the Area object is set to the text located at those coordinates. If the Screen changes at those coordinates, the Value property of the Area changes.

If Search does not find the specified text, the Area object's Value property is set to an empty string, its Type property is set to xNONE, and its coordinate properties are set to -1.

Copyright 1996 - 1999, Attachmate Corporation. All rights reserved.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

dvirgint,

what Skip has been trying to point out is that your search string does not specify where to search from:

CODE

Set MyArea = MyScreen.Search("HELLO",r,c)
Set rad_find = MyScreen.Search("Radiation par")

also, there's no need for this:

CODE

Loop Until Row = 23

all you need to do is test for where your cursor is after a search. if the cursor returns to 1,1 (see skip's last post, last paragraph), then there is NO other occurrence of your search string.

Try testing from a blank screen, insert "HELLO" in various places on your screen and step through the code and you will see how the code works.

zach

RE: Search for more instances of a string



If your search is sucessful, it returns an AREA OBJECT, where the TOP property is the ROW and the LEFT property is the COLUMN.  

All this stuff is in your documentatin AND it is posted above!!!

Set your NEXT search with ROW+1 until rad_find Is Nothing.

READ & UNDERSTAND the documentation that you have at hand.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Hi Zach,

I did what you said and I can see the cursor move to each of the "HELLO"s, however when it gets to the last one, it stays there, it does not return to 1,1. I have copied your code as it is.

And I appreciate both of you helping... if I don't give the answer you are looking for, it's just that I don't understand what you're asking. I've read through the help file Skip sent several times, but I just don't understand all of what it's saying.

Thanks again.

RE: Search for more instances of a string



Actually

Set your NEXT search with ROW+1 until rad_find.Top = -1, which is NOT FOUND.

Here's how I did it...

CODE

            Do
                
                Set rad_find = oScrn.Search("Radiation par", rad_find.Top + 1, 1)
                
                If rad_find.Top = -1 Then
                    'GET OUT!
                    Exit Do
                Else
                    MsgBox "found @ " & rad_find.Top
                    'now do some stuff here
                End If
            Loop Until row = 23

 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Skip,

When I changed Set rad_find = oScrn.Search("Radiation par", rad_find.Top + 1, 1), it gave an error 424, Object required, so I dimmed an object for rad_find, but now it gives me an error 424, object variable or with block variable not set.

What am I doing wrong?

RE: Search for more instances of a string



I had MY screen object, oScrn.

Substitute yours.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
Sorry, I have already replaced your screen object for mine, and that's when it gives me the error.

RE: Search for more instances of a string




Post your code!

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)


CODE

    Dim System As Object, Sess0 As Object, MyScreen As Object
    Set System = CreateObject("EXTRA.System")
    Set Sess0 = System.ActiveSession
    Set MyScreen = Sess0.Screen
    Dim i As Integer
    'Dim n_5 As Object
    
    
    

    Set tot_pages = MyScreen.area(1, 66, 1, 68)
    r = 1
    c = 1
    ex_line = 2
    
    Do
        
        'line_number = 5
        Set rad_find = MyScreen.Search("Radiation par", rad_find.Top + 1, 1) 'I also tried Sess0.Screen.Search
        If rad_find <> "Radiation par" Then
            Sess0.Screen.SendKeys ("<pf8>")
            Sess0.Screen.WaitHostQuiet (100)
        Else
            
            Do
                
                Set rad_find = MyScreen.Search("Radiation par", rad_find.Top + 1, 1)
                If rad_find.Top = -1 Then
                    Exit Do
                Else
                    MyScreen.MoveTo rad_find.Bottom, rad_find.Left
                    Row = MyScreen.Row
                    col = MyScreen.col
                    'MsgBox col
                    If rad_find = "Radiation par" Then
                        rad_tax_year = Sess0.Screen.GetString(rad_find.Bottom, rad_find.Right - 17, 4)
                        Sheets("Chiffres").Cells(ex_line, "a") = rad_tax_year
                        ex_line = ex_line + 1
                        MyScreen.MoveTo rad_find.Bottom, rad_find.Right
                        r = MyScreen.Row
                        c = MyScreen.col
                        'MsgBox Row
                        'MsgBox col
                        'Row = Row + 1
                    End If
                End If
                    'r = Row + 1
            Loop Until Row = 23
            Sess0.Screen.SendKeys ("<pf8>")
            r = 1
            c = 1
            Sess0.Screen.WaitHostQuiet (100)
        End If
        i = i + 1
    Loop Until i = tot_pages

RE: Search for more instances of a string



THINK!

The code I posted was WITHIN the inner do loop, where you search.

When you INITIALLY set the search, it must have a SPECIFIC row & column or NO row or column...

CODE

    Do
        
        'line_number = 5
        Set rad_find = MyScreen.Search("Radiation par")

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string




BTW,

It is NOT necessary the cursor or use MoveTo to identiry sucessive values on the screen.

Those statements do not seem to add ANYTHING to your code, except clutter.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Search for more instances of a string

(OP)
IT WORKS!!

Thanks to both of you for your seemingly infinite patience.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close