While Loop copy and Paste issue

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Donf
Newbie
Posts: 7
Joined: Tue Jul 11, 2017 11:39 am

While Loop copy and Paste issue

Post by Donf » Wed Nov 08, 2017 6:18 pm

Hello,

I've created a loop that opens a webpage where it opens items on dynamic content in new windows. I am trying to copy the text on the website and paste onto Notepad. All works well as far as opening the dynamic pages; however, it only copies and paste on the first iteration of the loop. I does not do it when x>1....

//Start Loop
While>x<valueArray_1

Let>x=x+1

//Start Loop
While>x<valueArray_1

Let>x=x+1

//Extract the document number

MidStr>URLArray_%x%,46,11,strSub

let>ID_Var=https://www.idx2.net/Tdx2/Docview.aspx? ... id=%strSub%

VBRun>CreateIE

VBEval>IE.Hwnd,ie_handle
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0

VBRun>Navigate,%ID_Var%

VBRun>WaitBusy

Press CTRL
Send>a
Release CTRL

Wait>1
Press CTRL
send>c
Release CTRL

//focus on Notepad
Let>WIN_REGEX=1
SetFocus>.+Notepad$
Let>WIN_REGEX=0
wait>1

//wait until clipboard ready
Wait>1
WaitClipBoard
Wait>1

//Paste onto Notepad
Press CTRL
Send>v
Release CTRL
wait>1

Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0
Press ALT
Press F4
wait>1

wait>1

Let>WIN_USEHANDLE=1
SetFocus>Org_IE_Handle
Let>WIN_USEHANDLE=0

EndWhile


Any insights or suggestions would be appreiated

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: While Loop copy and Paste issue

Post by Marcus Tettmar » Thu Nov 09, 2017 10:24 am

Hi,

Did you mean to duplicate the first 3 lines?

I don't see x being initialised before the loop - maybe you didn't paste all the code? But if missing that could be an issue.

This line will always return nothing:

MidStr>URLArray_%x%,46,11,strSub

Needs to change to:

Let>tmp=URLArray_%x%
MidStr>tmp,46,11,strSub

Any reason why you're using VBScript instead of the native IE functions?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Donf
Newbie
Posts: 7
Joined: Tue Jul 11, 2017 11:39 am

Re: While Loop copy and Paste issue

Post by Donf » Thu Nov 09, 2017 1:16 pm

Marcus Tettmar wrote:Hi,

Did you mean to duplicate the first 3 lines?

I don't see x being initialised before the loop - maybe you didn't paste all the code? But if missing that could be an issue.

This line will always return nothing:

MidStr>URLArray_%x%,46,11,strSub

Needs to change to:

Let>tmp=URLArray_%x%
MidStr>tmp,46,11,strSub

Any reason why you're using VBScript instead of the native IE functions?

