Closed Thread
Results 1 to 16 of 16

Create a new workbook with name without saving the file. Possible?

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Create a new workbook with name without saving the file. Possible?

    I am curious as to whether it is possible to create a new workbook with a specific file name. BUT can the file name be set without saving the file?

    (I have been reading this: http://stackoverflow.com/questions/1...ing-it-to-disk and this: http://www.ozgrid.com/forum/showthread.php?t=83024 )

    This is mainly a theoretical exercise so there is no need to point out that this has little to no use in the real world
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Create a new workbook with name without saving the file. Possible?

    Is this for coding purposes.
    For example: you want to set a variable to an added workbook?

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Create a new workbook with name without saving the file. Possible?

    You could create a new workbook from a template. The new workbook will have the template name in the Excel title bar if that's what you mean. You could have a blank template with whatever name you choose.

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by davesexcel View Post
    Is this for coding purposes.
    For example: you want to set a variable to an added workbook?
    Well the question is mainly theoretical...

    However in terms of practical use, I do have an add-in installed that has code running at Workbook_Open. This code creates a new workbook, does something and then closes the temporary workbook before finishing the Workbook_Open code. Now it works fine, except if I use Excel to create a new workbook rather than open an existing workbook. When I do, the new workbook created is default named Book2 (because the temp workbook filename defaults to Book1).

    So I was curious to see if it was possible to set the file name of the file in addition to the variable I already have assigned to the temporary workbook. (I could just SaveAs the temp workbook to set the file name and then kill the temp workbook after it's closed but setting the file name seemed the most simple option. If it can be done that is!)
    Last edited by mc84excel; 04-16-2013 at 11:01 PM. Reason: clarify

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by AlphaFrog View Post
    You could create a new workbook from a template. The new workbook will have the template name in the Excel title bar if that's what you mean. You could have a blank template with whatever name you choose.
    Hello AlphaFrog. Thanks for your suggestion.

    The new workbook would be created from VBA code for temporary purposes only (it would be closed without saving when I am finished with it). So could it be done other than creating it from a template?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Create a new workbook with name without saving the file. Possible?

    I don't know of a way to do that given all your limits.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Create a new workbook with name without saving the file. Possible?

    Why not create a reference to the new workbook when you create it?

    Then you could reference the workbook with that in the rest of the code without never having to use it's name.
    If posting code please use code tags, see here.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by AlphaFrog View Post
    I don't know of a way to do that given all your limits.
    That's OK (I am only after a solution to see what Excels limits are). I appreciated your help.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by Norie View Post
    Why not create a reference to the new workbook when you create it?

    Then you could reference the workbook with that in the rest of the code without never having to use it's name.
    Thanks for the suggestion Norie.

    I could (and already do) do that. However I am curious to see if the workbook filename can be set (without resorting to SaveAs to do so).
    Last edited by mc84excel; 04-17-2013 at 08:10 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    A big thank you to all those who have made suggestions so far.

    The only problem is that these suggestions aim to assign a variable reference to the new workbook. They don't set the workbook name.

    It's my fault for not explaining the thread question well enough in the opening post. I'll try again. See code below.

    Please Login or Register  to view this content.
    The first sub works fine. It is possible to set the worksheet name when you add a new worksheet to Worksheets. You don't need to save the file in order to set the worksheet name.

    The second sub doesn't work. It would appear that it is not possible * to set the workbook name when you add a new workbook to Workbooks.

    You can't even add a new workbook (with the name defaulting to Book#) and then add the workbook name in the next line unless you resort to SaveAs to do so.

    I find the inconsistency between these 2 scenarios (add new sheet, add new workbook) rather jarring. I can't think of a reason why MicroSoft would want to prevent a user setting a workbook filename without saving first. Unless of course, it was an oversight on their part?


    I'm not the only one who is seeking a solution on this scenario. See:
    http://www.ozgrid.com/forum/showthread.php?t=83024
    http://www.ozgrid.com/forum/showthread.php?t=52305


    * unless someone reading this knows otherwise?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Why do you want/need to name the workbook?

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by Norie View Post
    Why do you want/need to name the workbook?
    I don't really want or need to!

    I am mainly seeking this knowledge because I am curious to see whats Excels limits are (post #1). And because the inconsistency between the treatment of add new book to Workbooks collection vs add new sheet to Worksheets collection (as covered in post #10) is kind of bugging me.

    (I did cover a possible practical use in post #4 but I could work around this with SaveAs file name, close workbook, kill file name. So I'm not after this solution for any real practical use. The answer is its own reward! )

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Create a new workbook with name without saving the file. Possible?

    you can't. there is no inconsistency there-you can't name a workbook in the ui either unless you save it but you can a worksheet. there is no point that I can see in naming a workbook without saving it (your point 4 doesn't give a reason as far as I can see-as Norie already said you can use a variable to refer to the workbook object without ever knowing or caring about its name). you didn't say why you can't use a template
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Quote Originally Posted by JosephP View Post
    there is no point that I can see in naming a workbook without saving it
    Well I did say that this question was mainly theoretical.

    Quote Originally Posted by JosephP View Post
    (your point 4 doesn't give a reason as far as I can see-as Norie already said you can use a variable to refer to the workbook object without ever knowing or caring about its name)
    Sample code below is inserted into an add-in and the sub called from workbook open. Now open Excel and create new workbook. New workbook name defaults to Book2 because of this code.
    Please Login or Register  to view this content.

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Create a new workbook with name without saving the file. Possible?

    so use a template. if you don't you'll still have to create book1 before you can rename it and the "problem" remains ;-)

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Create a new workbook with name without saving the file. Possible?

    Firstly a big thank you to everyone who has contributed to this thread.
    (I'm pleasantly surprised that a thread I was only asking out of mild curiosity attracted far more suggestions than threads that I require solutions for practical reasons).


    I'm afraid that I will have to close this thread as it is getting off the topic IMHO. (The focus keeps shifting to alternative solutions to the weak practical use example that I offered in post #4

    I will mark it as SOLVED - It can't be done. ( I'm not entirely convinced. Absence of evidence is not evidence of absence. The most convincing argument against it so far was made by JosephP in the first two lines of post #13).


    P.S. I have another theoretical thread if anyone is interested... http://www.excelforum.com/excel-prog...roperties.html

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1