I want to create a PDF file with excel data. I have rows of data in excel, for each row I want to create a PDF file and save it to my machine. How to do it with excel or access macro in vba?
You may use File / Export / Create PDF/XPS Document menu for this purpose.
When this feature is used when recording a VBA macro, below code is generated, you may use it or get your own code by recording a macro. Then you may use the code within a loop to create multiple PDFs with one go.
This feature creates the PDF using the printable area of your sheet. So you may have to adjust the print area from your code before each PDF creation.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"\\test.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
This will do what you want.
Sub Save_Each_Row_As_PDF()
myrange = "A1:J10"
lastRow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
For myRow = 2 To lastRow
Range("A" & myRow & ":J" & myRow).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\your_path_here\Book" & myRow & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Next
End Sub
Related
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
I have a macro that creates a report which creates a table and a graph on sheet2. I then use the Excel camera function to paste an image of the combination table/graph on sheet1 below another range. (I do this because there are times the report image should not be visible.) The only help I can find is to print a range or an entire sheet. What I have tried is the code below. But I know that ExportAsFixedFormat is not a method for a shape or object. nor kind I find a method for printing a shape/object. What I have tried - and does not obviously work - is this code. I may e able to do some gymnastics with getting a range, but being able to print the image would be a whole lot easier. The image is named "Report". Any help is appreciated.
Set pic = ActiveSheet.Shapes.Range(Array("Report"))
strfile = " Projections" & "_" & Format(Now(), "yyyymmdd") & ".pdf"
pic.Select
pic.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strfile, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=Tru
After sleeping on it, I wondered if I could find the range occupied by the image. And I found this thread that answered that.
vba excel get picture's bottom row
The code that works is as follows"
Dim picRange As Range
Dim pdFile As String
Set picRange = Range(ActiveSheet.Shapes("Report").TopLeftCell, ActiveSheet.Shapes("Report").BottomRightCell)
strFile = "Dividend Projects as of " & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
pdFile = ThisWorkbook.Path & strFile
picRange.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
I want to save my xls document as a PDF.
Some documents include the "Readme" sheet, some don't.
In this event, I set the condition leading to mandatory saving despite of the "Readme" sheet presence.
I found some hint here: Test or check if sheet exists
Sub sheet_exist()
Dim I As Long
For I = 1 To Worksheets.Count
If Worksheets(I).Name = "Readme" Then
Sheets("Readme").Visible = False
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Readme").Visible = True
Else
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Next I
End Sub
My PDF document is saved twice. How can I eliminate the second save?
Your for/next loop is currently structured in a way that it will save your sheet with every iteration (it will first check if the current sheet is "Readme" and then save the the workbook - for every sheet in the workbook). So if you have two sheets in your workbook, it will save your workbook twice.
You need to restructure your code, so it first checks for the existence of "Readme" and then saves the workbook once.
Sub sheet_exist()
Dim I As Long
Dim bReadmeExists As Boolean
'First, we loop through all sheets and check if "Readme" exists. If it does, we hide the sheet.
For I = 1 To Worksheets.Count
If Worksheets(I).Name = "Readme" Then
Sheets("Readme").Visible = False
'If readme exists, we need to make it visible again in the end
bReadmeExists = True
End If
Next I
'Now we export the workbook once
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
'Finally, we make the readme sheet visible again
If bReadmeExists Then
Sheets("Readme").Visible = True
End If
End Sub
I have also added a boolean variable to "remember" if the readme sheets exists (so we eventually can make it visible again after the export).
I also allowed my self to indent the code correctly, so it is easier to read.
I'm trying to export a sheet to PDF, but when I run the code I keep getting a 1004 error.
I believe that it is linked to the folder path to the save destination. I've tried defining the file path in different ways but still get the error.
This code is supposed to pull a part of the file name from cell B1 and the file path from the location of the worksheet. The PDF is supposed to be saved to the location of the worksheet. I'm fairly new to VBA. Any help is appreciated!
Here is the code
Sub ExportAsPDFTest()
Dim Name As String
Dim Preface As String
Name = Cells(1, "B").Value
Preface = "PreR Summer 2019 - "
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=ActiveWorkbook.Path & Preface & Name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
End Sub
You're missing a backslash \ (or / if you're on Mac) after ActiveWorkbook.Path. You can use Application.PathSeparator so it will work on both:
Sub ExportAsPDFTest()
Dim Name As String
Dim Preface As String
Name = Cells(1, "B").Value
Preface = "PreR Summer 2019 - "
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & Application.PathSeparator & Preface & Name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
End Sub
Hope this helps.
Please add msgbox Activeworkbook.path to check your save path, you need add one more "\"
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.