Hello Marcus (I'm in the UK as well, Peak District), here's the full code minus the ID and password for the site:

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1
VBSTART
Dim IE

Sub CreateIE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible=1
End Sub

Sub Navigate(URL)
IE.Navigate URL
End Sub

Sub WaitBusy
do while IE.Busy
loop
End Sub

Sub KillIE
IE.Quit
Set IE = nothing
End Sub

'This function fills a form field
'fieldname is the name of the field to fill
'fieldvalue is the value to set it to
'set submit to 1 to submit the form
Sub WebFormFill(fieldname,fieldvalue,submit)
Dim FormNr
Dim ItemNr
Dim TheForm

if IE.Document.All.Tags("FORM").Length = 0 then
MsgBox("No form found in page")
else
for FormNr = 0 to IE.Document.Forms.Length - 1
Set TheForm = IE.Document.Forms(FormNr)
for ItemNr = 0 to TheForm.Elements.Length - 1
if TheForm.Elements(ItemNr).Name = fieldname then
TheForm.Elements(ItemNr).Value = fieldvalue
If submit=1 then
TheForm.submit
end if
exit for
end if
next
next
end if
End Sub


'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
dim var_t
set var_t = IE.document.getElementsbyTagname(Tagname)
if all=1 then
ExtractTag = var_t.Item(Num).outerHTML
else
ExtractTag = var_t.Item(Num).innerText
end if
End Function

VBEND


Position>NT.6,OS_VER,1,IsVistaOrAbove
If>IsVistaOrAbove=1
Ask>To run this script in Vista you may need to disable "Protected Mode" in Internet Explorer under Tools/Options/Security. Do you wish to proceed?,bProceed
If>bProceed=NO
Exit
Endif
Endif

//Open Notepad
Let>RP_WAIT=2
Run>notepad.exe
WaitWindowOpen>Untitled - Notepad
WaitReady>0

//Ask for start and end dates

Input>StartResult,Start Date in mm/dd/yyyy format:,9/16/2017
Input>EndResult,Start Date in mm/dd/yyyy format:,9/16/2017

VBRun>CreateIE

//Focus the new IE window
VBEval>IE.Hwnd,ie_handle
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0

//Store Original Handle
let>Org_IE_Handle=ie_handle

//Ask for start date


VBRun>Navigate,www.idx2.net/Tracker/Main.aspx
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,txtUserName,Useridnotgiven,0
VBRun>WaitBusy

VBRun>WebFormFill,txtPassword,passwordnotgiven,0
VBRun>WaitBusy

//Press login button in form
IETagEventByAttrib>https://www.idx2.net/Tdx2/Login.aspx?Re ... ogIn,click,

VBRun>WaitBusy
Wait>1

VBRun>WaitBusy
Wait>1


//Send Parameters to form
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxSender,gsaoms,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxDocType,850,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxFromDate,StartResult,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxToDate,EndResult,0
VBRun>WaitBusy
Wait>1

//Click Submit button
IETagEventByAttrib>https://www.idx2.net/Tdx2/Main.aspx,INP ... ton1,click,
VBRun>WaitBusy
Wait>3


//GET Number of documents available
IEGetTagsByAttrib>https://www.idx2.net/Tdx2/Main.aspx,SPA ... valueArray
//MessageModal>valueArray_1
VBRun>WaitBusy
Wait>3

// Get the DOC ID numbers
IEGetTagsByAttrib>{"https://www.idx2.net/Tdx2/Main.aspx"},A ... O,URLArray
VBRun>WaitBusy
Wait>1
Let>MSG_WIDTH=800


//Start Main Loop to capture documents
Let>x=0


//Start Loop
While>x<valueArray_1

Let>x=x+1

//Start Loop
While>x<valueArray_1

Let>x=x+1

//Extract the document number

MidStr>URLArray_%x%,46,11,strSub

let>ID_Var=https://www.idx2.net/Tdx2/Docview.aspx? ... id=%strSub%


VBRun>CreateIE

//SetFocus>
VBEval>IE.Hwnd,ie_handle
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0


VBRun>Navigate,%ID_Var%

VBRun>WaitBusy

//Select all text
Press CTRL
Send>a
Release CTRL

//Copy all text
Wait>1
Press CTRL
send>c
Release CTRL


//focus on Notepad

//find first matching window that starts with 1 or more printable characters and ends with "Notepad" (e.g. Untitled - Notepad)
Let>WIN_REGEX=1
SetFocus>.+Notepad$
Let>WIN_REGEX=0
wait>1

//wait until clipboard ready
Wait>1
WaitClipBoard
Wait>1

//Paste onto Notepad
Press CTRL
Send>v
Release CTRL
wait>1

//close IE Window
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0
Press ALT
Press F4
wait>1

//Focus on parent IE Window
Let>WIN_USEHANDLE=1
SetFocus>Org_IE_Handle
Let>WIN_USEHANDLE=0

//Reset loop IE variable
let>ie_handle=""


EndWhile

MessageModal>Finished!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: While Loop copy and Paste issue

Post by Marcus Tettmar » Thu Nov 09, 2017 1:30 pm

Please re-read my reply.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: While Loop copy and Paste issue

Post by Marcus Tettmar » Thu Nov 09, 2017 1:31 pm

To reiterate.

1 You have repeated the first three lines of the loop. You have the While> part twice.

2. This line will always return nothing:

MidStr>URLArray_%x%,46,11,strSub

Needs to change to:

Let>tmp=URLArray_%x%
MidStr>tmp,46,11,strSub
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Donf
Newbie
Posts: 7
Joined: Tue Jul 11, 2017 11:39 am

Re: While Loop copy and Paste issue

Post by Donf » Thu Nov 09, 2017 2:23 pm

Hello again,

Got rid of the double loop (missed that twice sorry), as for MidStr I was using what the help file defines:
MidStr>string,start,length,result

Returns a substring of specified length from a given position in a string. result is a variable in which to store the returned string. Any parameter can be a variable containing the appropriate values.
Abbreviation : Mid
See also: Position, ConCat, Length
Example
In the following example, the variable somevalue becomes equal to 'Happy' :
MidStr>Happy Birthday,1,5,somevalue
Message>somevalue



Both methods worked on the midstr, as I tried yours as well.
As for why I use VB it is because the built in IE commands had trouble changing focus so I figured it would be better using the IE handles. Thus far it always focus on the intended window throughout the loop.

However, the select all and the copy and paste onto notepad still only works on the first pass. Here is the code with your suggestions taken on board:


Let>IGNORESPACES=1

VBSTART
Dim IE

Sub CreateIE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible=1
End Sub

Sub Navigate(URL)
IE.Navigate URL
End Sub

Sub WaitBusy
do while IE.Busy
loop
End Sub

Sub KillIE
IE.Quit
Set IE = nothing
End Sub

'This function fills a form field
'fieldname is the name of the field to fill
'fieldvalue is the value to set it to
'set submit to 1 to submit the form
Sub WebFormFill(fieldname,fieldvalue,submit)
Dim FormNr
Dim ItemNr
Dim TheForm

if IE.Document.All.Tags("FORM").Length = 0 then
MsgBox("No form found in page")
else
for FormNr = 0 to IE.Document.Forms.Length - 1
Set TheForm = IE.Document.Forms(FormNr)
for ItemNr = 0 to TheForm.Elements.Length - 1
if TheForm.Elements(ItemNr).Name = fieldname then
TheForm.Elements(ItemNr).Value = fieldvalue
If submit=1 then
TheForm.submit
end if
exit for
end if
next
next
end if
End Sub


'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
dim var_t
set var_t = IE.document.getElementsbyTagname(Tagname)
if all=1 then
ExtractTag = var_t.Item(Num).outerHTML
else
ExtractTag = var_t.Item(Num).innerText
end if
End Function

VBEND


Position>NT.6,OS_VER,1,IsVistaOrAbove
If>IsVistaOrAbove=1
Ask>To run this script in Vista you may need to disable "Protected Mode" in Internet Explorer under Tools/Options/Security. Do you wish to proceed?,bProceed
If>bProceed=NO
Exit
Endif
Endif

//Open Notepad
Let>RP_WAIT=2
Run>notepad.exe
WaitWindowOpen>Untitled - Notepad
WaitReady>0

//Ask for start and end dates

Input>StartResult,Start Date in mm/dd/yyyy format:,9/16/2017
Input>EndResult,Start Date in mm/dd/yyyy format:,9/16/2017

VBRun>CreateIE

//Focus the new IE window
VBEval>IE.Hwnd,ie_handle
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0

//Store Original Handle
let>Org_IE_Handle=ie_handle

//Ask for start date


VBRun>Navigate,www.idx2.net/Tracker/Main.aspx
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,txtUserName,ID,0
VBRun>WaitBusy

VBRun>WebFormFill,txtPassword,Password,0
VBRun>WaitBusy

//Press login button in form
IETagEventByAttrib>https://www.idx2.net/Tdx2/Login.aspx?Re ... ogIn,click,

VBRun>WaitBusy
Wait>1

VBRun>WaitBusy
Wait>1


//Send Parameters to form
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxSender,gsaoms,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxDocType,850,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxFromDate,StartResult,0
VBRun>WaitBusy
Wait>1
VBRun>WebFormFill,_ctl5:DocumentsDir2:Criteria1:tbxToDate,EndResult,0
VBRun>WaitBusy
Wait>1

//Click Submit button
IETagEventByAttrib>https://www.idx2.net/Tdx2/Main.aspx,INP ... ton1,click,
VBRun>WaitBusy
Wait>3


//GET Number of documents available
IEGetTagsByAttrib>https://www.idx2.net/Tdx2/Main.aspx,SPA ... valueArray
//MessageModal>valueArray_1
VBRun>WaitBusy
Wait>3

// Get the DOC ID numbers
IEGetTagsByAttrib>{"https://www.idx2.net/Tdx2/Main.aspx"},A ... O,URLArray
VBRun>WaitBusy
Wait>1
Let>MSG_WIDTH=800


//Start Main Loop to capture documents
Let>x=0

//Start Loop

While>x<valueArray_1

Let>x=x+1

//Extract the document number


//MidStr>URLArray_%x%,46,11,strSub
//let>ID_Var=https://www.idx2.net/Tdx2/Docview.aspx? ... id=%strSub%

Let>tmp=URLArray_%x%
MidStr>tmp,46,11,strSub

let>ID_Var=https://www.idx2.net/Tdx2/Docview.aspx? ... id=%strSub%


VBRun>CreateIE

//SetFocus>
VBEval>IE.Hwnd,ie_handle
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0


VBRun>Navigate,%ID_Var%

VBRun>WaitBusy

//Select all text
Press CTRL
Send>a
Release CTRL

//Copy all text
Wait>1
Press CTRL
send>c
Release CTRL


//focus on Notepad

//find first matching window that starts with 1 or more printable characters and ends with "Notepad" (e.g. Untitled - Notepad)
Let>WIN_REGEX=1
SetFocus>.+Notepad$
Let>WIN_REGEX=0
wait>1

//wait until clipboard ready
Wait>1
WaitClipBoard
Wait>1

//Paste onto Notepad
Press CTRL
Send>v
Release CTRL
wait>1

//close IE Window
Let>WIN_USEHANDLE=1
SetFocus>ie_handle
Let>WIN_USEHANDLE=0
Press ALT
Press F4
wait>1

//Focus on parent IE Window
Let>WIN_USEHANDLE=1
SetFocus>Org_IE_Handle
Let>WIN_USEHANDLE=0

//Reset loop IE variable
let>ie_handle=""


EndWhile

MessageModal>Finished!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: While Loop copy and Paste issue

Post by Marcus Tettmar » Thu Nov 09, 2017 2:46 pm

When you say it's not working which bit isn't working. Does it do the select all or not? If it looks like it does the select all have you tried inserting a breakpoint or MessageModal so you can verify what's in the clipboard. If it's not even selecting all then I'd guess the main IE window is not being focused correctly. Is it focusing Notepad?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Donf
Newbie
Posts: 7
Joined: Tue Jul 11, 2017 11:39 am

Re: While Loop copy and Paste issue

Post by Donf » Thu Nov 09, 2017 2:52 pm

Marcus Tettmar wrote:When you say it's not working which bit isn't working. Does it do the select all or not? If it looks like it does the select all have you tried inserting a breakpoint or MessageModal so you can verify what's in the clipboard. If it's not even selecting all then I'd guess the main IE window is not being focused correctly. Is it focusing Notepad?
First pass everything works OK and pastes onto notepad.
On the second pass, it focuses on the IE window but it does not select all and copy it then goes to notepad and pastes nothing thus I am assuming that the copy is copying empty cells as otherwise it would paste the prior clipboard. You can see it does not select all during execution.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: While Loop copy and Paste issue

Post by Marcus Tettmar » Thu Nov 09, 2017 5:26 pm

Ok, so I think I may have seen the issue. The fact that the CTRL-a is failing made me wonder if some other key is still pressed down and looking further down your code I see you issue an ALT-F4 to close IE. Trouble is there's no Release ALT. So ALT is never getting released. The up shot will be that your subsequent CTRL-A's are actually ALT+CTRL+A which is something else.

So, find this:

Press ALT
Press F4

And on the line after add:

Release ALT
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Donf
Newbie
Posts: 7
Joined: Tue Jul 11, 2017 11:39 am

Re: While Loop copy and Paste issue

Post by Donf » Thu Nov 09, 2017 6:22 pm

Marcus Tettmar wrote:Ok, so I think I may have seen the issue. The fact that the CTRL-a is failing made me wonder if some other key is still pressed down and looking further down your code I see you issue an ALT-F4 to close IE. Trouble is there's no Release ALT. So ALT is never getting released. The up shot will be that your subsequent CTRL-A's are actually ALT+CTRL+A which is something else.

So, find this:

Press ALT
Press F4

And on the line after add:

Release ALT
That was the issue. It now works after inserting some wait commands here and there.

Thank you for your help! It's so easy to miss...

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts