VBA Save to sub folder - excel

I have a code that saves a PDF in the same folder as my workbook. I have a sub folder called PDF Files within the folder and I would like to save there. I can't seem to get it to work. Any help would be appreciated. Below is the code I have so far.
FileName = ThisWorkbook.Path & "\" & .Range("F" & CustRow).Value & "_" & _
.Range("K" & CustRow).Value & ".pdf"

Related

How to set file destination for to a certain map?

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

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.

VBA Saving PDF issue

I need to save around 300 pdf files but every 20-30 files. I have to press "Cancel" for the file to continue saving or else the macro does not keep running. This image will explain better my situation :
'Saving PDF FILE
Application.DisplayAlerts = False
Workbooks(Nom_Fichier_Model_MMS_ON).ExportAsFixedFormat xlTypePDF, _
Filename:=Path_CRM & site_oper & "_" & "4" & num_Canal_Dist & "_" & anneeFisc & ".pdf"
site_oper = Application.WorksheetFunction.Rept("0", 10 - Len(site_oper)) & site_oper
Workbooks(Nom_Fichier_Model_MMS_ON).ExportAsFixedFormat xlTypePDF, _
Filename:=Path_Sharepoint & site_oper & "_" & num_Client & "_MarginReport_312_" & anneeFisc & ".pdf"

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