Jump to content

Recommended Posts

Posted

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.

Posted

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:

  Reveal hidden contents

 

Posted

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!

Posted
  On 5/2/2016 at 2:18 PM, 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.

Expand  

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

My UDFs and Tutorials:

  Reveal hidden contents

 

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
×
×
  • Create New...