Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2021, 03:35 PM
PhilTilson PhilTilson is offline Correct syntax for printing from VBA Windows 10 Correct syntax for printing from VBA Office 2016
Novice
Correct syntax for printing from VBA
 
Join Date: Nov 2021
Posts: 4
PhilTilson is on a distinguished road
Default Correct syntax for printing from VBA

Could somebody (Graham?) please tell me where I am going wrong with the syntax in this snippet:

Code:
    curPtr$ = Application.ActivePrinter
    Application.ActivePrinter = "Microsoft Print to PDF"
    ActiveDocument.PrintOut False, False, 0, txtFilename, 0, 0, 0, 1, 0, 0, True
    Application.ActivePrinter = curPtr$
The intention is clearly to print the whole of the current document to a .pdf file, given the name in txtFilename, but I just get a 'type mismatch' error on the third line.



Phil
Reply With Quote
  #2  
Old 11-22-2021, 10:29 PM
gmayor's Avatar
gmayor gmayor is offline Correct syntax for printing from VBA Windows 10 Correct syntax for printing from VBA Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Microsoft doesn't permit the inclusion of a filename when printing to PDF.
You need instead
Code:
ub SaveAsPDF()
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"

    ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-23-2021, 03:32 AM
PhilTilson PhilTilson is offline Correct syntax for printing from VBA Windows 10 Correct syntax for printing from VBA Office 2016
Novice
Correct syntax for printing from VBA
 
Join Date: Nov 2021
Posts: 4
PhilTilson is on a distinguished road
Default

Oh, what a pain! Thank you Microsoft!

And thanks, Graham, for your explanation.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Correct syntax for printing from VBA Correct syntax for PtrSafe Function AccessibleObjectFromWindow Paul Cain Excel Programming 2 11-19-2018 04:29 AM
How do I ensure correct duplex printing? JCF Word 3 07-14-2017 06:55 AM
Correct syntax for printing from VBA VLookup Syntax ketbdnetbp Excel 2 04-12-2014 03:18 PM
Correct syntax for printing from VBA Can't seem to get the correct syntax for Wine list mailmerge daym Mail Merge 3 05-02-2011 03:26 AM
Correct syntax for printing from VBA Syntax help needed blazzercat Excel 3 07-09-2009 02:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft