|
-
Jan 20th, 2012, 10:29 AM
#1
Thread Starter
Member
[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
-
Jan 21st, 2012, 02:30 AM
#2
Re: Create New Workbook
you should avoid using the activeworkbook, instead set the new book to a workbook object like
vb Code:
' Create New Workbook set wb = .Workbooks.Add wb.Worksheets(1).Range("A3") = "Hello" ' This just avoids a possible error by saving an empty Book 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
-
Jan 21st, 2012, 06:45 AM
#3
Thread Starter
Member
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
-
Jan 22nd, 2012, 05:59 AM
#4
Frenzied Member
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
-
Jan 22nd, 2012, 07:08 AM
#5
Re: Create New Workbook
Moved to Office Development
-
Jan 22nd, 2012, 07:53 AM
#6
Thread Starter
Member
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.
-
Jan 22nd, 2012, 07:54 AM
#7
Thread Starter
Member
Re: Create New Workbook
Hack: Sorry, it has been quite a while since I was on the site and I had forgotten.
-
Jan 22nd, 2012, 10:29 AM
#8
Re: Create New Workbook
 Originally Posted by maurice_whittaker
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
-
Jan 22nd, 2012, 02:56 PM
#9
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
-
Jan 22nd, 2012, 02:57 PM
#10
Thread Starter
Member
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.
-
Jan 22nd, 2012, 03:07 PM
#11
Thread Starter
Member
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!
-
Jan 22nd, 2012, 03:19 PM
#12
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
-
Jan 22nd, 2012, 03:39 PM
#13
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
-
Jan 23rd, 2012, 03:18 AM
#14
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
-
Jan 23rd, 2012, 07:50 AM
#15
Thread Starter
Member
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.
-
Jan 27th, 2012, 11:37 AM
#16
Thread Starter
Member
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.
-
Jan 28th, 2012, 01:22 AM
#17
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
-
Jan 28th, 2012, 11:41 AM
#18
Thread Starter
Member
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!
-
Jan 28th, 2012, 09:01 PM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|