SaveAs sometimes not adding file extension ......

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
I have a workbook that I am making copies of and saving those copies as different names.

For some reason, what appears to be random, some of the files don't get the .xlsx extension added to the saved file.

The files are being saved with the correct name convention I setup, but the file extension is being dropped.

They are being saved in the correct location just fine.

So I will go into the directory and see files with the xlsx icon and a xlsx file extension. The type is even listed as: Microsoft Excel Worksheet. Yet, some random files have no icon (just blank, appears like a white sheet of paper), and the Type is listed as: File.

If I manually add the .xlsx extension then the icon appears (after a refresh) and the file opens just fine.

I can't figure out why it is dropping the file type extension when saving. I can't determine a pattern.

Any thoughts?


Here is the code I am using:

Code:
.....

ActiveWorkbook.SaveAs filename:=strPath & strFileName, FileFormat:=51

.....

Is there something wrong with how I am declaring the fileformat?

Maybe I should do something like:

Code:
.....

strFileName = Tracking(i, 1) & " - " & Tracking(i, 2) [B]& ".xlsx"[/B]

ActiveWorkbook.SaveAs filename:=strPath & strFileName
.....

Or perhaps something like:

Code:
.....


ActiveWorkbook.SaveAs filename:=strPath & strFileName, FileFormat:[COLOR=#333333][FONT=&quot]xlOpenXMLWorkbook[/FONT][/COLOR]
.....


Although, as I understand it, they should all do the same thing, right?

I am stumped .....

-Spydey
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You just have to debug it and see what arguments are being passed into the function... you probably have an error somewhere you haven't noticed

Use a breakpoint and step through it.
 
Upvote 0
try

ActiveWorkbook.SaveAs filename:=strPath & strFileName, FileFormatNum = 51

Although stating the fileformat does not necessarily add the extension name

somthing like

FileExtStr = ".xlsx": FileFormatNum = 51
 
Upvote 0
You just have to debug it and see what arguments are being passed into the function... you probably have an error somewhere you haven't noticed

Use a breakpoint and step through it.

Yes, I have stepped through it. If I step through through it all using F8, then all the files get saved correctly, no issues.

But I just run the code all in one go, i.e. F5, then it seems to have issues.

I will have to dig a little deeper I guess.

Thanks Cerfani!!

-Spydey
 
Upvote 0
try

ActiveWorkbook.SaveAs filename:=strPath & strFileName, FileFormatNum = 51

Although stating the fileformat does not necessarily add the extension name

somthing like

FileExtStr = ".xlsx": FileFormatNum = 51


I will give this a try and let you know what results I get.

It seems like it should be a simple fix, so hopefully this does it.


-Spydey
 
Upvote 0
@Dryver14

Just to clarify, when you state:

somthing like

Rich (BB code):
FileExtStr = ".xlsx": FileFormatNum = 51


You mean to add it to my already existing line of code, right? So something like this:

Rich (BB code):
ActiveWorkbook.SaveAs filename:=strPath & strFileName, FileExtStr = ".xlsx": FileFormatNum = 51


Did I understand you correctly?

-Spydey
 
Last edited:
Upvote 0
Ok, so I got it figured out. Thank you both @cerfani & @Dryver14.

I dove in a bit more, as Cerfani suggested.. I went back and read up a bit on the Microsoft Office VBA Dev Reference site.

By adding part of the suggestion from Dryver14, I was able to get all the files to have the correct .xlsx extension.

I do find it interesting that with all my other VBA projects, I didn't need to add this little bit of additional coding and they always saved correctly.

I could be that those were just single file saves whereas this was a larger file being parsed out to several hundred smaller files.

Anywho, it works now.

Here is the code that I changed:

Code:
ActiveWorkbook.SaveAs filename:=strPath & strFileName & ".xlsx", FileFormat:=51

-Spydey
 
Last edited:
Upvote 0
No prob, glad you got it worked out, although it is odd that it works when stepping through but not if you just let it run, there must be something different between the situations though as vba code is executed on a single thread and you stepping through the code at a slower rate shouldn't affect the output. :confused:
 
Upvote 0
No prob, glad you got it worked out, although it is odd that it works when stepping through but not if you just let it run, there must be something different between the situations though as vba code is executed on a single thread and you stepping through the code at a slower rate shouldn't affect the output. :confused:

That was my thought too. But it is working now.

Thanks again!

-Spydey
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top