VBA Code to convert multiple Sheets into one PDF

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi

I am new to VBA and managed to cobble together code from tutorials to convert active sheet to PDF and place whole workbook into new folder but I am struggling writing code to convert multiple sheets from workbook into one pdf and place whole workbook into new folder. Below is the coding but it either tells me i am missing an Else If or jumps to "The active worksheet cannot be blank" which it isn't. I would appreciate some guidance where I am going wrong, think it is in relation to the Array section and me not writing it correctly.

Here Goes

Code: Select all

Private Sub CommandButton1_Click()
Dim xWbk As Workbook
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
 
Set xSht = ActiveSheet
Set xWbk = ActiveWorkbook
xFolder = "F:\M Hub\M Workbook\Im Import PDFs" + "\" + xSht.Name + "-" + Worksheets("Packaging").Range("Packaging!F2") + "-" + Worksheets("Packaging").Range("Packaging!C3").Text + ".pdf"
 
'Check if file already exist
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 xSht = ActiveSheet 
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
Sheets(Array(“2”’,”3”,”5”)).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder & “\Sales”’ _
Quality:=xlQualityStandard
     
    
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If

MsgBox "PDF’s have been successfully exported."

xFolder = "F:\ Hub\M Workbook\Completions To Diary" + "\" + Worksheets("Packaging").Range("Packaging!F2") + "-" + Worksheets("Packaging").Range("Packaging!C3").Text + ".xlsm"
 
'Check if file already exist
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 file, 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
Dim sExcelFile As String
xFolder = "F:\M Hub\M Workbook\Completions To Diary" + "\" + Worksheets("Packaging").Range("Packaging!F2") + "-" + Worksheets("Packaging").Range("Packaging!C3").Text + ".xlsm"
sExcelFile = Application.GetSaveAsFilename(xFolder, "Excel Macro-Enabled workbook(*.xlsm), *.xlsm")
    If xFolder = "False" Then
        Exit Sub
    End If
    ActiveWorkbook.SaveAs sExcelFile

End Sub
Kind regards

Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

1) Do you want to export the 2nd, 3rd and 5th worksheet, or worksheets actually named 2, 3 and 5?

2) You have a line

Code: Select all

xFolder = "F:\ Hub\M Workbook\Completions To Diary" + "\" + Worksheets("Packaging").Range("Packaging!F2") + "-" + Worksheets("Packaging").Range("Packaging!C3").Text + ".xlsm"
Shouldn't that be

Code: Select all

xFolder = "F:\M Hub\M Workbook\Completions To Diary" + "\" + Worksheets("Packaging").Range("F2") + "-" + Worksheets("Packaging").Range("C3").Text + ".xlsm"
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans

Yes I would like to export the 2nd,3rd and 5th Worksheet. The second point you have raised relates to how it saves, the Range references picked up from Sheet1(Packaging!F2), I tried it your suggested way first of all but couldn't get it to pick up correct references, therefore tried this element which was working ok for a single PDF and saving the workbook to a specified destination with reference to name and ac no ranges as defined.

I will try the shortened code again in morning to see if it saves correctly

Kind regards

Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

Try this version of the code:

Code: Select all

Private Sub CommandButton1_Click()
    Dim xSht As Worksheet
    Dim xFolder As String
    Dim xFile As String
    Dim xYesorNo As Integer
    Dim xUsedRng As Range

    Set xSht = ActiveSheet
    xFolder = "F:\M Hub\M Workbook\Im Import PDFs\"
    xFile = xFolder & xSht.Name & "-" & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".pdf"

    'Check if file already exist
    If Len(Dir(xFile)) > 0 Then
        xYesorNo = MsgBox(xFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
            vbYesNo + vbQuestion, "File Exists")
        On Error Resume Next
        If xYesorNo = vbYes Then
            Kill xFile
        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
        'Save as PDF file
        Sheets(Array(2, 3, 5)).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFile, Quality:=xlQualityStandard
    Else
        MsgBox "The active worksheet cannot be blank"
        Exit Sub
    End If

    MsgBox "PDFs have been successfully exported."

    xFolder = "F:\M Hub\M Workbook\Completions To Diary\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"

    'Check if file already exist
    If Len(Dir(xFile)) > 0 Then
        xYesorNo = MsgBox(xFile & " 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 file, 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
    xFile = Application.GetSaveAsFilename(xFile, "Excel Macro-Enabled workbook(*.xlsm), *.xlsm")
    If xFile = "False" Then
        Exit Sub
    End If
    ActiveWorkbook.SaveAs xFile, xlOpenXMLWorkbookMacroEnabled
End Sub
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Thanks Hans

It works perfectly, you're a star.

One last thing, I have created a MsgBox as welcome message for user but I only want it to pop up once when the blank sheet is initially opened.

It welcomes the user and prompts them to create a customer name copy straight away, thereby keeping the master sheet clean.

Is there a code to prevent the MsgBox from repeating each time the customer named workbook is opened?

Kind regards

Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

Does the initial workbook have a fixed name?
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans

No I have not named it.

Thanks Donna

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans

Sorry being dim, it’s called Packaging Progress Form

Cheers Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

You could add the following line at the beginning of the code, before the message box is displayed:

Code: Select all

    If ActiveWorkbook.Name = "Packaging Progress Form.xlsm" Then Exit Sub
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Thank you, will give it a try on Monday:-)

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans

