ExportAsFixedFormat PDF with dots - excel

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"

Related

Excel vba code to save as pdf no longer works in excel 365 (windows 10)

Excel VBA code that worked previously on windows 7 (Excel 2013) no longer works on windows 10 (excel 365).
Now a run time error is generated.
Run-time error '-2147024773 (8007007b)'
Document not saved
The debugger highlights the 4 rows beginning with the first ActiveSheet.ExportAsFixedFormat all the way to False.
Is there a way to alter my code so this runs on windows 10 / excel 365?
Workbooks("Valuation Grail.xlsm").Save
Dim mydir As String
Dim mydrive As String
'Used activeworkbook.path instead of CurDir() because activeworkbook.path does not change for the same saved workbook
mydir = ActiveWorkbook.Path
mydrive = Left(mydir, 1)
'save original wmu as 2 PDFs
Dim month_end As String
Dim generic_vg As String
Dim archived_vg As String
Dim taa_packet As String
'creates saving format for archived pdf
month_end = Format(WorksheetFunction.EoMonth(Now(), -1), "yyyymmdd")
generic_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\Valuations Report.pdf"
archived_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\" & month_end & "-Valuations.pdf"
taa_packet = mydrive & ":\01\spec_folder2\#Packet Assembly\TAA\" & "12 - Valuation Grail.pdf"
'Saves the generic and archived version of valuation report to spce_folder data folder
ThisWorkbook.Sheets(Array("Table", "Table_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
generic_vg, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
archived_vg, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ThisWorkbook.Sheets("Summary").Select
'saves the pe tabs to the oshea packet assembly folder
ThisWorkbook.Sheets(Array("PE_Summary", "TAA_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
taa_packet, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ThisWorkbook.Sheets("Summary").Select
ActiveWorkbokk.Path previously returned the full path where the workbook was located including the letter the drive was mapped to (Z, Y, X, etc.). I would then use mydrive to pull only that letter to use in my file path. This method no longer works.
I changed mydrive = "Z" and the code now works.
Ideally I'd like to find a new way to pull that drive letter so members of our team don't have to manually change the mydrive variable each time but this works for now.

VBA Script to Create PDF from Excel Goes to Printer and Doesn't Export File

I have the following code which worked well on a Windows machine but since I moved to a MacBook, it is causing me problems:
With ActiveSheet
Fname = "SalarySlip-" + employeeName + "-" + month + ".pdf"
.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
End With
Rather than exporting as a PDF file (it worked like that on Windows), a request is sent to the printer to print the file. What am I missing here?
I am using MacOS Catalina 10.15.4 running MS Excel for MAC version 16.40.
If one cell value you're using as a variable includes slashes (for example the date in month as 2020/01/01), the naming process of the PDF file fails and it sends it out to the printer. I had that for several desperate hours...
And try
ChDir "/" & ActiveWorkbook.Path
as first line in the Sub and
fileName:= _
"/" & salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _
as filename. The "/" at the beginning made it working for me.

ExportAsFixedFormat VBA Error, unsure as to why

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

Save Excel Sheet as PDF in another directory

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

How do I create a macro in excel that will save the current document to a specific location

I am creating a template in excel 2010 and I want to make a macro that will save the current document to a folder with the name of a specific cell.
How would I do this?
Say cell D8 in Sheet4 contains:
C:\TestFolder
The try something like:
Sub SaveMe()
s1 = Sheets("Sheet4").Range("D8").Value
ActiveWorkbook.SaveAs s1 & "\" & ActiveWorkbook.Name
End Sub
EDIT#1:
Based on your comments the modified code should be:
Sub SaveMe()
s1 = Sheets("Sheet4").Range("D8").Value
patth = "P:\2013\processed\" & s1 & "\" & ActiveWorkbook.Name
ActiveWorkbook.SaveAs patth
End Sub
Just go to the developer tab and record a macro. Then do a save as to whatever location you want... Then stop recording the macro and view the code. should look like this.
x = the cell you want
ChDir "path"
ActiveWorkbook.SaveAs Filename:= _
"path" & x ".xls", _
FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Resources