Categories
VisualBasic Scripting

VBS Convert HTML Tables

Visual Basic script to convert HTML to CSV or TEXT using the Excel.Application object.

'****************************************************************************************
'bestandsnaam: ConvertTable2X.vbs
'geschreven_door: Tim van Kooten Niekerk
'versie: 17:38 15-4-2010
'Converteer html tabellen naar CSV of TEXT m.b.v. het Excel object
'****************************************************************************************

' Global variable and const declarations
option explicit
'on error resume next
dim oEXC, oArg
const xlCSV=6
const xlTXT=21

' Global objectdeclarations
set oArg = WScript.Arguments 
set oEXC = CreateObject("Excel.Application")

' Functie inlezen argumenten van de commandline (0-?)
function fReadArgument(vArgNumber)
  On Error Resume Next
  fReadArgument = oArg(vArgNumber)
end function

' Convert2X function...
function fTable2X(sHTMLFile, sCSVFile, sFileType)
  if (sHTMLFile ‹› "") and (sCSVFile ‹› "") then
    dim oWB
    set oWB =  oEXC.Workbooks.Open (sHTMLFile)
    'oEXC.Visible = True
    oEXC.Application.DisplayAlerts = False
    oEXC.ActiveWorkbook.SaveAs sCSVFile,  sFileType
    oEXC.ActiveWorkbook.Close False 
    oEXC.Application.DisplayAlerts = True

    ' Opschonen
    Set oWB =  Nothing
  end if
end function

' Start programma...
if fReadArgument("0") ‹› "" and fReadArgument("1") ‹› ""  and fReadArgument("2") ‹› "" then
  fTable2X fReadArgument("0"), fReadArgument("1"), fReadArgument("2")
else
  MsgBox ("Een of meerdere argumenten ontbreken..." & vbCrLf & vbCrLf & "Gebruik: convertTable2X   " & vbCrLf & "(filetypenum: 6=CSV, 21=TXT)")
end if

' Opschonen
set oArg = Nothing
set oEXC = Nothing