Finally got it to work:-) Slight variation on above

Code: Select all

Sub auto_open()
Dim UserName As String
    'Use the Application Object to get the Username
UserName = Application.UserName

If ActiveWorkbook.Name = "Mortgage Packaging Progress Form.xlsm" Then Exit Sub


MsgBox "Welcome" & " " & UserName _
+ vbNewLine + vbNewLine + "Please enter Lead Applicant Name and DOB, then select Packaging WIP tab before inputting any further information."
End Sub
Thank you again for sharing your knowledge and experience. I have one last challenge that has presented from testing the workbook. As the team progress through the worksheets of the workbook for audit purposes I have built macros to save the workbook at different stages to defined folders. For example, the team could pick the active workbook up from Packaging WIP folder and complete that task which would then save into Packaging Complete folder. During testing because of how I written the macro it saves the workbook in each folder where I would ideally like it to move it from Packaging WIP folder and replace and save the updated version to Packaging Completed folder. By doing this would mitigate unnecessary storage usage.

I have been trying to source suitable code without any success to deliver the above. Below is the present code and I know it needs to positioned where it checks for existing file exists but totally struggling with how to write it. Your insight would be gratefully received as always.

Code: Select all

xFolder = "F:\M Hub\M Wookbook\Packaging Completed\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"
' MsgBox (Len(Dir(xFolder)))
'Check if file already exist
If Len(Dir(xFolder)) = 0 Then
    xYesorNo = MsgBox("Have all pink fields been completed?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbNo Then
    Exit Sub
    End If
    Else
           xYesorNo = MsgBox(xFile & " 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 file, 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
    xFile = Application.GetSaveAsFilename(xFile, "Excel Macro-Enabled workbook(*.xlsm), *.xlsm")
    If xFile = "False" Then
        Exit Sub
    End If
    ActiveWorkbook.SaveAs xFile, xlOpenXMLWorkbookMacroEnabled
    ActiveWorkbook.Close False
    Application.ScreenUpdating = True
End Sub
Kind regards

Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

Does this do what you want?

Code: Select all

    xFolder = "F:\M Hub\M Wookbook\Packaging Completed\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"
    'Check if file already exist
    xYesOrNo = MsgBox("Have all pink fields been completed?", vbYesNo + vbQuestion)
    If xYesOrNo = vbNo Then
        Exit Sub
    End If
    If Dir(xFile) <> "" Then
        xYesOrNo = MsgBox(xFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
            vbYesNo + vbQuestion, "File Exists")
        If xYesOrNo = vbYes Then
            On Error Resume Next
            Kill xFile
            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
            On Error GoTo 0
        Else
            MsgBox "if you don't overwrite the existing file, I can't continue." _
                & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
            Exit Sub
        End If
    End If
    ActiveWorkbook.SaveAs xFile, xlOpenXMLWorkbookMacroEnabled
    ActiveWorkbook.Close False
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans

Will give it a go and let you know

Kind regards

Donna

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans
Unfortunately still leaves copy file in original location in addition to new location. Thank you for trying, will keep trying some different solutions. Regards Donna

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

Yes, I know that. The code that I posted only saves the workbook in the new location, but doesn't delete the original. See if this works. I haven't tested it and I'm not sure it is possible...

Code: Select all

    Dim xOriginal As String
    xOriginal = ActiveWorkbook.FullName
    xFolder = "F:\M Hub\M Wookbook\Packaging Completed\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"
    'Check if file already exist
    xYesOrNo = MsgBox("Have all pink fields been completed?", vbYesNo + vbQuestion)
    If xYesOrNo = vbNo Then
        Exit Sub
    End If
    If Dir(xFile) <> "" Then
        xYesOrNo = MsgBox(xFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
            vbYesNo + vbQuestion, "File Exists")
        If xYesOrNo = vbYes Then
            On Error Resume Next
            Kill xFile
            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
            On Error GoTo 0
        Else
            MsgBox "if you don't overwrite the existing file, I can't continue." _
                & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
            Exit Sub
        End If
    End If
    ActiveWorkbook.SaveAs xFile, xlOpenXMLWorkbookMacroEnabled
    Kill xOriginal
    ActiveWorkbook.Close False
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi Hans
I have tried the above without success so have tried a different approach as below however it doesn't like the highlighted section. Please could you shed light on where I may be going wrong. The first part of code relates to saving as PDF for audit trail and the latter as a working platform to next task section. Thanks Donna

Code: Select all

Private Sub CommandButton4_Click()
Application.ScreenUpdating = False
Dim xSht As Worksheet
    Dim xFolder As String
    Dim xFile As String
    Dim xYesorNo As Integer
    Dim xUsedRng As Range
    Set xSht = ActiveSheet
    xFolder = "C:\MHub\M Workbook\Imscan Import PDFs\"
    xFile = xFolder & "Packaging Completed" & "-" & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".pdf"

    'Check if file already exist
    If Len(Dir(xFile)) > 0 Then
        xYesorNo = MsgBox(xFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
            vbYesNo + vbQuestion, "File Exists")
        On Error Resume Next
        If xYesorNo = vbYes Then
            Kill xFile
        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
        'Save as PDF file
               ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFile, Quality:=xlQualityStandard, IgnorePrintAreas:=False
    Else
        MsgBox "The active worksheet cannot be blank"
        Exit Sub
    End If

    MsgBox "PDF successfully exported."

Dim FSO As Object
Dim FromDir As String
Dim ToDir As String
Dim FExtension As String
Dim FName As String
FromDir = “C:\M Hub\MWorkbook\Packaging WIP\"
ToDir ="C:\M Hub\M Workbook\Packaging Completed\"
FExtension = “*.xslm”
FNames = Dir(FromDir & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & FExtension )
If Len(FNames) = 0 Then
MsgBox “No Files in” & FromDir


xFolder = "C:\M Hub\M Workbook\Packaging Completed\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"
' MsgBox (Len(Dir(xFolder)))
'Check if file already exist
If Len(Dir(xFolder)) = 0 Then
    xYesorNo = MsgBox("Have all pink fields been completed?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbNo Then
    Exit Sub
    End If
    Else
           xYesorNo = MsgBox(xFile & " 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 file, 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 FSO = CreateObject(“Scripting.FileSystemObject”)
FSO MoveFile Source: = FromDir & FExtension, Destination: =ToDir
    xFile = Application.GetSaveAsFilename(xFile, "Excel Macro-Enabled workbook(*.xlsm), *.xlsm")
    If xFile = "False" Then
        Exit Sub
    End If
    ActiveWorkbook.SaveAs xFile, xlOpenXMLWorkbookMacroEnabled
    ActiveWorkbook.Close False
Kill FromDir
    Application.ScreenUpdating = True
End Sub

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

Which highlighted section?
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Sorry Hans
Set FSO line and line below. Thanks

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code to convert multiple Sheets into one PDF

Post by HansV »

1) In the part

Code: Select all

xFolder = "C:\M Hub\M Workbook\Packaging Completed\"
    xFile = xFolder & Worksheets("Packaging").Range("F2") & "-" & Worksheets("Packaging").Range("C3").Text & ".xlsm"
' MsgBox (Len(Dir(xFolder)))
'Check if file already exist
If Len(Dir(xFolder)) = 0 Then
    xYesorNo = MsgBox("Have all pink fields been completed?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbNo Then
    Exit Sub
    End If
    Else
           xYesorNo = MsgBox(xFile & " 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 file, 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
you appear to be mixing up files and folders. If the folder xFolder doesn't exist, you put up a message that xFile already exists. Also, Kill xFolder won't work - Kill is for files only, not for folders.

The line

Code: Select all

FSO MoveFile Source: = FromDir & FExtension, Destination: =ToDir
should be

Code: Select all

FSO.MoveFile Source:=FromDir & FExtension, Destination:=ToDir
But it won't work, since FromDir and ToDir are folder paths, not file paths.

What was the problem with the code that I posted?
Best wishes,
Hans

Donna_Dav
Lounger
Posts: 25
Joined: 19 May 2019, 21:03

Re: VBA Code to convert multiple Sheets into one PDF

Post by Donna_Dav »

Hi
Thank you, started getting carried away with expectations. Will run your suggestion again and let you know outcome as forgotten

Kind regards Donna