Jump to content

Creating new excel file


Recommended Posts

Hey guys.

 

I've been searching, and reading the documentation, but I can't get this to work.

 

I need to programmatically create a new Excel (xlsx) file in a directory.

 

The examples I've seen online are using an excel workbook that already exists, and just renaming it or changing the file format.

 

I'm sure it is really simple, but I can't get it to work. This is what I have:

 

Local $oExcelTest = _Excel_Open()
_Excel_BookNew($oExcelTest,5)
Local $sWorkbook = @ScriptDir & "\testFile.xlsx"
_Excel_BookSaveAs($oExcelTest, "testFile.xlsx", $xlWorkbookDefault, True);
Sleep(50);

 

I've tried making the 2nd parameter $sWorkbook, but that doesn't change anything. The script does open a new excel window with 5 sheets, but it doesn't change the name and save it.

Link to comment
Share on other sites

What is the value of @error after you have called

#include <Excel.au3>
Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookNew($oExcelTest, 5)
Global $sWorkbook = @ScriptDir & "\testFile.xlsx"
_Excel_BookSaveAs($oWorkbook, $sWorkbook, $xlWorkbookDefault, True)

 

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

Interesting, that seemed to have worked. I had to change the line that you posted

Global $oWorkbook = _Excel_BookNew($oExcelTest, 5)

 to "_Excel_BookNew($oExcel,5)"

 

but then it worked. Thanks.

 

I guess my code was not working because I didn't assign the return value of _Excel_BookOpen to an object, and then not using that object, so the SaveAs didn't know what to point to.

 

 

Interestingly enough, the @error value was zero for both your code and my code.

 

 

 

Thanks for the help!

Link to comment
Share on other sites

2 minutes ago, Nighterfighter said:

I guess my code was not working because I didn't assign the return value of _Excel_BookOpen to an object, and then not using that object, so the SaveAs didn't know what to point to.

Interestingly enough, the @error value was zero for both your code and my code.

Correct. _Excel_BookSaveAs expects the workbook object as parameter 1. You provided the Excel application object so it failed.

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...