My old answer is unnecessary. Maybe I misunderstood your question. ExportAsFixedFormat has "To" & "From" arguments already. So, there is no point in using a custom code to create the same thing.
New Answer:
I am trying to add a button where the button prompts user to enter a page number FROM AND TO and save that specific page to pdf.
All you needed was just a way to ask for the user's input, I guess. In this case, use this code:
Sub AskForPages()
Dim PageFromStr As String, PageToStr As String, ExportFullName As String
ExportFullName = ThisWorkbook.Path & "\Test.pdf"
PageFromStr = InputBox("Insert the number of the first page to export.")
'Validate the input to be a positive number.
If IsNumeric(PageFromStr) Then
If PageFromStr < 1 Then Beep: Exit Sub
Else
Beep
Exit Sub
End If
PageToStr = InputBox("Inster the number of the last page to export.")
'Validate the input to be a number greater than the "From".
If IsNumeric(PageToStr) Then
If PageToStr < PageFromStr Then Beep: Exit Sub
Else
Beep
Exit Sub
End If
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ExportFullName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
From:=PageFromStr, To:=PageToStr, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
The old answer:
You can use this sub (passing the page numbers to print (From & To)) to do that:
Sub PrintPages(FromPageNum As Long, ToPageNum As Long, ExportFullName as string)
Dim Rng As Range, i As Long
If FromPageNum > ToPageNum Then 'If TO and FROM are mixed, fix them
i = FromPageNum
FromPageNum = ToPageNum
ToPageNum = i
End If
Set Rng = GetPageArea(FromPageNum)
For i = FromPageNum + 1 To ToPageNum
Set Rng = Union(Rng, GetPageArea(i))
Next
Debug.Print Rng.Address
Rng.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ExportFullName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
It uses the function GetPageArea
which returns the range of the selected page number.
And it needs RndUp
function to work, which just rounds up a number.
Function GetPageArea(PageNum As Long, Optional Sh As Worksheet) As Range
'By Abdallah Ali El-Yaddak
Dim VBreakMax As Long, HBreakMax As Long, HBreak As Long, VBreak As Long
Dim c1 As Long, r1 As Long, c2 As Long, r2 As Long
If Sh Is Nothing Then Set Sh = ActiveSheet
With Sh
VBreakMax = .VPageBreaks.Count
HBreakMax = .HPageBreaks.Count
If PageNum > (VBreakMax + 1) * (HBreakMax + 1) Then
Set GetPageArea = Nothing 'Too high page number!
Else
If VBreakMax = 0 And HBreakMax = 0 Then
Set GetPageArea = .UsedRange 'Only one page
Else
VBreak = RndUp(PageNum / (HBreakMax + 1))
HBreak = PageNum - ((VBreak - 1) * (HBreakMax + 1))
If HBreak = 0 Then
HBreak = HBreakMax + 1
r2 = .UsedRange.Rows.Count
VBreak = VBreak - 1
Else
r2 = .HPageBreaks(HBreak).Location.Row - 1
End If
If VBreak > VBreakMax Then
c2 = .UsedRange.Columns.Count
Else
c2 = .VPageBreaks(VBreak).Location.Column - 1
End If
VBreak = VBreak - 1
HBreak = HBreak - 1
If VBreak = 0 Then
c1 = 1
Else
c1 = .VPageBreaks(VBreak).Location.Column
End If
If HBreak = 0 Then
r1 = 1
Else
r1 = .HPageBreaks(HBreak).Location.Row
End If
Set GetPageArea = .Range(.Cells(r1, c1), .Cells(r2, c2))
End If
End If
End With
End Function
Function RndUp(Amount As Double, Optional digits As Integer = 0) As Double
RndUp = Round((Amount + (5 / (10 ^ (digits + 1)))) * (10 ^ digits)) / (10 ^ digits)
End Function
To test, you can try this:
Sub Test()
PrintPages 3, 5, ThisWorkbook.Path & "\Test.pdf"
End Sub