VBA - concatenate filename with date - vba

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_

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.

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

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

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

vba excel 2010 Environ("username") not working in saveas filepath

For some reason I can get the Message Box to tell me what the username is, but it doesn't work the same way within the save as path. I get a "Cannot find path" error.
Not sure how to fix this. I've also looked at references, and they seem like they are correct.
If I change the path to include the actual username per computer, the macro works fine. But I need this to be a global macro for any computer/user. Any help would be greatly appreciated.
Here is what I have:
Sub SAVEAS_2010()
'
' SAVEAS_2010 Macro
MsgBox Environ("username")
Dim UserName As String
UserName = Environ("username")
ChDir "C:\Users\" & UserName & "\Dropbox\Open Machine Schedule"
ActiveWorkbook.SaveAs FileName:= _
"C:\Users\" & UserName & "\Dropbox\Open Machine Schedule\Open Machine Schedule - Current_2.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
Seems an old question. But for anyone who stumble on this post, adding $ worked for me -
& Environ$("username") &
Or you could just put VBA. in front of it. This fixes it for me.
i.e. Environ("Username") becomes VBA.Environ("Username").
Environ("UserProfile") &
This is what worked for me on 2007-2013 Excel versions.
Just found the same problem using VBA 7.1
Solution is to use
path = Environ$("Userprofile") & "\Documents\temp_excel_files\"
Environ$("Userprofile")
Will give you something like "c:\users\Johnsmith" to put into your path.
Cheers!
Andrew