+ Reply to Thread
Results 1 to 2 of 2

looping through folders - saving xls files as PDFs

  1. #1
    Registered User
    Join Date
    05-11-2016
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    2

    looping through folders - saving xls files as PDFs

    Hi,

    I've written code in one worksheet module that I'm using to loop through multiple folders containing dozens of xls files. Each file is of the same format with multiple graphs and 3 tables of data. The charts and tables (which are linked from a second tab in the workbook) are manipulated then the active sheet is saved as a PDF file.

    Everything runs fine except that I'm updating the headings on each of the 3 tables with 3 character acronyms for the department that the report is for. When I step through the code it's great, pdf saves and the proper headings are there. When I let the code run the updated headings are not there, the default title is.

    I've tried everything i can think of. I ended up writing code to copy each table and pasted it over the original table, which seemed to work for a while, but that didn't last. Default titles returned.

    Anyway, I'm using Excel 2010

    UPDATE - I brought the files home and everything runs fine, table headings update. Must be something in the work environment?

    Below is the main code, sorry for the messiness.

    Dim wb As Workbook
    Dim myPath As String
    Dim myfile As String
    Dim myextension As String

    Dim strFolder As FileDialog
    Dim strBase As Folder
    Dim strSub As Folder
    Dim strFSO As FileSystemObject
    Dim xlFile As File

    Dim strBranch As String
    Dim strBranchFullName As String
    Dim sFrench As String

    'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    Set strFSO = New FileSystemObject

    'Retrieve Target Folder Path From User
    Set strFolder = Application.FileDialog(msoFileDialogFolderPicker)

    With strFolder
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    .Show


    myPath = .SelectedItems(1) & "\"
    If myPath = "" Then Exit Sub
    End With


    Set strBase = strFSO.GetFolder(myPath)

    For Each strSub In strBase.SubFolders

    For Each xlFile In strSub.Files


    If xlFile.Name Like "*.xls" Then
    Set wb = Workbooks.Open(Filename:=xlFile.Path)

    strBranch = Mid(xlFile.Name, 14, (InStr(14, xlFile.Name, "-") - 14))

    If IsNumeric(Left(strBranch, 6)) Then
    strBranch = Mid(strBranch, 7)
    Else
    strBranch = Mid(strBranch, 5)
    End If

    wb.Worksheets("Layout").Activate

    If strBranch = "EPD" Then strBranch = "PRD"
    If strBranch = "ERP" Then strBranch = "EPR"
    If strBranch = "CSR" Then strBranch = "SCSR"

    Range("A32") = strBranch

    If Left(Right(xlFile.Name, 6), 2) = "EN" Then
    Call UpdateBranchFull_ENG(strBranch, Range("A30"))
    Else
    Call UpdateBranchFull_FR(strBranch, Range("A30"))
    sFrench = Trim(Range("A30"))
    Call UPDATEBranchAcronym_FR(sFrench, Range("A32"))
    End If

    '************************************************************
    'uPDATE CELLS WITH BRANCH ACRONYM!
    '************************************************************
    wb.Worksheets("Data").Activate

    Cells.Select

    Selection.Replace What:="BRANCH", Replacement:=strBranch, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    Range("A1").Select

    wb.Worksheets("Table").Activate

    Range("D6").Select
    ActiveCell.Value = strBranch

    Range("M24").Select
    ActiveCell.Value = strBranch

    Range("U35").Select
    ActiveCell.Value = strBranch

    Range("A1").Select

    wb.Worksheets("layout").Activate

    Range("V6").Select
    ActiveSheet.Shapes.Range(Array("Image 57")).Select
    Selection.Copy
    Selection.Delete
    Range("V6").Select
    ActiveSheet.Pictures.Paste.Select
    Selection.ShapeRange.IncrementLeft 229
    Selection.ShapeRange.IncrementTop -11.25


    ActiveSheet.Shapes.Range(Array("Picture 59")).Select
    Selection.Copy
    Selection.Delete
    Range("V20").Select
    ActiveSheet.Pictures.Paste.Select
    Selection.ShapeRange.IncrementLeft 13
    Selection.ShapeRange.IncrementTop 33
    Range("V20").Select

    '*******************************************************
    Call Template_uPDATE
    '*******************************************************
    'Update Charts
    '*******************************************************
    wb.Worksheets("layout").Activate
    Call uPDATEchartlegend

    wb.Worksheets("layout").Activate
    Range("A1").Select

    '*******************************************************
    'Save and Close Workbook
    '*******************************************************

    wb.SaveAs xlFile

    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=p & "\" & newFile & ".PDF", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

    'Call saveToPDF(strSub.Path, xlFile.Name)

    Set wb = Nothing
    End If
    Next

    Any thoughts.
    Last edited by Hugh Jass; 05-11-2016 at 08:24 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: looping through folders - saving xls files as PDFs

    Perhaps if you uploaded some sample files as well as the codes you are using and then someone here may be able to provide a resolution. Not enough information to provide you with an answer.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. looping through folders - saving xls files as PDFs
    By Hugh Jass in forum Excel General
    Replies: 1
    Last Post: 05-11-2016, 06:06 PM
  2. Saving as PDFs, but individually....
    By luke1234567 in forum Excel General
    Replies: 1
    Last Post: 11-19-2014, 06:25 AM
  3. Looping thru files in 2 folders,but the macro picking the files by the modified date
    By XLbeginer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-21-2013, 10:37 AM
  4. Replies: 2
    Last Post: 01-04-2013, 07:22 AM
  5. Replies: 1
    Last Post: 02-29-2012, 01:15 PM
  6. Saving Worksheet as different pdfs
    By manny_cb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2009, 03:49 AM

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