The file format and extension of filename.csv don't match - vba

I saved a file in .csv format using VBA. Below is the code:
ActiveWorkbook.SaveAs "FilePath" & Format(Now() - 3, "mm.dd.yy") & ".csv"
The file is saved successfully. However, while opening the file, it is throwing the error "The file format and extension of filename.csv don't match. The file could be corrupted or unsafe". I am trying to suppress this warning message. It would be great if someone helps me to know what I am missing here.

i guess you are missing the fileformat.
ActiveWorkbook.SaveAs Filename:="FilePath" & Format(Now() - 3, "mm.dd.yy") & ".csv", _
FileFormat:=xlCSV
If this Fileformat is not working, you can have a look in the following link,
where you can find more versions for using CSV:)
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel

Related

Word Macro for Mail Merge export to PDF

I'm running a macro in Word which worked fine before my last clean format.
The mail-merge word reads records from an Excel file.
It gives me a run-time error 5941 on the following line:
ActiveDocument.SaveAs FileName:=strDirectory & "\" & ActiveDocument.MailMerge.DataSource.DataFields("company_name").Value & ".pdf", FileFormat:=wdFormatPDF
I don't know if a reference needs to be added to the Macro or what? Pls help.
Z

Converting IQy files to XLSX using VBA

I currently wrote a Pyhon script that extract metadata from SharePoint online using the "Export to Excel" button. When the data saves it saves as a IQy files (internet query file) and I need to save it as an XLSX. Theres about 30 files that I want to loop through and save.
These IQy files can be opened with excel and then I can use saveas to save it as an XLSX. Is there a way to automate this? I found some code in another forum but I don't completely understand it.
ThisWorkbook.Sheets.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
ActiveWorkbook.Close
the line I don't understand is ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
Would it be possible to get an explanation of this line? or is there another way to use the saveas to convert these files into XLSX? I'm open to using any other language as well.
This seems pretty clear. It's calling SaveAs to save the workbook to a particular path, using the FileFormat argument to save it as an XLSX file, which is type 51.

How to save as with VBA in current location

I'm currently saving my excel file with this command:
ActiveWorkbook.SaveAs Filename:="pleasework", FileFormat:=52
But when it saves it, it saves it in documents.
I want to save it in the current location of the macro (of the file, where it's activated).
Any advice? Changing it to:
Filename:="C:/pleasework"
Won't work...
Thanks!
Give the following a try
...
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\pleasework", FileFormat:=52
...
In addition you may first want to check whether Application.ActiveWorkbook.Path returns a valid path so in case your currently open file wasn't saved yet you won't try to save it to an invalid path.
Try this
Option Explicit
Sub Book_Path()
Debug.Print ThisWorkbook.Path
ActiveWorkbook.SaveAs FileName:=ThisWorkbook.Path & "\" & "pleasework", FileFormat:=52
End Sub

How can I put a function the FileName parameter of a File...SaveAs?

I am trying to create a file using VBA, name the file and embed a date into the filename. In Mac Excel 2011, if "wb" were the reference for the newly created workbook, it would have been no problem to say...
wb.SaveAs Filename:="Old_New_Cust" & Cstr(Date) & ".xlsm", FileFormat:=52
In Excel 2016 and El Capitan, this particular line gives an error saying the file has been saved as a 9-digit number which I can never find.
Whenever I use the FileFormat parameter, this results in an error so I dropped it for experimentation purposes. I have tried putting the entire filename (including the date) into a string variable and running...
wb.SaveAs Filename:=xxx, := FileFormat:=52 with xxx being the unquoted variable name referring to the filename. This interprets the variable name as a literal.
The same result with "\FileNameString", "$FileNameString", "FileNameString", "/$FileNameString", Application.Evaluate(FileNameString), [FileNameString]. Either I get an error message stating the file can't be saved, the file is saved as a nine-digit integer, or the VBA won't execute the line.
The only way I can get this to work is to use a literal for the file name without trying to execute any functions such as "Date" or "Cstr(Date)" in the line. Does anybody know how to either execute a function (like we used to be able to) or get a string from a variable name while using the FileName parameter?
Here is something I use,
Sub G5()
Dim Path As String
Dim filename As String
Path = "C:\Users\ME\Documents\Testing\" & _
Range("G5") & "\" & theMonth & "\" 'change this path to whatever destination you want the files saved to
filename = Range("G5")
ActiveWorkbook.SaveAs filename:=Path & filename & "-" & Format(Date, "mmddyyyy") & ".xlsm", FileFormat:=52
End Sub

VBA - concatenate filename with date

I am trying to create a macro which saves a file and sends it as e-mail. The issue is when saving the file I get the error saying:
Run-time error'1004':
Document not saved. The document may be open, or an error may have been encountered when saving.
The code in context is:
ChDir "C:/Users/username/Desktop"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:/Users/username/Desktop/test-save_" & Format(Date, "ddmmmyyyy") & ".pdf", OpenAfterPublish:=True
Forgive me if the mistake is silly but it's my first experience with vba.
Thanks in advance,
Regards,
Joseph
Try backslashes
"C:\Users\username\Desktop\test-save_