Results 1 to 19 of 19

Thread: [RESOLVED] Create New Workbook

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Resolved [RESOLVED] Create New Workbook

    Firstly, thank you to Si; I made progress with the Long Path problem but I can't see it on here to set it RESOLVED.

    I am in deeper water now in trying to Create a new workbook. This coding has been working fine for months but is now giving error "Type of file not consistent with its extension, (.xls here). The coding I am using is as below & I only copied this without understanding it properly.

    Create_New_Workbook MyBook ' Create a new W/Book and name it MyBook
    Workbooks.Open Filename:=MyBook ' now open it.
    ActiveWorkbook.Save
    ActiveWindow.Close


    Private Sub Create_New_Workbook(MyBook As String)
    Dim xlsApp As Excel.Application
    Set xlsApp = Excel.Application
    With xlsApp
    ' .Show Excel
    .Visible = True
    ' Create New Workbook
    .Workbooks.Add
    .ActiveWorkbook.Worksheets(1).Range("A3") = "Hello" ' This just avoids a possible error by saving an empty Book
    .ActiveWorkbook.Close SaveChanges:=True, Filename:=MyBook, Routeworkbook:=False
    End With
    Exit Sub
    End Sub

    I had assumed that the xlsApp above determined an xls file and I am very puzzled as to why it should work well and then badly. Any help would be much appreciated.
    Last edited by maurice_whittaker; Jan 27th, 2012 at 11:49 AM. Reason: To remove RESOLVED

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Create New Workbook

    you should avoid using the activeworkbook, instead set the new book to a workbook object like
    vb Code:
    1. ' Create New Workbook
    2. set wb = .Workbooks.Add
    3. wb.Worksheets(1).Range("A3") = "Hello" ' This just avoids a possible error by saving an empty Book
    4. wb.Close SaveChanges:=True, Filename:=MyBook, Routeworkbook:=False
    if the excel version installed is > 2003 then the default extention would be .xlsx (this would match your error)
    if you want to save to a .xls, you should issue a wb.saveas where you can specify the filetype to save to as well as the file extention to suit, then wb.close false
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Thanks Pete, This is very useful. I am now inclined to go with the default of .xlsx rather than fight the system! I got very confused some time ago with the differences, which caused me to stay with .xls I am very grateful that there are chaps like you with the patience to help. Maurice

  4. #4
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Create New Workbook

    have you actually updated you office package and found this error after that update?

    would be good to know in light of the comments made

    here to talk

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Create New Workbook

    Moved to Office Development

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Incidentals: No I have not updated; The major problem came when I tried to adopt westconn1's suggestion which was Set wb = .WorkBooks.Add and the compiler did not like the .WorkBooks bit
    This is due to my ignorance as to whether the "Set" should go inside a "With loop" or before it and whether a "Dim" is required for wb and if so what should the "Dim" be?
    I have tried quite a few variations, but not the right one!
    Thanks.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Hack: Sorry, it has been quite a while since I was on the site and I had forgotten.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create New Workbook

    Quote Originally Posted by maurice_whittaker View Post
    Thanks Pete, This is very useful. I am now inclined to go with the default of .xlsx rather than fight the system! I got very confused some time ago with the differences, which caused me to stay with .xls I am very grateful that there are chaps like you with the patience to help. Maurice
    Even when you are saving it as default xlsx, you will get a problem.

    To elaborate on what Pete mentioned, if you are using Excel version which is 2007 and greater then you need to set the file format as below

    Code:
    51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
    52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
    50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
    56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)
    So for example if you want to save the file as .xls then you have to use the following code.

    Code:
    Wb.SaveAs Filename:=MyBook, FileFormat:= 56
    Similarly for xlsx, the above code will be

    Code:
    Wb.SaveAs Filename:=MyBook, FileFormat:= 51
    HTH

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Create New Workbook

    I am now inclined to go with the default of .xlsx rather than fight the system!
    if your program could be used with different versions of excel you would have to provide code to handle differently, which ever way you go
    as 2003 or < requires the xls extention and would not recognise the fileformat for xlexcel12 (or event the constant for xlexcel8 if xl2000)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Thanks coolsid, I have taken note of that but I am still unable to compile the "wb = .WorkBooks.Add" line! When I succeed I will incorporate your suggestion.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Thanks Pete, My version came with the 2007 version of Office and I will not be changing for some years. How do I ensure that my system recognizes "wb = .WorkBooks.Add" ??? Do I have to Dimension wb as Object, WorkBook or something? I tried Object and it didn't work!

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Create New Workbook

    wb would be a workbook object so can be a workbook, object or variant
    if you are doing this inside xl2007 you may not require the [B]set [\b]*keyword, so try without
    i had assumed originally you were working in vb6, as your post was in the vb6 forum
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create New Workbook

    Maurice, since you are using 2007, here is a quick example. I could have written it in more compact manner but I have deliberately broken it into small parts for the sake of clarity.

    Code:
    Sub Sample()
        Dim FlName As String, Extn As String
        Dim FileExtn As Long
        
        FlName = "C:\MyFile"
        Extn = ".xls"
        
        Select Case UCase(Extn)
        Case ".XLSX": FileExtn = 51
        Case ".XLSM": FileExtn = 52
        Case ".XLSB": FileExtn = 50
        Case ".XLS": FileExtn = 56
        End Select
        
        Create_New_Workbook FlName & Extn, FileExtn
    End Sub
    
    Private Sub Create_New_Workbook(MyBook As String, Flfmt As Long)
        Dim wb As Workbook
        
        Set wb = Workbooks.Add
        wb.Sheets(1).Range("A3") = "Hello"
        wb.SaveAs Filename:=MyBook, FileFormat:=Flfmt
        wb.Close SaveChanges:=False
        
        Set wb = Nothing
    End Sub
    HTH

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Create New Workbook

    Private Sub Create_New_Workbook(MyBook As String)
    Dim xlsApp As Excel.Application
    Set xlsApp = Excel.Application
    With xlsApp
    ' .Show Excel
    .Visible = True
    ' Create New Workbook
    .Workbooks.Add
    i based the code on the posted sample, where you were working with an application object for whatever reason, that is why i believed initially your code was in vb6
    if you are now not using an application object remove the . from in front of .workbooks.add
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    OOH ECK! Thanks all, This will take me a while to assimilate. My fiddling about got it to compile with Dim wb as FileStyles and removing the dot before WorkBooks. More luck than judgement, I'm afraid. Even then when I check what I have against all your suggestions it may have only compiled and not worked. I will not yet mark my string as RESOLVED. I don't want to be too hasty again! I am much obliged for all your help everyone.

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Create New Workbook

    Hello again everybody! I feel like Frank Spencer here! I find that my version of VB is 6.5 which is a version of VB 6 isn't it? Anyway I think I am Ok now with the main problem of New WorkBook Creation so I will mark this RESOLVED. However, I have another thrilling episode to come for you to look forward to?? I will start another string for this on (hopefully correctly) VB6. Thanks again everybody you have been very kind.

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Create New Workbook

    vb6 (visual basic 6) is for creating stand alone programs (exe files)
    vb 6.3 or 6.5 etc (depending on the application hosting it) are all VBA (visual basic for applications)
    and depending on the application hosting, as to what objects and methods are natively available to it, from any application you can automate other applications

    much of the code that will run in vb6 can run in VBA and vice versa, but some of the objects would need to be created when required

    vba queries should be posted in office development forum, rather than the VB6 forum, even though in many cases the answer would be the same, but specifically queries regarding application objects are more likely to get responses in office development
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: [RESOLVED] Create New Workbook

    Thanks again Pete, I am self-taught and I don't think the teacher was all that good! I saw your reply too late to avoid posting in the wrong place. I did remember the code tags though!

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Create New Workbook

    doing it right helps to get best response
    generally i reply to any in this forum first, then the vb6
    probably a moderator will move in due course
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width