VBA - Print PDF, but maintain A4 print margins whatever the screen resolution of the PC.

Rinse04

New Member
Joined
May 19, 2020
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
I have the current code which selects the PDF printer of whatever PC it is used on, however depending on the screen resolution of the PC the margins will change so the printed PDF looks different depending on which PC is used.
I am looking for a way to print the same PDF on whatever PC or resolution the spreadsheet is opened on.


VBA Code:
Function FindPrinter(ByVal PrinterName As String) As String
  Dim Arr As Variant
  Dim Device As Variant
  Dim Devices As Variant
  Dim Printer As String
  Dim RegObj As Object
  Dim RegValue As String
  Const HKEY_CURRENT_USER = &H80000001
       
    Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
    
      For Each Device In Devices
        RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
        Printer = Device & " on " & Split(RegValue, ",")(1)
        If InStr(1, Printer, PrinterName, vbTextCompare) > 0 Then
           FindPrinter = Printer
           Exit Function
        End If
      Next
End Function

Sub Printallpages()
Dim filenamex As String
Dim sCurrentPrinter As String

sCurrentPrinter = Application.ActivePrinter
Application.ActivePrinter = FindPrinter("Microsoft Print to PDF")

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select


Path = CreateObject("WScript.Shell").specialfolders("Desktop")


filenamex = Path & "" & "Document"


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    filenamex, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
     IgnorePrintAreas:=False, OpenAfterPublish:=True
     
'print document, then go back to the original printer
Application.ActivePrinter = sCurrentPrinter
    
End Sub

Any help greatly appreciated.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:
VBA - Print PDF, but maintain A4 print margins whatever the screen resolution of the PC.

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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