Results 1 to 7 of 7

Open all files in a folder using list of password

Threaded View

  1. #1
    Registered User
    Join Date
    06-11-2021
    Location
    Vietnam
    MS-Off Ver
    MS Office 365 Pro
    Posts
    5

    Open all files in a folder using list of password

    Dear all,

    I am a beginner in using macro excel for automation.

    I have nearly 100 of excel files with password. I would like to do as follows.

    - Step 1: I will choose Input folder contains all excel files.
    - Step 2: I will choose Output folder contains all output excel file.
    - Step 3: File structure and password are as follows.

    File structure Password
    PV*.xlsx PV
    JP*.xls JP
    PIT calculation*01-20*.xlsx 0120
    PIT calculation*02-20*.xlsx 0220
    PIT calculation*03-20*.xlsx 0320
    PIT calculation*04-20*.xlsx 0420
    PIT calculation*05-20*.xlsx 0520
    PIT calculation*06-20*.xlsx 0620
    PIT calculation*07-20*.xlsx 0720
    PIT calculation*08-20*.xlsx 0820
    PIT calculation*09-20*.xlsx 0920
    PIT calculation*10-20*.xlsx 1020
    PIT calculation*11-20*.xlsx 1120
    PIT calculation*12-20*.xlsx 1220

    Step 4: if the input file in .xls, convert it to .xlsx

    I have write a code as follows but when I run it, it seems endless run. Please help to correct my code. Thank you.

    Sub RemovePasswords()
    Dim xlBook As Workbook
    Dim InPath As Variant 'Input Folder Path
    Dim OutPath As Variant 'Output Folder Path
    Dim x1BookName As Variant
    
        InPath = ThisWorkbook.Sheets("RemovePassword").Range("B2").Value & "\"       'The Input folder, must end with "\"
        OutPath = ThisWorkbook.Sheets("RemovePassword").Range("B3").Value & "\"       'The Output folder , must end with "\"
    Const strEditPassword As String = "" 'If no password use ""
    
    'File structure 1 and password 1
    Dim strFilename1 As String
    Dim strPassword1 As String
        strFilename1 = Dir$(InPath & ThisWorkbook.Sheets("RemovePassword").Range("A8").Value)  'will open xls & xlsx etc
        strPassword1 = ThisWorkbook.Sheets("RemovePassword").Range("B8").Value
        While Len(strFilename1) <> 0
            Application.DisplayAlerts = False
            Set xlBook = Workbooks.Open(Filename:=InPath & strFilename1, _
                                        Password:=strPassword1, _
                                        WriteResPassword:=strEditPassword)
            x1BookName = ActiveWorkbook.Name
            
            xlBook.SaveAs Filename:=OutPath & x1BookName, _
                          Password:="", _
                          WriteResPassword:="", _
                          CreateBackup:=True
            xlBook.Close 0
            Application.DisplayAlerts = True
        Wend
    
    'File structure 2 and password 2 then save as .xlsx
    
    Dim strFilename2 As String
    Dim strPassword2 As String
        strFilename2 = Dir$(InPath & ThisWorkbook.Sheets("RemovePassword").Range("A9").Value)  'will open xls & xlsx etc
        strPassword2 = ThisWorkbook.Sheets("RemovePassword").Range("B9").Value
        While Len(strFilename2) <> 0
            Application.DisplayAlerts = False
            Set xlBook = Workbooks.Open(Filename:=InPath & strFilename2, _
                                        Password:=strPassword2, _
                                        WriteResPassword:=strEditPassword)
            x1BookName = ActiveWorkbook.Name
            
            xlBook.SaveAs Filename:=OutPath & Left(x1BookName, Len(x1BookName) - 5) & ".xlsx", _
                            FileFormat:=xlOpenXMLWorkbook, _
                          Password:="", _
                          WriteResPassword:="", _
                          CreateBackup:=True
            xlBook.Close 0
            Application.DisplayAlerts = True
        Wend
    
    'File structure 3 and password 3
    
    Dim strFilename3 As Range
    Dim strPassword3 As String
    Set FilenameRange = ThisWorkbook.Sheets("RemovePassword").Range("A9:A20")
        
        For Each strFilename3 In FilenameRange 'or For Each strFilename3 In Range("A5", Cells(Rows.Count, 1).End(xlUp))
        strPassword3 = strFilename3.Offset(0, 1).Value
        
            Application.DisplayAlerts = False
        If Not Len(strFilename3) = 0 Then
             Application.DisplayAlerts = False
            Set xlBook = Workbooks.Open(Filename:=InPath & strFilename2, _
                                        Password:=strPassword2, _
                                        WriteResPassword:=strEditPassword)
            x1BookName = ActiveWorkbook.Name
            
            xlBook.SaveAs Filename:=OutPath & x1BookName, _
                          Password:="", _
                          WriteResPassword:="", _
                          CreateBackup:=True
            xlBook.Close 0
            
        End If
    
        Next strFilename3
        Application.DisplayAlerts = True
    End Sub
    Attached Files Attached Files
    Last edited by smiledt06; 06-17-2021 at 11:14 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA to open the list of excel files and save with password
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2020, 02:38 AM
  2. [SOLVED] Macro to list and open .xlsm files in folder and sub-folder
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-24-2019, 07:35 PM
  3. [SOLVED] open list of all .csv files in a folder and copy few colums in a excel of same sheet.
    By gnrao80 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-04-2014, 01:19 AM
  4. [SOLVED] Open password protected files in folder using list of passwords
    By Shift-4 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-22-2013, 06:08 PM
  5. [SOLVED] Password array to open all password protected workbook in a folder
    By rename in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-07-2012, 05:06 AM
  6. [SOLVED] Open all files in a SharePoint folder (Problem: Files open out of order)
    By holmstarrunner in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2012, 10:13 AM
  7. Replies: 2
    Last Post: 04-15-2005, 12:06 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