SilentRage47
SilentRage47

Reputation: 952

Create Excel workbook without any sheet

Is there a way to create an Excel workbook without any sheets in it?

This is the code that I use to create the workbook:

excelApp = new Microsoft.Office.Interop.Excel.Application();
excelBook = excelApp.Workbooks.Add();

I tried also adding

excelApp.SheetsInNewWorkbook = 0;

before the creation of the excelBook but the minimum value is 1 so the program crashes.

EDIT:

Tried also to delete the first sheet as soon as it is created but still doesn't work

Sheets excelSheetsToDelete = excelBook.Sheets[1];
excelSheetsToDelete.Delete();

I would like to be able to add my sheets with my names later without having to rename the first one.

Upvotes: 0

Views: 4992

Answers (2)

Moist Carrot
Moist Carrot

Reputation: 43

Like people have already said, Excel must contain at least 1 visible worksheet at all times. Since it seems like you just want to create a workbook, and manually specify your sheet name without having to rename the one created automatically, I wrote this quick VB Script that may work for you.

Dim objShell, objExcel, objWorksheet, strSheetName
strSheetName = InputBox("Enter a name for your new Worksheet:")
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("EXCEL.EXE")
WScript.Sleep 3000 'Wait for new excel file to open
Set objExcel = GetObject(, "Excel.Application")
Set objWorksheet = objExcel.ActiveWorkbook.Worksheets(1)
objWorksheet.Name = strSheetName

What this does is prompt you to enter a name for your new sheet. It then runs a new instance of Excel and sets the first worksheet of the newly created file as the name you specified.

If Excel includes 3 sheets when creating new workbooks, you can change this to 1 by going in any excel worbook and clicking - File > Options > General - and look for the "When the creating new workbooks" separator. There will be an option "Include this many sheets" which you can change to 1.

Also, to confirm what people have already said, if you try and change this value to 0, it will tell you "The entry must be greater than or equal to 1" meaning all workbooks must contain at least 1 visible worksheet. Hope this helped.

Upvotes: 0

Victor Gorban
Victor Gorban

Reputation: 1699

You can't create an Excel without any sheet, Excel must contain at least one sheet. Try to delete single sheet in Excel application (desktop). You won't do that.

Upvotes: 1

Related Questions