File-2-Web.vbs

From iMacros
Jump to: navigation, search

This script takes values from a comma separated value file (.csv) using the DATASOURCE features.

It calls the macro Wsh-File-2-Web, which handles the file accessing directly.

The macro could also be run directly in the iMacros browser. However, this script demonstrates advanced error checking and provides greater flexibility.

' iMacros File-2-Web Script
' (c) 2008-2015 iOpus/Ipswitch Inc.  

Option Explicit

Dim message
message = "This script demonstrates how to read data from the <address.csv> text file in the /datasource directory using"
message = message + " iMacros DATASOURCE features. In other words, the macro reads the data directly,"
message = message + " this script is only responsible for the loop over the data. This is similar to pressing the LOOP button,"
message = message + "but offers more flexibility e. g. when the number of records is unknown. The script uses the macro <wsh-file-2-web.iim>." + vbCrLf + VbCrLf 
message = message + "Tip: This script has the same function as <file-2-web-method2.vbs> but uses a different method."

' find current folder
Dim myname, mypath
myname = WScript.ScriptFullName
mypath = Left(myname, InstrRev(myname, "\"))

Dim iim1, i, iret

set iim1= CreateObject ("imacros")

iret = iim1.iimOpen("")
if iret < 0 then MsgBox "Error during Initialization. Error code: "+cstr (iret)

iret = iim1.iimDisplay("Submitting Data")
if iret < 0 then MsgBox "Error iimDisplay. Error code: "+cstr (iret)

'Note: The input file name is specified in the macro
'You can also specify it inside the script with "iimSet"

'Loop through the input file until end
'We start at "2" to skip the first header line in the file
i = 2
while iret <> -1240                  ' at end of input file, iMacros stops with error -1240
   'Set the current read position
   iret = iim1.iimSet("line", cstr(i))

   'Run the macro
   iret = iim1.iimPlay(mypath & "Macros\wsh-file-2-web.iim")

'Check if user wants to stop
if (iret = - 102) Or (iret = -101) Then
    MsgBox "Stopped by user"
    WScript.Quit(iret)
End If 
  i = i + 1
wend

iret = iim1.iimDisplay("Done!")
iret = iim1.iimClose
WScript.Quit(iret)

Macro code for Wsh-File-2-Web:

TAB T=1     
TAB CLOSEALLOTHERS        
'Uses a Windows script to submit several datasets to a website, e. g. for filling an online database
' Specify input file (if !COL variables are used, IIM automatically assume a CSV format of the input file
'CSV = Comma Separated Values in each line of the file
SET !DATASOURCE address.csv
'Increase the current position in the file with each loop 
SET !DATASOURCE_LINE {{line}}
'
' Fill web form   
URL GOTO=http://demo.imacros.net/Automate/AutoDataEntry
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:fname CONTENT={{!COL1}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:lname CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:address CONTENT={{!COL3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:city CONTENT={{!COL4}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:zip CONTENT={{!COL5}}
'Tip: Use the string ($) instead of the index to select the state and country 
'Index would be the position of an entry in the combo box list, e. g. 161 for United States
'The $ option is more robust against changes of the combo box content
'Note * is used to ignore leading and trailing blanks that could be in the input data
TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=NAME:state CONTENT=$*{{!COL6}}*
TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=NAME:country CONTENT=$*{{!COL7}}*
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=NAME:email CONTENT={{!COL8}}
TAG POS=1 TYPE=BUTTON:SUBMIT FORM=ID:demo ATTR=TXT:SUBMIT  
TAG POS=1 TYPE=A ATTR=TXT:*Back*