3

I am saving a file using the command

FilNam = "//drive/folder/Test"

File.SaveAs FileName:=FilNam, FileFormat:=51
File.SaveAs FileName:=FilNam, FileFormat:=52

Here is how it appears

enter image description here

But I am able to select this file and open with excel to see the data inside them. I would want this to appear as en excel file. How to fix this error.

1
  • 2
    FilNam = "//drive/folder/Test.xlsx" or FilNam = "//drive/folder/Test.xlsm". I forgot which type is 51 v 52 off hand. Basically include extension. Jan 29, 2018 at 18:47

1 Answer 1

7

As mentioned in the comments, it's better to include the extension when you name the file. In your case:

51  is for Open XML Workbook (*.xlsx)
52  is for Open XML Workbook With Macros Enabled (*.xlsm)

For all the possible file types, you can have a look at the XlFileFormat Enumeration page on the MSDN.

Additionally, as mentioned by Jeeped, if you are saving on a Windows location, the extension in the filename is not absolutely necessary since it should default automatically to the extensions mentioned above. Now, from the style of filename that you are using, I guess that you are saving the file to a remote Unix location which is probably why the extension is not added automatically. It should be different if you were saving on your desktop.

3
  • 2
    I would disagree. The FileFormat:=51 should add the correct file xlsx extension and leaving it off makes the code more generic; e.g xls or csv can be saved as xlsx without modification if any existing extension is removed. There must be other issues at stake.
    – user4039065
    Jan 29, 2018 at 19:57
  • 1
    @Jeeped Thanks for the comment. I've adjusted my answer to address the potential cause of the missing extension. Regarding making the code more generic by not specifying the extension, I have mixed feelings. I'd personally rather be more specific to avoid any surprises. Jan 29, 2018 at 22:28
  • 1
    @Jeeped I often save sheets in xlTextMSDOS using extensions other than .txt and VBA SaveAs doesn't add the .txt to the name. Save manually and .txt is added! Jan 30, 2018 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.