+ Reply to Thread
Results 1 to 4 of 4

save multiple worksheets as PDF

Hybrid View

  1. #1
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    save multiple worksheets as PDF

    Hi,

    I have a code set up to save a worksheet as a pdf and open in outlook but i was hoping to adapt it to attach several work sheet within the same work book as a single PDF. The Work sheets will be the active one and the following 3 to the right. There are other worksheets at the beginning of the work book which i dont want attached. The Current code i have is

    Sub Saveaspdfandsend()
    Dim xSht As Worksheet
    Dim xFileDlg As FileDialog
    Dim xFolder As String
    Dim xYesorNo As Integer
    Dim xUsedRng As Range
    Dim xPath As String
    
    Set xSht = ActiveSheet
    xPath = Range("k14") 'here "workshet to pdf" is the destination folder to save the pdf files
       
    xFolder = xPath & "\" & xSht.Range("k17").Value
    If Len(Dir(xFolder & ".pdf")) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
    vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
    Kill xFolder & ".pdf"
    Else
            MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                        & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
            Exit Sub
        End If
        If Err.Number <> 0 Then
            MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                        & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
            Exit Sub
        End If
    End If
     
    Set xUsedRng = xSht.UsedRange
    If Application.CountA(xUsedRng.Cells) <> 0 Then
        'Save as PDF file
     
    xSht.ExportAsFixedFormat 0, xFolder
        
        
        'Create Outlook email
    With CreateObject("Outlook.Application").CreateItem(0)
            .Display
            .To = ""
            .CC = ""
            .Subject = "Invoice for Payment " & xSht.Range("k22").Value
            .htmlBody = "Hi," & "<br>" & "<br>" & "Please find attached our invoice for payment " & Range("k22").Value & "<br>" & "<br>"
            .Attachments.Add xFolder & ".pdf"
            If DisplayEmail = False Then
                '.Send
            End If
        End With
    Else
      MsgBox "The active worksheet cannot be blank"
      Exit Sub
    End If
    End Sub
    Any help would much be appreciated.

    Thanks

    Danny
    Last edited by davesexcel; 12-22-2021 at 09:46 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,357

    Re: save multiple worksheets as PDF

    Change this

    xSht.ExportAsFixedFormat 0, xFolder
    to this

        x = xSht.Index
        Sheets(Array(xSht.Name, Sheets(x + 1).Name, Sheets(x + 2).Name, Sheets(x + 3).Name)).Select False
        ActiveSheet.ExportAsFixedFormat 0, xFolder
        Sheets(x).Select
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: save multiple worksheets as PDF

    Great, that worked perfectly, thank you very much

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,357

    Re: save multiple worksheets as PDF

    You're welcome and thanks for rep+.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Macros to save multiple worksheets into individual workbook with cel value
    By hecgroups in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-13-2020, 12:21 PM
  2. Save WorkSheets to Multiple Folders based on the WorkSheet Name
    By HangMan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2015, 03:20 PM
  3. [SOLVED] Save Multiple Worksheets as Individual PDFs
    By jmk8602 in forum Excel General
    Replies: 3
    Last Post: 02-26-2014, 01:26 PM
  4. [SOLVED] Macro to save a cell value from multiple worksheets
    By naga in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2013, 01:12 PM
  5. [SOLVED] VBA to save multiple worksheets as separate CSV files using name of worksheet
    By Yg74 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2012, 10:55 AM
  6. VBA to save multiple worksheets as separate CSV files using name of worksheet
    By titushanke in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2012, 08:08 AM
  7. save multiple worksheets and name workbook
    By Christy416 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-21-2008, 03:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1