Jump to content

Data (table) from web page to excel


Go to solution Solved by water,

Recommended Posts

Posted

Here it's the code. It's good and works for me but...

My problem: It's  opening the "Test.xlsm" and working in it and still working while I using this "Test.xlsm" excel. Cause I always using the "Test.xlsm" therefore I would like to the currently opened Test.xlsm working in DataSheet and don't open another "Test.xlsm" Excel.

Code:

#include <IE.au3>
#include <Array.au3>
#include <Excel.au3>

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsm")
$oWorkbook.Sheets("DataSheet").Activate
Local $iRow2Write2 = 1
Local $oIE = _IECreate("URL", 0, 0)
Local $oTableCollection = _IETableGetCollection($oIE)
For $oAll In $oTableCollection
    $aTableData = _IETableWriteToArray($oAll, True)
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aTableData, "A" & $iRow2Write2)
    $iRow2Write2 = $oWorkBook.Activesheet.UsedRange.Rows.Count + 1
 Next

_IEQuit($oIE)

Thanks for helping!

Posted

You should always check @error and @extended after calling an _Excel_* function to make sure everything worked as expected.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Check the help file. Every _Excel_* function comes with examples that do full error checking.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 2/15/2022 at 6:52 PM, water said:

You should always check @error and @extended after calling an _Excel_* function to make sure everything worked as expected.

Expand  

Okay.  The examples in the help file run without error. 

Posted

That's fine because I have written those example scripts, added code to do the error checking and tested them ;)
Now guess what you have to do to your code?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)
  On 2/15/2022 at 8:32 PM, water said:

That's fine because I have written those example scripts, added code to do the error checking and tested them ;)
Now guess what you have to do to your code?

Expand  

I'm trying...

Update: It worked! Sure, the code "isn't nice", but it works! :)

Edited by pecsenye
Posted

:)

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 2/16/2022 at 9:05 AM, water said:

:)

Expand  

Thanks! I have an other question: is there any solution that the code in the url came from an Excel cell? For example if A1 cell valie = www.autoit.com then open this link, if A1 cell value = www.google.com then open it. 

  • Solution
Posted

Use _Excel_RangeRead to read the cell into a variable ($sCellValue) , then use "ShellExecute($sCellValue)" to call the default browser and open the URL.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 2/16/2022 at 11:41 AM, water said:

Use _Excel_RangeRead to read the cell into a variable ($sCellValue) , then use "ShellExecute($sCellValue)" to call the default browser and open the URL.

Expand  

Ok, so this is original code:

Local $sFileName1 = @ScriptDir & "\Test.xlsm"

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsm")
$oWorkbook = _Excel_BookAttach($sFileName1)
$oWorkbook.Sheets("DataSheet").Activate
Local $iRow2Write2 = 1
Local $oIE = _IECreate("URL", 0, 0)
Local $oTableCollection = _IETableGetCollection($oIE)
For $oAll In $oTableCollection
    $aTableData = _IETableWriteToArray($oAll, True)
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aTableData, "A" & $iRow2Write2)
    $iRow2Write2 = $oWorkBook.Activesheet.UsedRange.Rows.Count + 1
 Next

_IEQuit($oIE)

It's working...

I trying what you wrote to me, but I need help! :) 

Local $sFileName1 = @ScriptDir & "\Test.xlsm"
$sCellValue = $oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2)

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsm")
$oWorkbook = _Excel_BookAttach($sFileName1)
$oWorkbook.Sheets("DataSheet").Activate
Local $iRow2Write2 = 1
ShellExecute($sCellValue)
;~ Local $oIE = _IECreate("URL", 0, 0)
Local $oTableCollection = _IETableGetCollection($oIE)
For $oAll In $oTableCollection
    $aTableData = _IETableWriteToArray($oAll, True)
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aTableData, "A" & $iRow2Write2)
    $iRow2Write2 = $oWorkBook.Activesheet.UsedRange.Rows.Count + 1
 Next

This might be the last step... Thank you! :)

Posted

Again, this is wrong:

Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsm") ; Returns the workbook object
$oWorkbook = _Excel_BookAttach($sFileName1)                            ; Returns the workbook object

BookOpen opens an existing workbook, Attach connects to a workbook manually opened by the user

Means: Use Open OR Attach.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

If you need to process the URL (extract data etc.) then I suggest to use IE:

$oIE = _IECreate($sCellValue)

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)
  On 2/16/2022 at 12:50 PM, water said:

If you need to process the URL (extract data etc.) then I suggest to use IE:

$oIE = _IECreate($sCellValue)

 

Expand  

Thanks! It works now! But now I see the open website. i tried @ SW_HIDE but it doesn't work. :)

Code:

#include <IE.au3>
#include <Array.au3>
#include <Excel.au3>

Local $sFileName1 = @ScriptDir & "\Test.xlsm"

Local $oExcel = _Excel_Open()
$oWorkbook = _Excel_BookAttach($sFileName1)
$oWorkbook.Sheets("DataTable").Activate
Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2)

Local $iRow2Write2 = 1
Local $oIE = _IECreate($aResult)

Local $oTableCollection = _IETableGetCollection($oIE)
For $oAll In $oTableCollection
    $aTableData = _IETableWriteToArray($oAll, True)
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aTableData, "A" & $iRow2Write2)
    $iRow2Write2 = $oWorkBook.Activesheet.UsedRange.Rows.Count + 1
 Next
 
  _IEQuit($oIE)

 

Edited by pecsenye

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...