PDF is saved twice rather than once - excel

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.

Related

VBA code for Print to PDF (print pages 1 to 3 only)

I have the below code, and I am here to ask what I would need to add to the code to print pages 1 to 3 only?
Sub PrintVisa()
Dim invoiceRng As Range
Dim pdfile As String
'Setting range to be printed
Set invoiceRng = Range("C7:L175")
pdfile = " Seabourn_Visa_Letter"
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
Please, try the next code:
Sub ExportFirstThreeSheets()
Dim FileName As String
FileName = ThisWorkbook.Path & "\MyThreeSheets.pdf"
Sheets(Array(1, 2, 3)).copy 'it creates a new workbook containing the first three sheets
With ActiveWorkbook
.ExportAsFixedFormat xlTypePDF, FileName, , , , , , True
.Close False
End With
End Sub
The ExportAsFixedFormat has a page range built in. See below:
Sheet5.ExportAsFixedFormat Type:=xlTypePDF, Filename:="YouFileName.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=True

Excel cycle through validation list and export as Workbook

I have
-worksheet name "sheet2:
-Datavalidation list in G1
looking if there's a way to have macro that cycles through the data validation list and exports a workbook with the name of the text in that cell and loop through so every item of list exports as their own workbook . aka if there's 100 lines in the drop down, then i'd end with 100 excel files each individually named for the 100 drop down options.
Example , This worked for me to print out pdfs :
Public Sub Create_PDFs()
Dim destinationFolder As String
Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
destinationFolder = "C:\Users\DELL 04\Desktop\Q-Book Activities\Experiment" 'Same folder as workbook containing this macro
'destinationFolder = "C:\path\to\folder\" 'Or specific folder
If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
'Cell containing data validation in-cell dropdown
Set dataValidationCell = Worksheets("sheet2").Range("G1")
'Source of data validation list
Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
'Create PDF for each data validation value
For Each dvValueCell In dataValidationListSource
dataValidationCell.Value = dvValueCell.Value
With dataValidationCell.Worksheet.Range("A1:I45")
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
Next
End Sub
What if you just use the same, working code, and create workbooks instead?
For Each dvValueCell In dataValidationListSource
Workbooks.Add.SaveAs Filename:=destinationFolder & dvValueCell
Workbooks(dvValueCell & ".xlsx").Close
Next
Try replacing of:
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
with
Worksheets("sheet2").Copy 'it creates a new workbook with Sheet2 content
ActiveWorkbook.saveAs Filename:=destinationFolder & dvValueCell.Value & ".xlsx", FileFormat:=xlWorkbookDefault
ActiveWorkbook.Close False

Export range of columns to PDF page

I have the below script to export each excel sheet to a separate PDF page. It works fine, except for one small issue. I need the range Columns A:H to fit on one PDF page. How can I update the below script to make sure I export all information on one page for the specified columns?
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.Columns("A:H").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub
I would set the print area to A:H and set scale to fit to 1 page wide and 1 page high through the Page Setup part of the Ribbon. Then the following code should work without having to mess with the scaling through VBA.
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub

Save excel workbook as pdf

Hi have 50+ sheets in my excel workbook and i want to save them individual pdf files.
I also want to save them in specific folder and want to name these files from certain cell.
I got the basic of basic, saving them individually to desktop, but having trouble saving to certain folder and name part.
Sub Save_As_PDF_To_Desktop_4()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> "Instructions" And sh.Visible = True Then sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
CreateObject("WScript.Shell").specialfolders("Desktop") & "\" & sh.Name & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Next sh
End Sub
Could someone assist me on this? Thanks!

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

Resources