Pass variable (Extract) from iim to vbs script

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
Gutt
Posts: 5
Joined: Thu Nov 16, 2017 6:41 pm

Pass variable (Extract) from iim to vbs script

Post by Gutt » Thu Nov 16, 2017 6:52 pm

HI, I am new to imacros and having trouble passing an extracted variable to the vbs script. Below is the script -- what am I missing here? The variable is successfully printed to the csv at the end of the macro. I am going to have this as a loop but just wanted to get the first line item correct prior to adding the loop. I assume its just an over site at the end there, as i just started a day ago. I need to get this variable because documents are added to the site several times a day and they need to be processed quickly -
so, i am going to be checking this variable (the file name) against what has already been processed.

Also, if anyone has any suggestions as to making this cleaner or more efficient (as is or when I add the loop) i'd appreciate any feedback from experienced users.

Thanks!

Code: Select all

Option Explicit

Dim iim, iim1, status, date, fso, objFso, objFile, extract, strFile, FileNo, iret, WshShell, iMacros, FileInfo, CleanedFileInfo, varnow, varDateFull, varYear, varMonth, varDay

Set WshShell = CreateObject("WScript.Shell")
Set iim1 = CreateObject("iMacros")
Set objFSO = CreateObject("Scripting.FileSystemObject")

varnow = now
varMonth = Month(varnow)
varDay = Day(varnow)
varYear = Year(varnow)
varDateFull = varMonth & "-" & varDay & "-" & varYear & ".csv"

strFile = "c:\temp\output" & varDateFull
	If objFSO.FileExists(strFile) Then
	  'Set objOutputFile = objFSO.OpenTextFile (strFile, FOR_WRITING)
	   Set objFile = objFSO.OpenTextFile (strFile, ForAppending, OpenAsASCII)
	Else
	  'Set objOutputFile = objFSO.CreateTextFile(strFile)
	   Set objFile = objFSO.CreateTextFile (strFile, ForAppending, OpenAsASCII)
	End If
	If Err <> 0 Then
	  Wscript.Echo "Unable to open " & strFile & " for output."
	  WScript.Quit
	End If
iret = iim1.iimOpen("-ie", true)

Const ForAppending = 8
Const CreateIfNotExist = True
Const OpenAsASCII = 0

Dim macro
macro = macro + "'SET !PLAYBACKDELAY 0.00" + vbNewLine
macro = macro + "URL GOTO=https://www.site.com/" + vbNewLine
macro = macro + "TAG SELECTOR=#httpd_username CONTENT=Username" + vbNewLine
macro = macro + "TAG SELECTOR=#loginButton" + vbNewLine
macro = macro + "SET !ENCRYPTION NO" + vbNewLine
macro = macro + "TAG SELECTOR=#httpd_password CONTENT=PW{{!NOW:yyyymm}}" + vbNewLine
macro = macro + "WAIT SECONDS=2" + vbNewLine
macro = macro + "TAG SELECTOR=#loginButton" + vbNewLine
macro = macro + "WAIT SECONDS=3" + vbNewLine
macro = macro + "TAG SELECTOR=#serviceList>A>IMG" + vbNewLine
macro = macro + "URL GOTO=https://www.webpage.com" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "TAG POS=1 TYPE=B ATTR=TXT:Mail<SP>Box" + vbNewLine
macro = macro + "'TAG POS=1 TYPE=A ATTR=TXT:TEXT1<SP>and<SP>TEXT2<SP>Outbox<SP>(TEXT3)" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH FORM=ID:downloadFiles ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "TAG SELECTOR=""HTML>BODY>DIV>DIV:nth-of-type(2)>DIV>DIV>DIV>DIV:nth-of-type(2)>UL>LI>A""" + vbNewLine
macro = macro + "WAIT SECONDS=2" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH FORM=ID:downloadFiles ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "SET !EXTRACT NULL" + vbNewLine
macro = macro + "TAG POS=1 TYPE=TD ATTR=TXT:*REBRKSO.* EXTRACT=TXT" + vbNewLine
'macro = macro + "PROMPT {{!EXTRACT}}" + vbNewLine
macro = macro + "SAVEAS TYPE=EXTRACT FOLDER=c:\temp FILE=output.csv" + vbNewLine

