Print PDF VBA

agrawaltanu21

New Member
Joined
Aug 15, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I have made VBA code to print PDF any excel sheet. But i am running this code, i am getting one extra page that is blank, can you please help to rectify this problem.
Sub Macro1()
'

Dim lRow As Long
Dim lCol As Long
Dim sht As Worksheet
Dim StartCell As Range

'
Sheets("Sheet1").Select
Worksheets("Sheet1").UsedRange

On Error Resume Next
lRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0

MsgBox "Last Row: " & lRow


On Error Resume Next
lCol = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0

'MsgBox "Last Column: " & lCol


Set StartCell = Range("A1")

'MsgBox "Last Row: " & lRow
'ActiveSheet.Range("A1:F8").Select

ActiveSheet.Range("A1", ActiveSheet.Cells(lRow, lCol)).Select

ActiveSheet.PageSetup.PrintArea = Range("A1", Cells(lRow, lCol)).Address
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
'ActiveSheet.PageSetup.PrintArea = "$A$1:$AN$100"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 90
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\\Downloads\credits\Automation\Formula.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
 
I am running the macro and it works fine for me.
What is the full range that you are printing ?
From what cell to what cell ? I will try and replicate the same range.

If I still can cause it to happen then, I won't be able to do it without a copy of your workbook.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I am running the macro and it works fine for me.
What is the full range that you are printing ?
From what cell to what cell ? I will try and replicate the same range.

If I still can cause it to happen then, I won't be able to do it without a copy of your workbook.
Try Large Range ( A1:BZ600), fill random data in the cells and try to print.
 
Upvote 0
I think it much to wide to for it to make sense outputting it to a pdf but on my Excel is still only produces a single page.

1629193286384.png


1629193128389.png


1629193192260.png
 
Upvote 0
Can you desensitize your workbook and put it as public in Dropbox or onedrive or Googledrive ?
 
Upvote 0
I am a bit surprised you haven't cut down your code to just the essential code I showed you previously.
However, it is not the cause of your additional page and on my machine it is still only producing 1 page.

A workaround might be to add a from and to page on your export to pdf function as per the below:-
(It would be interesting if you could run your version on a colleagues machine and see if it has the same issue because it looks like it might be specific to your machine)

Restricting the output to one page
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\ADMIN\Downloads\Formula.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True, from:=1, to:=1
 
Upvote 0
I am a bit surprised you haven't cut down your code to just the essential code I showed you previously.
However, it is not the cause of your additional page and on my machine it is still only producing 1 page.

A workaround might be to add a from and to page on your export to pdf function as per the below:-
(It would be interesting if you could run your version on a colleagues machine and see if it has the same issue because it looks like it might be specific to your machine)

Restricting the output to one page
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\ADMIN\Downloads\Formula.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True, from:=1, to:=1
i have cut down my code in the main file. the file above i have shared its woking fine with me means no blank pages, but when i run other files, its give me extra blank pages. I am trying to share that file with you so u can have better idea why blank page is coming
I have used from:=1, to:=1 but its give me only one page.
 
Upvote 0
I got the solution. The file i was Printing, cells having some notes and comments due to that extra blank page was showing now it is resolved. Thanks anyway
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top