user2457968
user2457968

Reputation: 109

Create a blank workbook which is not saved

I am trying to open a workbook in separate instance. Currently, this workbook is saved on the desktop. I would want to open a new workbook which is not saved or located anywhere on my system. Below is the current code which I have. Please advise.

Sub New_Excel()
  'Create a Microsoft Excel instance via code 
  'using late binding. (No references required)
  Dim xlApp As Object
  Dim wbExcel As Object

  'Create a new instance of Excel
  Set xlApp = CreateObject("Excel.Application")

  'Open workbook, or you may place here the 
  'complete name and path of the file you want 
  'to open upon the creation of the new instance
  Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls") 

  'Set the instance of Excel visible. (It's been hiding until now)
  xlApp.Visible = True

  'Release the workbook and application objects to free up memory
  Set wbExcel = Nothing
  Set xlApp = Nothing
End Sub

Upvotes: 1

Views: 362

Answers (1)

Ken White
Ken White

Reputation: 125749

If you want to create a new blank workbook, stop trying to open an existing one. Just change the line

Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls") 

to

'Add a new, empty workbook
Set wbExcel = xlApp.Workbooks.Add

For more information, see Creating a New Workbook (the link is for Excel 2003, because it's the first one I found via Google, but it still applies, and if you want a more recent link you can probably find it the same way I did).

Upvotes: 3

Related Questions