How to set file destination for to a certain map? - excel

I have this code and I would like to save the file in a certain map on my pc.
How could I add a custom path, so I could change it in the future when needed?
As of now it just saves on my desktop.
Code:
ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now, "yyyymmdd") & " Testing"

This is the way:-
ThisWorkbook.SaveAs "C:\Users" & Format(Now, "yyyymmdd") & " Testing.xlsx"
You can replace "C:\Users" with any path. But please don't forget to add the file extension at the end.

Solution:
Testing = "C:\Testing\" & Format(Now(), "yyyymmdd") & " Testing " & ".xlsx"
MsgBox "File saved in: C:\Testing\" & vbNewLine & "Your_Second_Text"
ActiveWorkbook.SaveAs Filename:=Testing, FileFormat:=xlOpenXMLWorkbook

Related

How to save new created folder based name of person defined in integer

I have created folder by user name which defined in "V"
MkDir ("D:\Test\" & V)
Now I want save file in respective folder name:
ActiveWorkbook.SaveAs "D:\Test\" & V & "_" & Format(Date, "dd-mmm-yyyy") & ".xlsx", FileFormat:=51
Is this code right?
Try with a backslash included:
ActiveWorkbook.SaveAs "D:\Test" & V & "\" & Format(Date, "dd-mmm-yyyy") & ".xlsx", FileFormat:=51

I want to make a save as with file name as, time stamp(space)text in range M2(space)Design Format

I want to make a save as with file name as, time stamp(space)text in range M2(space)Design Format
example: 20200525 1747 Client Design Format
Range M2 = Client
Sub Make_saveas()
Worksheets(Array("Sheet1", "Sheet2")).Copy
With ActiveWorkbook
.SaveAs Filename:=Environ("USERPROFILE") & "\Desktop\" & Format(Now(),
"YYYYMMDD HHMM, sheets(3).range("M2"), "Design Format"")& ".xlsx",
FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
End Sub
Please help me with the above code.
Try the following...
Filename:=Environ("userprofile") & "\Desktop\" & Format(Now(),"YYYYMMDD HHMM") & " " & sheets(3).range("M2") & " Design Format.xlsx"

Variable not being consistant in excel vba macro

I have a macro in excel that if a drive exists the macro saves the file to my harddrive and thumbdrive. If it doesn't exist, it saves to the harddrive. When the macro runs I am getting an error. Here is the macro:
Sub SaveFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim filepath As String
name = "Siemens"
filepath = "F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
If fso.DriveExists("F:\") = True Then
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
'ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:=filepath
Else
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
End If
End Sub
Here is the error I am getting:
I don't know if you can see but the last part of the error message says "\Siemens\8E555720. That should also say the customer name (i.e. Siemens). In the code I have set the customer name in the variable "name". So why is it giving me this crazy error? All help is appreciated.
Something like this might be better:
Sub SaveFile()
Const PATH_C As String = "C:\Users\dgray\Documents\"
Const PATH_F As String = "F:\Dave backup\Open Orders\Label Manifests\" & _
"Active Labels Manifest\Manifest Related\File saving testing folder\"
Dim fileName As String, custName As String
custName = "Siemens"
fileName = custName & " Manifest " & Format(Now, "mm-dd-yyyy") & ".xlsx" 'or .xlsm
ActiveWorkbook.SaveAs fileName:=PATH_C & fileName 'assume C is always available
'save to F if available
If Len(Dir(PATH_F)) > 0 Then
'assumes the custName folder already exists...
ActiveWorkbook.SaveAs fileName:=PATH_F & custName & "\" & fileName
End If
End Sub
I can see the space in folder name which may cause this error.
By removing space in the foldername this error would be fixed.

Why can't Excel 365 vba save my pdf file?

I think now I tried nearly everything to save a pdf file with Excel vba.
This is my actual code:
Dim pdfFile As String
If excel2016 Then
pdfFile = Application.DefaultFilePath & "/Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
Else
pdfFile = ActiveWorkbook.Path & Application.PathSeparator & "Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
End If
wsOutputOne.ExportAsFixedFormat Type:=xlTypePDF, fileName:=pdfFile
I know that Excel 2016 is creating a "container" and saving the files under ~/Library/Containers/com.microsoft.Excel/Data/Documents/ but why does that code not work with Excel 365?
Or what has a friend with Excel 365 to do to save this generated pdf file?
Because the call of Application.DefaultFilePath returns nothing on his machine...
Can somebody help me?
Please try the following code:
Dim pdfFile As String
If excel2016 Then
pdfFile = Application.DefaultFilePath & "/Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
Else
pdfFile = Application.ActiveWorkbook.Path & Application.PathSeparator & "Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
End If
wsOutputOne.ExportAsFixedFormat Type:=xlTypePDF, fileName:=pdfFile

Attach certain files

I want to build the following:
select sheets for pdf printing - works
create folder and print sheets - works
attach those printed files to an email - doesn't work
the filename depends on cell values + Date (last Range.Value in filename), is there a way to get those pdfs attached?
I tried the following, but that doesn't work
'code ...
Dim myDir as String, mySht as String
myDir = "C:\Users\ihlin\OneDrive\Düngung\" & Worksheets("Drip_Drain_Eingabe").Range("s13").Text
mySht = Worksheets("Druckansicht_mmol").Range("c2").Text & "_" & Worksheets("Druckansicht_mmol").Range("K2").Text & "_" & Worksheets("Druckansicht_mmol").Range("P2").Text & "_" & "mmol_" & Worksheets("Druckansicht_mmol").Range("T1").Text
`code ... ...
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht & ".pdf"
End if
If CheckBox2 = True Then
.Attachments.Add myDir & "\" & mySht2 & ".pdf"
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht3 & ".pdf"
End if
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht4 & ".pdf"
End if
Publishing takes forever and ends with crashing Excel.
Any help would be appreciated.
Here is a simple debugging method. Try the following:
.Attachments.Add "c:\somehardcoded\address\ofthe\worksheet\folder\file.pdf"
If it works, then super, you simply have to find a way to represent the folder.
If it does not work, then forget it and try to attach the file in another way.

Resources