• Password Protecting Multiple Word Docs. in a Single File

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Password Protecting Multiple Word Docs. in a Single File

    Author
    Topic
    #508387

    Is there a macro (or more efficient way) for protecting multiple word docs in a single file (have over 1200) using the SAME password? I saw a post by RetiredGeek for multiple passwords but I just need 1 password for a large volume of docs. I tried using the macro offered by Mr. Mayor but I can’t seem to get it to work. Very new to VBA – Windows 10; Office 2013.

    Thanks in advance.

    Viewing 5 reply threads
    Author
    Replies
    • #1593393

      BBaker,

      Here’s some code that should do the trick:

      Code:
      Option Explicit
      
      '                        +--------------------------+            +----------+
      '------------------------|Windows Function Type Defs|------------| 08/11/05 |
      '                        +--------------------------+            +----------+
      Public Type BROWSEINFO
          hOwner         As Long
          pidlRoot       As Long
          pszDisplayName As String
          lpszTitle      As String
          ulFlags        As Long
          lpfn           As Long
          lParam         As Long
          iImage         As Long
      End Type
      
      '                     +-----------------------------+            +----------+
      '---------------------|Windows Function Declarations|------------| 08/11/05 |
      '                     +-----------------------------+            +----------+
      Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
                                      ByVal pszPath As String) As Long
                                      
      Declare Function SHBrowseForFolder Lib "shell32.dll" _
      Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
      
      Sub PWPDirectory()
      
         Dim lDocCnt      As Long
         Dim lPWDCnt      As Long
         Dim zProcessDir  As String
         Dim zNextFile    As String
         Dim zPWD         As String
         Dim oDocToPW     As Document
        
         zProcessDir = zGetDirectory()
         If zProcessDir = "" Then Exit Sub
         
         zPWD = "YOURWORDHERE"   '*** Change the Password! ***
         
         zNextFile = Dir(zProcessDir & "*.doc*")
         If zNextFile = "" Then Exit Sub
         
         Application.ScreenUpdating = False
          
         Do
      
           Set oDocToPW = Documents.Open(zProcessDir & "" & zNextFile)
           
           With oDocToPW
               .Password = zPWD
               .WritePassword = zPWD
               .Save
               .Close
           End With 'oDocToPw
           
           Set oDocToPW = Nothing
           zNextFile = Dir()
           
         Loop Until zNextFile = ""
         
        Application.ScreenUpdating = True
        
      End Sub   'PWPDirectory
      
      '                         +-------------------------+            +----------+
      '-------------------------|     zGetDirectory()     |------------| 07/25/05 |
      '                         +-------------------------+            +----------+
      'Calls: N/A
      'Notes: This function will bring up a form to let the user select a directory
      
      Public Function zGetDirectory(Optional Msg) As String
      
          Dim bInfo As BROWSEINFO
          Dim zPath As String
          Dim lRetVal2 As Long, lRetVal As Long, iEndOfStr As Integer
      
          bInfo.pidlRoot = 0  '*** Root folder = Desktop ***
      
      '***   Title in the dialog ***
          If IsMissing(Msg) Then
              bInfo.lpszTitle = "Select a Drive/Directory."
          Else
              bInfo.lpszTitle = Msg
          End If
      
          bInfo.ulFlags = &H1  '*** Type of directory to return ***
          lRetVal = SHBrowseForFolder(bInfo)  '*** Display the dialog ***
          zPath = Space$(512)     '*** Parse the result ***
          lRetVal2 = SHGetPathFromIDList(ByVal lRetVal, ByVal zPath)
          If lRetVal2 Then
              iEndOfStr = InStr(zPath, Chr$(0))
              zGetDirectory = Left(zPath, iEndOfStr - 1)
          Else
              zGetDirectory = ""
          End If
          
      End Function             'zGetDirectory(Optional Msg)
      

      The code has an interactive dialog to select the directory to process. I you want to hard code that value you can delete the zGetDirectory function and change the line zProcessDir = zGetDirectory() to zProcessDir = “d:path…” or zProcessDir = Inputbox(“Enter Directory to process”) to be prompted to manually enter the directory information.

      BTW: This code should be placed in its own document in a Module!

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1593401

      1200+ docs in a single file? 7-zip?

    • #1593402

      Bruce,

      I assumed he meant Directory but you may be right. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1593404

      New-Bruce has a choice of two Bruces, Bruce! 😀

    • #1593420

      I have typed file folder from time to time, probably thread starter also meant to type file folder, which of course, is the same thing as a directory 🙂 However, if thread starter did manage to shovel DOCs into a single file — I’m sticking around to find out just how that was done.

      "Take care of thy backups and thy restores shall take care of thee." Ben Franklin, revisted

    • #1593437

      I meant directory. 😮

      Thanks RG! You’re the man.

      • #1593483

        I meant directory. 😮

        Don’t worry about it, even Microsoft can’t get it right and they were the ones to rename Directory to Folder a few versions of Windows ago, about the time they introduced LFN/Long File Names.

        Before you wonder "Am I doing things right," ask "Am I doing the right things?"
    Viewing 5 reply threads
    Reply To: Password Protecting Multiple Word Docs. in a Single File

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: