Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Print out sheets to pdf and save them to specific file

0

I am trying to write a macro for a collegue and i have hit a wall.

I need the macro to print the last 8 pages in a 9 page workbook to a pdf file and i am struglling with one little bit of code.

Sub PrintDocuments()

Dim WS As Worksheet
Dim Relative As String
Dim FName As String
Dim File As String

For Each WS In ThisWorkbook.Worksheets

RelativePath = ThisWorkbook.Path & "/" & WS.Name & "/" & WS.Name
FName = RelativePath

ActiveWorksheets.ExportAsFixedFormat , Type:=xlTypePDF, Filename:=FName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
       
Next

Exit Sub
 
End Sub

Answer
Discuss

Discussion

Are you writing this for use on a Mac? Why the 'Exit Sub' before the 'End Sub'?
Variatus (rep: 4889) Aug 23, '17 at 5:04 am
Appologies that is just me making an assumption based on a video I watched. I am operating a windows PC.
Dewbo1234 Aug 23, '17 at 5:08 am
Add to Discussion

Answers

0

The reason I asked about the Mac was because you used the slash as a path separator in your question. In Windows it's a backslash. :-)

Try this code. The target path might be specified differently, depending upon your requirement. Please read the comments in the code.

Sub PrintToPDF()
    ' 24 Aug 2017
    Dim Wb As Workbook
    Dim SheetsToPrint As String
    Dim OutputFileName As String
    Dim Tmp() As String
    Dim i As Integer
    
    Set Wb = ActiveWorkbook                 ' specify the workbook
    Tmp = Split(Wb.FullName, ".")           ' separate the extension in the file name
    Tmp(UBound(Tmp)) = "PDF"                ' replace the extension
    OutputFileName = Join(Tmp, ".")         ' the output will be in the same directory
                                            ' as the workbook
    
    On Error GoTo ErrExit
    For i = 2 To 9                          ' omit the first worksheet
        If Len(SheetsToPrint) Then SheetsToPrint = SheetsToPrint & "|"
        ' an error will occur if there are less than 9
        ' worksheets in the active workbook
        SheetsToPrint = SheetsToPrint & Wb.Worksheets(i).Name
    Next i
    
    ' select the worksheets to be printed:
    Wb.Sheets(Split(SheetsToPrint, "|")).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                    Filename:=OutputFileName, _
                                    Quality:=xlQualityStandard, _
                                    IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=True
ErrExit:
    MsgBox Split("The PDF file was created successfully.|The PDF file couldn't be created.", _
           "|")(Abs(Sgn(Err)))
End Sub
Discuss

Discussion

Hi
Thanks.
Generally works great but this combines the sheets into one pdf document.
I would need them as seperate documents and the thing i had most dificulty with was pushing the PDF for each sheet into there specific subfolder which carries the same name as each worksheet in the workbook.
Dewbo1234 Aug 24, '17 at 3:24 am
You wrote, "I need the macro to print the last 8 pages in a 9 page workbook to a pdf file". That is what the above code does. I suggest we make a deal. Since you tested the code and found it working, accept the answer so that I get some points and other visitors find an answer if they ahve the same question as you did. Next, ask another question explaining exactly what you want but this time in the question itself (for other viewers). I shall then modify the code in such a way that it will do what you want. It will be quite different because your task is far from being the same. Just make sure you explain the task precisely.
Variatus (rep: 4889) Aug 24, '17 at 5:59 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login