iret = iim1.iimPlayCode(macro)

FileInfo = iim1.iimGetExtract()
CleanedFileInfo = (Replace(FileInfo,"User File Download - ", ""))
WshShell.Popup CleanedFileInfo,, "Test Output Popup"
objFile.WriteLine varDateFull & "," & time & "," & FileInfo & ","
objFile.Close
iret = iim1.iimExit()
WScript.Quit()
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Pass variable (Extract) from iim to vbs script

Post by chivracq » Thu Nov 16, 2017 9:23 pm

Gutt wrote:HI, I am new to imacros and having trouble passing an extracted variable to the vbs script. Below is the script -- what am I missing here? The variable is successfully printed to the csv at the end of the macro. I am going to have this as a loop but just wanted to get the first line item correct prior to adding the loop. I assume its just an over site at the end there, as i just started a day ago. I need to get this variable because documents are added to the site several times a day and they need to be processed quickly -
so, i am going to be checking this variable (the file name) against what has already been processed.

Also, if anyone has any suggestions as to making this cleaner or more efficient (as is or when I add the loop) i'd appreciate any feedback from experienced users.

Thanks!

Code: Select all

Option Explicit

Dim iim, iim1, status, date, fso, objFso, objFile, extract, strFile, FileNo, iret, WshShell, iMacros, FileInfo, CleanedFileInfo, varnow, varDateFull, varYear, varMonth, varDay

Set WshShell = CreateObject("WScript.Shell")
Set iim1 = CreateObject("iMacros")
Set objFSO = CreateObject("Scripting.FileSystemObject")

varnow = now
varMonth = Month(varnow)
varDay = Day(varnow)
varYear = Year(varnow)
varDateFull = varMonth & "-" & varDay & "-" & varYear & ".csv"

strFile = "c:\temp\output" & varDateFull
	If objFSO.FileExists(strFile) Then
	  'Set objOutputFile = objFSO.OpenTextFile (strFile, FOR_WRITING)
	   Set objFile = objFSO.OpenTextFile (strFile, ForAppending, OpenAsASCII)
	Else
	  'Set objOutputFile = objFSO.CreateTextFile(strFile)
	   Set objFile = objFSO.CreateTextFile (strFile, ForAppending, OpenAsASCII)
	End If
	If Err <> 0 Then
	  Wscript.Echo "Unable to open " & strFile & " for output."
	  WScript.Quit
	End If
iret = iim1.iimOpen("-ie", true)

Const ForAppending = 8
Const CreateIfNotExist = True
Const OpenAsASCII = 0

Dim macro
macro = macro + "'SET !PLAYBACKDELAY 0.00" + vbNewLine
macro = macro + "URL GOTO=https://www.site.com/" + vbNewLine
macro = macro + "TAG SELECTOR=#httpd_username CONTENT=Username" + vbNewLine
macro = macro + "TAG SELECTOR=#loginButton" + vbNewLine
macro = macro + "SET !ENCRYPTION NO" + vbNewLine
macro = macro + "TAG SELECTOR=#httpd_password CONTENT=PW{{!NOW:yyyymm}}" + vbNewLine
macro = macro + "WAIT SECONDS=2" + vbNewLine
macro = macro + "TAG SELECTOR=#loginButton" + vbNewLine
macro = macro + "WAIT SECONDS=3" + vbNewLine
macro = macro + "TAG SELECTOR=#serviceList>A>IMG" + vbNewLine
macro = macro + "URL GOTO=https://www.webpage.com" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "TAG POS=1 TYPE=B ATTR=TXT:Mail<SP>Box" + vbNewLine
macro = macro + "'TAG POS=1 TYPE=A ATTR=TXT:TEXT1<SP>and<SP>TEXT2<SP>Outbox<SP>(TEXT3)" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH FORM=ID:downloadFiles ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "TAG SELECTOR=""HTML>BODY>DIV>DIV:nth-of-type(2)>DIV>DIV>DIV>DIV:nth-of-type(2)>UL>LI>A""" + vbNewLine
macro = macro + "WAIT SECONDS=2" + vbNewLine
macro = macro + "TAG POS=1 TYPE=INPUT:SEARCH FORM=ID:downloadFiles ATTR=* CONTENT={{!NOW:yyyy-mm-dd}}" + vbNewLine
macro = macro + "SET !EXTRACT NULL" + vbNewLine
macro = macro + "TAG POS=1 TYPE=TD ATTR=TXT:*REBRKSO.* EXTRACT=TXT" + vbNewLine
'macro = macro + "PROMPT {{!EXTRACT}}" + vbNewLine
macro = macro + "SAVEAS TYPE=EXTRACT FOLDER=c:\temp FILE=output.csv" + vbNewLine

