Jump to content

Appending Many Excel Files Together


Recommended Posts

So I am doing a custom inventory script that creates an excel document with 4 columns and sends a copy of the excel file over to a network location.

What I end up with is a few hundred excel files each representing a single computer.  My goal is to append all of those documents together into a single document so that I can turn the data into a table for sorting and review.

I know there are a few ways to skin this cat, just not sure the easiest and best.

I was trying using com objects based on an old thread I found by searching but I fail on my Line 10 when I try to declare the $master sheet I get not an object.

I am open to any solution, and interested in the com method as well since I am learning that stuff on the side.

My Broken Script
 

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>

$aFiles = _FileListToArray("\\vpsfs1\pstinfo\pst info\", "*.xls", $FLTA_FILES, TRUE)

;_ArrayDisplay($aFiles)

$oEX = ObjCreate("","excel.Application")
$master = $oEX.Workbooks.Open("\\vpsfs1\pstinfo\master.xlsx")

For $i = 1 to $aFiles[0]
$another = $oEX.Workbooks.Open($aFiles[$i])
$another.Activesheet.UsedRegion.Copy
$next = StringSplit($master.ActiveSheet.UsedRange.Address,"$")
$master.Activesheet.Range("a" & Activesheet.usedrange.rows.count + 1).Select
$master.Activesheet.Paste
Next

 

Link to comment
Share on other sites

Link to comment
Share on other sites

I second that. The UDF might be a bit slower but you get better error handling :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Working on it :) I should only use ExcelOpen() once and ExcelClose() at the end after my read loop correct?  Or will I need to open/close for each iteration of the loop?

Having a few issues, even when I put everything in the loop, this only gives me a result on the first pass.  Once I get it working then I need to figure out the best way to append this to an array.

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls", $FLTA_FILES, TRUE)
;_ArrayDisplay($aFiles)

For $i = 1 to $aFiles[0]
Local $oExcel = _Excel_Open(False)
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook, DEFAULT, DEFAULT)
_Excel_Close($oExcel, False, True)
_ArrayDisplay($aResult)
Next

 

Link to comment
Share on other sites

This work For me.

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls",$FLTA_FILES, TRUE)


Local $oExcel = _Excel_Open(False)

For $i = 1 to $aFiles[0]
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook, DEFAULT, DEFAULT)
_ArrayDisplay($aResult)
Next

_Excel_Close($oExcel, False, True)
close book if is needed with_Excel_BookClose

Saludos

Edited by Danyfirex
Edited
Link to comment
Share on other sites

You should move _Excel_Open/_Excel_Close to the begin/end of the script (untested):

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls", $FLTA_FILES, TRUE)
Global $oWorkbook, $aResult
Global $oExcel = _Excel_Open(False)
$oMaster = _Excel_BookOpen("\\vpsfs1\pstinfo\master.xlsx")
For $i = 1 to $aFiles[0]
    $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
    $aResult = _Excel_RangeRead($oWorkbook)
    _Excel_RangeWrite($oMaster, Default, $aResult, "A" & $oMaster.ActiveSheet.UsedRange.Rows.Count + 1)
    _Excel_BookClose($oWorkBook, False)
Next
_Excel_Close($oExcel, False, True)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Yes I had it that way first Water & Danny it was not working so I moved in in the loop for testing.

I think my issue maybe Excel related or something, at first my Excel_Open was not even working until I went and messed with Trust Center settings.

Edit: Also tried adding Book Close.

 

So it seems my issue needs to be investigated and its not because I am being dumb. 
 

Edited by ViciousXUSMC
Link to comment
Share on other sites

Could you please insert a COM error handler to your script so we get detailed error information? Please check the helpfile for ObjEvent.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I fixed it, I had to turn off protected mode for these Type 2 files that are created from the ArrayToXLS UDF on the forums.

I imagine when it goes to close/save the first file it messes up the rest in the loop.

 

This looks good, thanks guys!

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls",$FLTA_FILES, TRUE)
Local $oExcel = _Excel_Open(False)
Local $aFinalResults[0][4]

For $i = 1 to $aFiles[0]
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook)
;_ArrayDisplay($aResult)
_ArrayConcatenate($aFinalResults, $aResult)
Next

_ArrayDisplay($aFinalResults)
_Excel_Close($oExcel, False, True)

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

Do not forget to close the workbooks you opened again. Else you will end up with severel hundred of open workbooks eating up your memory.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It will. But why have hundreds of worksbooks open which you no longer need?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...