Having issues getting an export to pdf to work, for some reason it isn't working and I am unsure as to why? All I want is the document to save the activeSheet to C:\Users\Documents, using the value found is cell N6.
Easy Enough...
Or so I thought...
Code:
Sub SaveActiveWorkbookAsPDF()
' Strings Variable
Dim fName As String
Dim saveLocation As String
' The Variables
fName = Range("N6").Value
saveLocation = "C:\Users\Documents\SDS QUOTATIONS\" & fName & "\"
' Save as PDF
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"saveLocation"
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any thoughts?
Thank you for your time and help,
TCS
You'll need to remove the quotes from saveLocation...
Filename:=saveLocation
Also, it looks like you've omitted the username from your path, and you've mistakenly added a backslash (\) at the end of the string being assigned to saveLocation. It should be something like this...
saveLocation = "C:\Users\<yourusername>\Documents\SDS QUOTATIONS\" & fName
You can also use the Environ function to get the user's name...
saveLocation = "C:\Users\" & Environ("username") & "\Documents\SDS QUOTATIONS\" & fName
Related
Can you help me with this problem
I was making a macro to save the file as pdf in excel for a certain range of objects but face a runtime error
I got an error stating:
Runtime error 1004:
"Document Not Saved or maybe open or an error occurred while saving."
below is the code of it:
Sub AntigenReportSlip()
Sheets("AntigenReportSlip").Select
Dim filename As String
Dim ChDir As String
filename = Range("E9")
ChDir = "D:\New Lab Report\AntigenReports\"
Sheets("AntigenReportSlip").Range("$A$1:$U$33").ExportAsFixedFormat Type:=xlTypePDF,
filename:= _
ChDir & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
openAfterPublish:=True
End Sub
As far as I know, Excel's Range class does not have a ExportAsFixedFormat method. However, Workbook class does.
This should help you
Sub AntigenReportSlip()
' Make sure this folder already exists
Dim OutputFolder As String
OutputFolder = "D:\New Lab Report\AntigenReports\"
' Get filename from E9 cell
Dim filename As String
filename = Sheets("AntigenReportSlip").Range("E9").value
' Compute full filename.
' Note: You should always store fullpath on a variable before using it.
Dim fullFilename As String
fullFilename = OutputFolder & filename
' Select cells range you would like to export
Sheets("AntigenReportSlip").Range("$A$1:$U$33").Select
' Export selected range to PDF e present it
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, filename:=fullFilename, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, openAfterPublish:=True
End Sub
For further detailon ExportAsFixedFormat method, check https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
I have a macro that works perfectly well to copie an excel file from one directory to another and adding in the title of the date. But what I need, is to copy only one sheet of the Excel file, and has it saved as .PDF
Both path (source, new location) should stay the same as the code below.
here is my existing code :
Sub CopierFichier()
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.CopyFile("\\mtr.intra\367$\profils\R06C286A\_RD\NASC02\Desktop\test\Heures.xls", "V:\DVI\11000_Surveillance\11200_ISP\PCQ\Suivi\FeuilleDeTemps_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xls")
End Sub
EDIT:
I just tried something and It worked perfectly. There's one problem, when I run the macro, it takes an eternity to publish the pdf in the directory. Any help is appreciated
this is the code I used :
Sub Macro1()
Sheets("Grille").Activate
ActiveSheet.UsedRange.Select
ThisWorkbook.Sheets("Grille").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"V:\DVI\11000_Surveillance\11200_ISP\PCQ\Suivi\FeuilleDeTemps_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
I need to save a copy of the worksheet I'm using as a CSV file.
The save name is to be the date and the user's name. I set up variables to get this information.
I get
Run-time error '1004'; Application-defined or object-defined error
on the line
Activeworkbook.saveAs Filename
Code is:
Sub SaveCSV()
rundate = DateTime.Now
runname = Application.UserName
savename = rundate & runname
sfilename = ActiveWorkbook.Path & "\" & savename & ".csv"
ActiveWorkbook.Save
ActiveWorkbook.SaveAs Filename:=sfilename, FileFormat:=xlCSV
Application.DisplayAlerts = True
End Sub
Value of rundate (based on default regional settings) will be something similar to below:
3/12/2019 10:25:11
of which forward slash (/) & colon (:) are illegal characters as per the file name conventions in Windows.
To resolve this specific issue, use the below code to assign current-time to the rundate variable:
rundate = Format(DateTime.Now, "dd-MMM-YYYY_hh-mm-ss_")
Another good way is to use generic function because we don't always know what illegal characters are creating the trouble. Because there could be more illegal characters in the filename. Above approach is correct but it's not comprehensive list of illegal characters to remove or replace from the filename before saving it. For eg. these characters are missing from the array in your code -> : & . However it is advised to keep filename rid of other allowed special characters too.
Below, I am providing the function which returns a safe string that can be used to produce filename before saving.
Function ReplaceIllegalCharacters(strIn As String, strChar As String) As String
Dim strSpecialChars As String
Dim i As Long
strSpecialChars = "~""#%&*:<>?{|}/\[]" & Chr(10) & Chr(13)
For i = 1 To Len(strSpecialChars)
strIn = Replace(strIn , Mid$(strSpecialChars, i, 1), strChar)
Next
ReplaceIllegalCharacters = strIn
End Function
Specifically, in your code, replace the ActiveWorkbook.SaveAs line with this line:
ActiveWorkbook.SaveAs Filename:= ReplaceIllegalCharacters(sfilename, "_") & _
, FileFormat:=xlCSV, CreateBackup:=False
I recently updated to office 2016 and now my macro that i am using to select a range in excel, and then convert this range to PDF and automatically send an email, does not fully work.
Before when i used this macro, the filename was automatically filled in the SaveAs dialog box, but now it is empty. I do not understand why.
Does anyone else has a problem like this or know how to fix it?
Here is my code:
Function Skicka_projektunderlag_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws = Sheets("Partner_information")
Set ws1 = Sheets("Kundinformation")
Set ws2 = Sheets("Kalkyl")
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
FileFormatstr = "PDF Files (*.pdf), *.pdf"
Fname = Application.GetSaveAsFilename(ws.Range("B1").Value & " - Projektunderlag " & ws2.Range("BF104").Value & " " & ws1.Range("B3").Value _
, FileFilter:=FileFormatstr, Title:="Create PDF")
If Fname = False Then Exit Function
Else
Fname = FixedFilePathName
End If
If OverwriteIfFileExist = False Then
If Dir(Fname) <> "" Then Exit Function
End If
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
If Dir(Fname) <> "" Then Skicka_projektunderlag_PDF = Fname
End If
End Function
Best regards
AgatonSaxx
The following answer isn't refined but I have also been struggling with this problem in Word 2016 VBA to generate a default file name when Save As is selected in Word 2016
and wanted to share what I've found thus far as it is working with some success.
I was able to get the code semi-working again by adding an event handler.
Application.DocumentBeforeSave Event
example here https://msdn.microsoft.com/en-us/library/office/ff838299.aspx
tied to Using Events with Application Object
example here https://msdn.microsoft.com/en-us/library/office/ff821218.aspx
I moved my actual code to within the class module
Cancel=true
had to be added to the end of the code or the Save As dialog box would open twice.
This "solution" has some drawbacks that it only works once per document. So, if for some reason, you want to use SaveAs on the same document more than once, the name won't default. It also seems a bit clunky/limited for my taste but it is a start.
This "solution" is Word based but you should be able to do/ find something similar for Excel.
Hope this helps put you on the path to success. Apologies for not being a perfect answer. Just wanted to share lessons learned as maybe it will cut down on your time to a solution!
I am trying to export excel files to PDF using VBA. The exporting function works fine, but I am unable to successfully add dots/periods to the filename.
When I use the SaveAs function for the excel spreadsheet, the filename saves correctly. For example:
"(M.003) Bill Johnson.xlsx"
But when I try to export the file to pdf, the filename excludes the portion of "path_id" that comes after the "."; for example:"(M Bill Johnson.pdf"
I'm using Excel 2011 on my Macbook Pro running 10.9.5.
Here's the relevant section of the code:
'Save file
Dim sheet_name As String
Dim path_id As String
sheet_name = Application.Index(table_mlm.ListColumns("Full Name").DataBodyRange, _
Application.Match(consultant_id, table_mlm.ListColumns("Consultant ID").DataBodyRange, 0))
consultant_id = "M.001"
path_id = folder & "September:" & "(" & consultant_id & ")"
wb_report.Sheets(1).Name = sheet_name
wb_report.SaveAs path_id & " " & sheet_name & ".xlsx"
wb_report.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=path_id & sheet_name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False,
OpenAfterPublish:=False
Thanks!
Could you try using the CHR method and calling up the appropriate ASCII value for a "dot". Try this:
consultant_id = "M" & Chr(46) & "001"