iret = iim1.iimPlayCode(macro)

FileInfo = iim1.iimGetExtract()
CleanedFileInfo = (Replace(FileInfo,"User File Download - ", ""))
WshShell.Popup CleanedFileInfo,, "Test Output Popup"
objFile.WriteLine varDateFull & "," & time & "," & FileInfo & ","
objFile.Close
iret = iim1.iimExit()
WScript.Quit()
FCIM...! :mrgreen: (Always mention your FCI when you open a Thread, read my Sig..., many Commands are not implemented for all Browsers/Versions or get broken or behave differently for/from some specific Version...)
=> iMB v12.0/v11.5/v11.0...?, OS...?

But yep, the Behaviour you describe is normal as the 'SAVEAS' at the end of your on-the-fly '.iim' Macro resets the '!EXTRACT' Built-in Var... which then is empty when you try to pull its Content with 'iimGetExtract()' from your main '.vbs' Script...
=> You need to store it in some Temp Var prior to doing the 'SAVEAS' and you put it back into '!EXTRACT' after the 'SAVEAS'... 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Gutt
Posts: 5
Joined: Thu Nov 16, 2017 6:41 pm

Re: Pass variable (Extract) from iim to vbs script

Post by Gutt » Fri Nov 17, 2017 5:20 am

Ah, sorry - newbie mistake!

That makes sense now! haha, ironically, I dont even need the save as - I was just doing that to make sure that I was capturing the variable. Will edit accordingly and try tomorrow, im sure there will be no problems. Thanks for your help!
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Pass variable (Extract) from iim to vbs script

Post by chivracq » Fri Nov 17, 2017 5:59 am

Gutt wrote:Ah, sorry - newbie mistake!

