Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-13-2021, 03:41 AM
RobertDany RobertDany is offline Save pages  range as PDF by input box Windows 7 64bit Save pages  range as PDF by input box Office 2013
Novice
Save pages  range as PDF by input box
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default Save pages range as PDF by input box

Hello everyone,



This VBA code export the range of pages in pdf format, but you need to insert the range manually in the VBA editor window for each run of the code which may be annoying

Code:
Sub SavePageRangeAsPDF()

    ActiveDocument.ExportAsFixedFormat _
     OutputFileName:=CurrentFolder & "Robert" & ".pdf", _
     ExportFormat:=wdExportFormatPDF, _
range:=wdExportFromTo, From:=2, To:=4

End Sub
According to the previous code, how to design an input box which asks me to enter the range that I want to export such as: 5-8 or 5,9,12 or 1-7,12.

Thanks in advance
Reply With Quote
  #2  
Old 08-13-2021, 09:26 PM
gmayor's Avatar
gmayor gmayor is offline Save pages  range as PDF by input box Windows 10 Save pages  range as PDF by input box 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

You cannot use non-contiguous ranges with ExportAsFixedFormat you would need to use the PDF printer driver to achieve that. e.g.


Code:
Sub PrintAsPDF()
'Graham Mayor - https://www.gmayor.com - Last updated - 14 Aug 2021
Dim sPrinter As String
Dim sRange As String
    On Error GoTo lbl_Exit
    sPrinter = Application.ActivePrinter
    ActivePrinter = "Microsoft Print to PDF"
    sRange = InputBox("Enter the range of pages to be printed eg 1" & vbCr & _
                      "or 1-3" & vbCr & _
                      "or 1-3,6", "Print Pages")
    If sRange = "" Then
        MsgBox "No pages selected!", vbCritical, "Print Pages"
        GoTo lbl_Exit
    End If
    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
__________________
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 08-13-2021, 10:56 PM
RobertDany RobertDany is offline Save pages  range as PDF by input box Windows 7 64bit Save pages  range as PDF by input box Office 2013
Novice
Save pages  range as PDF by input box
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You cannot use non-contiguous ranges with ExportAsFixedFormat you would need to use the PDF printer driver to achieve that.
Thank you Mr. gmayor for your comment and assistance

Dear gmayor,
I tried to edit your code, in order to save the file automatically under the current folder and name by using the following vars, but I failed (compile error syntax)

Code:
Dim CurrentFolder As String
Dim FileName As String

  myPath = ActiveDocument.FullName
  CurrentFolder = ActiveDocument.Path & "\"
  FileName = Mid(myPath, InStrRev(myPath, "\") + 1, _
   InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1)
......

 Application.PrintOut FileName:="FileName",
Additionally, I edited my code according to your input box for continuous export but also failed (Invalid procedure or argument):

Code:
Sub SavePageRangeAsPDFffffff()

Dim sRange As String

sRange = InputBox("Enter the range of pages to be printed eg 1-3" & "")
    If sRange = "" Then
        MsgBox "No pages selected!", vbCritical, "Print Pages"
    End If
    
    ActiveDocument.ExportAsFixedFormat _
     OutputFileName:=CurrentFolder & "Robert" & ".pdf", _
     ExportFormat:=wdExportFormatPDF, _
     range:=sRange

End Sub
So I will be so appreciative for solving these two problems
Thank you again
Reply With Quote
  #4  
Old 08-13-2021, 11:14 PM
gmayor's Avatar
gmayor gmayor is offline Save pages  range as PDF by input box Windows 10 Save pages  range as PDF by input box 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

You can't make up your own syntax. The print to PDF does not support file naming from VBA.
You can save a contiguous range using ExportAsFixedFormat (For a single page enter e.g. 2-2 in the input box) e.g.
Code:
Sub SavePageRangeAsPDF()
Const sPath As String = "C:\Path\"
Dim sRange As String

    sRange = InputBox("Enter the range of pages to be printed eg 1-3")
    If sRange = "" Then
        MsgBox "No pages selected!", vbCritical, "Print Pages"
    End If

    ActiveDocument.ExportAsFixedFormat _
            OutputFileName:=sPath & "Robert.pdf", _
            ExportFormat:=wdExportFormatPDF, _
            Range:=wdExportFromTo, From:=Split(sRange, "-")(0), To:=Split(sRange, "-")(1)
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
  #5  
Old 08-14-2021, 01:25 AM
RobertDany RobertDany is offline Save pages  range as PDF by input box Windows 7 64bit Save pages  range as PDF by input box Office 2013
Novice
Save pages  range as PDF by input box
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You can save a contiguous range using ExportAsFixedFormat (For a single page enter e.g. 2-2 in the input box)
Wow, it's amazing
Thank you, thank you, thank you Mr. gmayor
I can't thank you enough.

Quote:
Originally Posted by gmayor View Post
You can't make up your own syntax. The print to PDF does not support file naming from VBA.
Ahhh, bad news, but it's a piece of new information for me in VBA.

Thank you again
Best Regards
Reply With Quote
Reply

Tags
word vba, word vba code, word vba macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Save File with specific name from fields input. PM1 Word 17 10-04-2020 07:22 PM
Drag and Drop into an input range results in #REF! error DJ0691 Excel 4 02-20-2020 10:16 AM
Save pages  range as PDF by input box Auto generate X pages from input lipatin Word VBA 12 06-01-2018 12:13 AM
Automatically copy/paste a form field onto new pages, with user input included Pindar Word VBA 3 01-13-2017 11:49 AM
Save pages  range as PDF by input box Save a range of pages as PDF gn4619 Word VBA 9 10-27-2015 02:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:53 AM.


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