xls to PDF vba code right margin not working

dcrighton

New Member
Joined
Nov 24, 2014
Messages
31
Bellow is a vba code I have used to print sheets from multiple excel files. It works great except the right margin doesn't go to 0.5. I would like the top, Left, Right margins all to be 0.5 and if needed the excel sheet data can be scaled to make the margins in the PDF. I do not want the bottom margin to be set. Any help is much appreciated.

Thank you,
Devin



Sub PrintSheetToPDF()

shName = "Weld Map "

With Application.FileDialog(3)

.AllowMultiSelect = True
.Show

If .SelectedItems.Count = 0 Then
MsgBox "No file selected!"
Else

For i = 1 To .SelectedItems.Count

Set wb = Workbooks.Open(.SelectedItems(i))
Set ws = wb.Sheets(shName)

pdfName = Left(wb.FullName, InStrRev(wb.FullName, Chr(46))) & "pdf"

With ws.PageSetup
.PrintArea = "$A$1:$W$20"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.5)
'.BottomMargin = Application.InchesToPoints(0.5)'
End With

ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=pdfName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

wb.Close False

Next i

End If

End With
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If the sheet is compressed vertically to fit on a single page, the width may not extend to the right margin.
 
Upvote 0
all the sheets pdf'd are significantly shorter in length than a full page so they are not compressed vertically, if anything I would like to change the code so that the left and right margins are the driving factors and any vertical spill over will be pdf'd on consecutive pages.
 
Upvote 0
Code:
.FitToPagesWide = 1
.FitToPagesTall = False
That will not magnify a sheet to extend to the right margin, though.
 
Upvote 0
Select Center Horizontally. Or increase the font size of style Normal to make everything wider.
 
Last edited:
Upvote 0
I tried using .HorizontalAlignment = x1Center within the PageSetup with, but it doesn't apply there if I could have it center within the page setup I think the code would do what I want. I don't know if it is possible though.

Devin
 
Upvote 0
Sorry, I can't parse that.

The constant is xlCenter (or xlHAlignCenter), not x1Center.
 
Upvote 0
I got it working I commented out the left and right margins and did what you said, .FitToPagesWide = 1 and .TopMargin = Application.InchesToPoints (0.5) and it centers with a .5 top margin. :D

Thank you for your help
Devin Crighton
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,253
Members
449,093
Latest member
Vincent Khandagale

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