That makes sense now! haha, ironically, I dont even need the save as - I was just doing that to make sure that I was capturing the variable. Will edit accordingly and try tomorrow, im sure there will be no problems. Thanks for your help!
Yep "sorry" (about FCI not mentioned), no real need, but you could/should still mention it... => Which iMB Version are you using...? (I won't react otherwise next time you open a Thread, I only reacted to this one because it was your first Thread on the Forum...)

Well about the 'SAVEAS', I had a look at your whole Script, your '.vbs' Script looks quite "professional" (you could add Comments to your Script btw, for yourself and/or if sbd else one day needs to "maintain" it, as I guess it will become a bit more complex...), but using some regular 'SAVEAS' to keep track of what your Script is doing and handling some Logging yourself is a good Idea, together always with a Timestamp, you'll be happy one day to have this Log File in case "stg" goes wrong, to be able afterwards to find out if the Pb came from your Script, or the HTML Structure of the Site changing, or a Connection Pb or the Site being down...
You can use the 'Profiler' as well for more or less the same Purpose, but the Profiler slows down the Execution of "heavy" or looping Macros by about 30% in my Experience, and I often find those "30%" a "too expensive" Price to pay if you need to go for Performance... (Oh ja, and the 'Profiler' cannot be enabled per Macro, it is only a YES/NO Global Setting, which is not very practical in my Opinion...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
hoguz2
Posts: 15
Joined: Wed Apr 01, 2015 11:03 am
Location: TURKEY

Re: Pass variable (Extract) from iim to vbs script

Post by hoguz2 » Sat Nov 18, 2017 11:05 am

hi i have same problem in VBA ( excel )

so sory i dont speak english wery well... if I understand it correct


i cant use VBS but VBA is basic VBS therefore i have an idea

please try cancel

Code: Select all

macro = macro + "SAVEAS TYPE=EXTRACT FOLDER=c:\temp FILE=output.csv" + vbNewLine
this lines in your code ..

SAVEAS TYPE=EXTRACT method will reset your all exctract value... like

Code: Select all

SET !EXTRACT NULL 


SAVEAS method = !EXTRACT NULL

and try change this line

Code: Select all

FileInfo = iim1.iimGetExtract()

Code: Select all

FileInfo = Replace(iim1.iimGetLastExtract(), "[EXTRACT]", "")
Mozilla : 53.0.2
iMacros Version : iMacros for Firefox 9.0.3
Win 8.1 64 bit
Ms_Office 2010 .TR
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Pass variable (Extract) from iim to vbs script

Post by chivracq » Sat Nov 18, 2017 5:19 pm

hoguz2 wrote:hi i have same problem in VBA ( excel )

so sory i dont speak english wery well... if I understand it correct


i cant use VBS but VBA is basic VBS therefore i have an idea

please try cancel

Code: Select all

macro = macro + "SAVEAS TYPE=EXTRACT FOLDER=c:\temp FILE=output.csv" + vbNewLine
this lines in your code ..

SAVEAS TYPE=EXTRACT method will reset your all exctract value... like

Code: Select all

SET !EXTRACT NULL 


SAVEAS method = !EXTRACT NULL

and try change this line

Code: Select all

FileInfo = iim1.iimGetExtract()

Code: Select all

FileInfo = Replace(iim1.iimGetLastExtract(), "[EXTRACT]", "")
Hum..., you make it a little bit "confusing" @hoguz2 I would think even if I understand what you are trying to explain..., but again, there is nothing "wrong" with the 'SAVEAS', and the "Mechanism" to use is pretty simple and consists of surrounding a 'SAVEAS' with a Save and Restore of the '!EXTRACT' using some Temp Var...

In pure '.iim', that would give:

Code: Select all

SET EXTRACT_Temp {{!EXTRACT}}
SAVEAS TYPE=EXTRACT FOLDER=* FILE=*
SET !EXTRACT {{EXTRACT_Temp}}
... And applied to the VBS/VBA Script of @OP...:

Code: Select all

macro = macro + "SET !EXTRACT NULL" + vbNewLine
macro = macro + "TAG POS=1 TYPE=TD ATTR=TXT:*REBRKSO.* EXTRACT=TXT" + vbNewLine
'macro = macro + "PROMPT {{!EXTRACT}}" + vbNewLine
macro = macro + "SET EXTRACT_Temp {{!EXTRACT}}" + vbNewLine
macro = macro + "SAVEAS TYPE=EXTRACT FOLDER=c:\\temp FILE=output.csv" + vbNewLine
macro = macro + "SET !EXTRACT {{EXTRACT_Temp}}" + vbNewLine
Hum..., and just to be "sure", I doubled the Backslash in the Path...! Not sure about VBS but a single Backslash wouldn't work in a '.js' Script as the JS Runtime Engine would "think" that you are trying to escape the "t" from the 'C:\temp' Folder in the Path... :idea:

+ And like I mentioned, I'm not sure the 'iimGetExtract()' embedded in the 'Replace()' is a valid Syntax, and even if it works, I consider it a bit "bad Practice" or at least "not the best Practice", ah-ah...! I often see it on Forums from "semi-Advanced" Users/Programmers trying to make their Code look more complex than necessary to "impress" other less Advanced Users, but that Construction can quickly turn into a "pain-in-the-ass" if anything goes wrong and the Statement needs to be debugged as it will need to be unpeeled like an Onion anyway for each embedded Level...

=> And that would give for that part...: ... Euh..., nothing...! ah-ah...! :oops:
Now I'm the one being confused/confusing, oops, I'm actually mixing up this current Thread with this very similar other Thread from @hoguz2 indeed... => There you go @hoguz2, you have your Solution for your 2 Pb's then, Script from @Gutt is correct for those 2 parts and you can use it as Example... 8)

You should work together Guys...!, are you sure you are not both working on the same Project, ah-ah...!? :twisted:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply