• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Selecting multiple sheets in vba

spartapsing

New Member
Hi, i'm stuck with code and it is not working. I'm trying to select multiple sheets to create PDF and for somehow it is not going thru.
I'll share my code that i use thus only worked for active sheet. ( ex. Sheet1 ) now i need to select Sheet 2 along to create one pdf file. After creating the pdf of multiple sheets i need to e-mail it.
This works with only 1 active sheet, bet need to add another also in the PDF.
Can someone help me out

>>> use code - tags <<<
Code:
Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Dim strbody As String

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.name & "_" + Format(Range("G3").Value, "dd-mm-yyyy") & ".pdf"
                         
If Len(Dir(xFolder)) > 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
    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.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then

                            
xSht.ExportAsFixedFormat Type:=xlTypePDF, filename:=xFolder, Quality:=xlQualityStandard
   
                       
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
 
                                        
            'strbody = "<FONT SIZE = 3>Good Afternoon,<p>XXXXXX"
           
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = ""
        .Attachments.Add xFolder
        .HTMLbody = strbody & .HTMLbody & "</body>"
     
                            '.Send
       
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub
 

Attachments

  • 1647339509019.png
    1647339509019.png
    80 KB · Views: 1
Last edited by a moderator:
Back
Top