Print the Document under Current Name and Folder Silently

Jakov93
NewLounger
Posts: 9
Joined: 26 Feb 2024, 21:56

Print the Document under Current Name and Folder Silently

Post by Jakov93 »

Hi,
I want to print my document under current and folder using "Application.PrintOut" method, I don't want by "ExportAsFixedFormat" because I have issue with this method.
I googled and found some codes, but I can't assembled them

Code: Select all

Sub SaveAsPDF()
Dim sPrinter As String
Dim sRange As String
    On Error GoTo lbl_Exit
    sPrinter = Application.ActivePrinter
    ActivePrinter = "Microsoft Print to PDF"
    sRange = All
    Application.PrintOut FileName:="", _
                         range:=wdPrintRangeOfPages, _
                         Item:=wdPrintDocumentWithMarkup, _
                         copies:=1, _
                         Pages:=sRange, _
                         PageType:=wdPrintAllPages, _
                         collate:=True, _
                         Background:=False, _
                         PrintToFile:=False, _
                         PrintZoomColumn:=0, _
                         PrintZoomRow:=0, _
                         PrintZoomPaperWidth:=0, _
                         PrintZoomPaperHeight:=0
lbl_Exit:
    Application.ActivePrinter = sPrinter
    Exit Sub
End Sub
Also, for current name and folder, I found

Code: Select all

Dim strDocName As String
Dim strPath As String
Dim intPos As Integer
Start:
    'Find position of extension in filename
    strDocName = ActiveDocument.Name
    strPath = ActiveDocument.path & "\"
    intPos = InStrRev(strDocName, ".")
    If intPos = 0 Then
        ActiveDocument.Save
        GoTo Start
    End If
    strDocName = Left(strDocName, intPos - 1)
    strDocName = strPath & strDocName & ".pdf"
I tried to make line such as

Code: Select all

Application.PrintOut FileName:=strDocName
But failed and ignored the file name.
Any suggestion, please
Thanks

User avatar
HansV
Administrator
Posts: 78566
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print the Document under Current Name and Folder Silently

Post by HansV »

How about

Code: Select all

Sub SaveAsPDF()
    Dim sPrinter As String
    Dim strDocName As String
    Dim strPath As String
    Dim intPos As Integer
    'Find position of extension in filename
    strDocName = ActiveDocument.Name
    strPath = ActiveDocument.Path & "\"
    intPos = InStrRev(strDocName, ".")
    strDocName = Left(strDocName, intPos - 1)
    strDocName = strPath & strDocName & ".pdf"
    On Error GoTo lbl_Exit
    sPrinter = Application.ActivePrinter
    ActivePrinter = "Microsoft Print to PDF"
    Application.DisplayAlerts = False
    ActiveDocument.PrintOut _
        OutputFileName:=strDocName, _
        PrintToFile:=True
lbl_Exit:
    Application.DisplayAlerts = True
    Application.ActivePrinter = sPrinter
    Exit Sub
End Sub
Best wishes,
Hans

Jakov93
NewLounger
Posts: 9
Joined: 26 Feb 2024, 21:56

Re: Print the Document under Current Name and Folder Silently

Post by Jakov93 »

HansV wrote:
29 Feb 2024, 12:36
How about
Thanks, HansV
It works perfectly
You are awesome
Thank you so much
Although some told me that I couldn't rename the file from VBA using the PrintOut method, but you do it :clapping: .
Thanks again

Jakov93
NewLounger
Posts: 9
Joined: 26 Feb 2024, 21:56

Re: Print the Document under Current Name and Folder Silently

Post by Jakov93 »

Hi HansV,
Your code works perfectly, but the only problem is the output file is not my document size
I always use A4 (210*297 mm), when I use ExportAsFixedFormat method, the output file is the same size as my document size,
but when I use "Application.PrintOut" method edited by you, the output file size is 215*279 mm, which is different from A4 size and causes some problems when printing on paper.
I changed the default paper size in "Microsoft Print to PDF" printer, but the problem persists, (see attached files).
https://www.upload.ee/files/16349529/Te ... d.pdf.html
https://www.upload.ee/files/16349536/Te ... d.pdf.html
Please solve this issue as you can with my special thanks.

User avatar
HansV
Administrator
Posts: 78566
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print the Document under Current Name and Folder Silently

Post by HansV »

I'm sorry, I don't know how to solve that.
Best wishes,
Hans