Excel VBA SaveAs "Read-only" PDF file - excel

The following code successfully saves an excel sheet as PDF. I would like to save it as a file, that can be opened by everyone, but not edited with the "fill and sign" feature, that is provided by Adobe Acrobat Reader DC. I have tried to simply add the following, but that does not work:
Attributes:=vbReadOnly
Here is the rest of the code:
Dim sPath As String
sPath = "O:\"
With Worksheets("Sheet 1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & UserForm.TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With

You should be able to use SetAttr after saving.
SetAttr "c:\path\yourfilename.pdf", vbReadOnly

Related

Saving document as a PDF with modified filename

I am trying to save my Excel file as a PDF but with custom file name.
I would only add a piece to the prompted filename from the Excel file.
According to the queries here:
Save excel as PDF in current folder using current workbook name
Save excel as PDF in current folder using current workbook name
My code looks as follows:
Sub DPPtoPDF()
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Frontsheet").Select
End Sub
From this code we know the PDF filename will be the Excel filename.
I tried something like this:
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= &fName $ "-Route-Aprooval.pdf" _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
I get an error as per in the picture below.
I can see something is up, as my code is turning red.
The code:
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
" &fName $ "-Route-Aprooval.pdf""
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
also doesn't work.
Any code behind the Filename:= destroys whole code.
I want to keep the name as in Excel, but add another part of the name after dash (per the image above).
Where should I place my new output filename?
Try the code below.
Sub DPPtoPDF()
Dim Custom_Name as string
ThisWorkbook.Sheets.Select
Custom_Name= ThisWorkbook.Name & "-route approval" & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Custom_Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Frontsheet").Select
End Sub

How to print excel file to pdf with the option ".ExportAsFixedFormat" via excel macro

I have wrote a code to print excel file to .PDF file with the page setups parameters.And also it eliminates the need of having a prompt dialog box also.
But I need to know if I need to name the .PDF file as same as the excel file name with below code but not the same destination path.As an example:= if excel file name is "Quality Report 1411185623689" This file is generated by a system therefore its name is changed everyday.
How do I solve this?
Sub Save_As_PDF()
With ActiveSheet.PageSetup
.Orientation=xlLandscape
.Zoom=16
End With
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:="C\:Desktop\Reports\Same as excel file name", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Exit Sub
Untested, but assuming you want to name the PDF the same as the Excel file (ignoring file extension), but in a different folder (say some folder/directory called "C\:Desktop\Reports\" for example):
Option explicit
Sub SaveAsPDF()
Dim folderPath as string
folderPath = "C\:Desktop\Reports\" ' Change to whatever folder, but make sure it ends with a \
If len(dir$(folderPath, vbDirectory)) = 0 then
Msgbox("'" & folderPath & "' is not a valid/existing directory. Abandoning export. Code will stop running now.")
Exit sub
End if
Dim Filename as string
Filename = left$(Thisworkbook.name, instrrev(Thisworkbook.name, ".", -1, vbbinarycompare) -1) & ".pdf"
With ActiveSheet.PageSetup
.Orientation=xlLandscape
.Zoom=16
End With
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=folderPath & filename, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Exit Sub

Template based letters generation programs

Is there any free tool to generate pdf documents from excel data
I found some with price (http://xlvba.net/excel_to_word_automation.html) for it but not free one, can you help ?
Regards,
Ragavendra
The following code will export your chosen Sheet as PDF:
FPath = C:\Users\HomeUser\Desktop"
'set path
FName = "NewFilename.pdf"
'set filename
'EXPORT AS PDF
FName = FPath & "\" & FName
Sheets("Sheet1").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Warning message if file name already exists when creating a pdf file

I use the following macro to create a PDF file from my Excel spreadsheet:
Sub PDF_01()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & "test.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
This macro works fine so far. However, once I have created the file "test.pdf" and I run the macro again it will overwrite the first file without giving any warning message such as "The filename already exists. Do you want to overwrite it?".
Do you know how I can include this message in my code?
You can use Dir to see if the file already exists and then give the user an alternative choice, i.e
Dim StrIn As String
StrIn = ThisWorkbook.Path & "\" & "test.pdf"
If Len(Dir(StrIn)) = 0 Then
ActiveSheet.ExportAsFixedFormat xlTypePDF, Filename:=StrIn, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "file already exists"
' do something else
End If

Saving worksheets with forms as pdf

I'm trying to save a worksheet as PDF and I was successful at doing this, but when I open the generated PDF file all the textboxes in the sheet are deformed.
How can I keep the ratio of the exported textboxes?
I'm using a button in the worksheet that executes the following code:
ThisWorkbook.Sheets(1).Select
Sheets(1).Activate
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="\\10.18.105.44\Setor Proteção RDs\01 - Ordens de Ajustes\Banco de ordens\AL" & alimentador & "\" & nome_arquivo & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Sheets(1).Select
Range("D2").Select
I discovered that this only occurs when i try to save with the zoom of the workbook larger than 100%, when i print with zoom at 100% it goes like a charm.

Resources