+ Reply to Thread
Results 1 to 10 of 10

Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    I am trying to save all the worksheets as individual workbook in PDF format in a special folder with cell value. But it is giving me syntax error in below code. Can anyone help me to resolve this issue please.

    Showing Error
     .SaveAs FolderName _
        & "\Invitation Letter - " & .Sheets(1).Range("E4").Value & FileExtStr, ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False,OpenAfterPublish:=False




    Original Complete Code
    
    'Working in 97-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim FolderName As String
        With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        End With
    
    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ThisWorkbook
    'Create new folder to save the new files in
            DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
                FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " (Invitation Letter) " & " " & " " & DateString
                    MkDir FolderName
                        'Copy every visible sheet to a new workbook
                        For Each sh In Sourcewb.Worksheets
                    'If the sheet is visible then copy it to a new workbook
                    If sh.Visible = -1 Then
                sh.Copy
            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook
        'Determine the Excel version and file extension/format
        With Destwb
        End With
    'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                        .Cells.PasteSpecial xlPasteValues
                        .Cells(1).Select
                    End With
                Application.CutCopyMode = False
            End If
        With Destwb
        
       .SaveAs FolderName _
        & "\Invitation Letter - " & .Sheets(1).Range("E4").Value & FileExtStr, ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False,OpenAfterPublish:=False
        
        
            '.SaveAs FolderName _
            '& "\Invitation Letter - " & .Sheets(1).Range("E4").Value & FileExtStr, _
            'FileFormat:=51 'FileFormatNum
                        .Close False
                            End With
                                End If
    GoToNextSheet:
                            Next sh
                        Response = MsgBox("You can find the files in..." & FolderName + vbCrLf + " " + vbCrLf + "Abdul Aleem - Lets Make Life Easier...", vbOKOnly + vbInformation, "Managed Care Department")
                    With Application
                .ScreenUpdating = True
            .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    try this
    Sub exportToPdf()
    strFilePath = "D:\test\"
     Application.ScreenUpdating = False
    For Each Sh In Sheets
       strPdfName = Sh.Name & ".pdf"
       Sh.Copy
       ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilePath & strPdfName, _
       Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
       OpenAfterPublish:=False
       ActiveWorkbook.Close (False)
    Next
    Application.ScreenUpdating = True
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    Thanks for reply again i am getting an error.

    Display Error:
    Document not saved. The document may be open, or an error may have been encountered when saving.
    I am attaching a sample file where Excel format is working fine but in the same way i need in PDF also.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    I tested your file with my code, it works fine, did you change the path ?
    strFilePath = "D:\test\"

  5. #5
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    I appreciate if you make necessary changes on the script posted in #1 because that scrip is making the folder where the file is running and caving the files base on cell value.

    Your script is totally different from that script.

  6. #6
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    I'm sorry, but for me your code is very confused, try this
    Sub exportToPdf()
    Dim sh As Worksheet
    Set Sourcewb = ThisWorkbook
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " (Invitation Letter) " & " " & " " & DateString
    MkDir FolderName
    For Each sh In Sourcewb.Worksheets
       If sh.Name <> "Briefing List" Then
         sh.Copy
         strPdfName = FolderName & "\Invitation Letter - " & Range("C2").Value & ".pdf"
         ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPdfName, _
         Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
         OpenAfterPublish:=False
         ActiveWorkbook.Close (False)
       End If
    Next
    Application.ScreenUpdating = True
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    Hai Patel,

    This code works perfect as per my requirement. I will test it on my original file and get back to you if i have any problem.

  8. #8
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    Hai Patel,

    The script is showing error on my original file on

    sh.Copy
    My original file have some hidden sheets might be this is the problem. Can you help me on this issue please. I need to convert only visible sheets into PDF.

  9. #9
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    Sub exportToPdf()
    Dim sh As Worksheet
    Set Sourcewb = ThisWorkbook
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    'FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " (Invitation Letter) " & " " & " " & DateString
    'MkDir FolderName
    For Each sh In Sourcewb.Worksheets
       If sh.Name <> "Briefing List" And sh.Visible = xlSheetVisible Then
         sh.Copy
         strPdfName = FolderName & "\Invitation Letter - " & Range("C2").Value & ".pdf"
         ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPdfName, _
         Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
         OpenAfterPublish:=False
         ActiveWorkbook.Close (False)
       End If
    Next
    Application.ScreenUpdating = True
    End Sub

  10. #10
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    906

    Re: Save All Worksheets As Individual Workbooks In a Special Folder in PDF format

    Thanks it is working on my original file also. Topic marked as solved and reputation added.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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