+ Reply to Thread
Results 1 to 6 of 6

Add or Remove Passwords from Files

  1. #1
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Add or Remove Passwords from Files

    I have a script that loops through the files in a choosen folder and is supposed to protect or unprotect the workbook depending on what the user types into a Application.InputBox.

    The script will cycle through the files but does not either protect or unprotect them as expected. And from all my research, the code seems to be correct.
    I am looking for a little guidance with the code below.

    I have sectioned off the part I need help with.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Add or Remove Passwords from Files

    ' 'I NEED HELP WITH THIS BIT.
    ' ' Want to test if the user entered a correct value before proceeding
    A bit different than you thought:
    Please Login or Register  to view this content.
    ' I NEED HELP WITH THIS PART
    ' THE PROTECT AND UNPROTECT DO NOT SEEM TO BE DOING WHAT THEY ARE SUPPOSED TO
    ' I DO NOT GET AN ERROR
    You found the wrong protection. You want to password protect the file on opening, but you used workbook structure protection. Keep looking.

    Artik

  3. #3
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Add or Remove Passwords from Files

    First off thank you for taking the time to review and answer my questions, this is very helpful.


    I am however, still unable to protect workbooks??

    Please Login or Register  to view this content.

    I have tried each of these and then both together. I'm sure I am close, but almost all websites are telling me the same thing, and that is what I am trying.
    Any guidance?

  4. #4
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Add or Remove Passwords from Files

    I ended up getting the code to work.
    See the code below:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Add or Remove Passwords from Files

    Your code after the fix is pretty good, although you trusted me too much.
    For example. This condition will never be met:
    Please Login or Register  to view this content.
    I wrote it wrong.

    This snippet won't necessarily work either:
    Please Login or Register  to view this content.
    You mistakenly assumed that the user would type words in capital letters. And before that, you didn't convert user responses to uppercase.

    This code construction is very dangerous:
    Please Login or Register  to view this content.
    The explanation is going to be a little tricky.
    Before explaining the problem with the cited code, you need to know what happens when you use SaveAs. This command saves the workbook under a new name, the "source" workbook is abandoned (closed without saving changes), and the active workbook is already a new instance of the "source" workbook.
    With ActiveWorkbook refers to the active workbook. And everything inside that construction refers to what comes after With, which is the "source" workbook.
    .SaveAs still refers to the correct workbook, but .Close does not. You are trying to close a workbook that is no longer there because it was abandoned when you saved it. But do not worry. You are not alone. Even though I know about this trap, I made this mistake a moment ago.

    Sometimes a folder may contain temporary files that are created when you open a particular workbook. After the file is properly closed, the temporary file is deleted by Excel. However, when a file is closed abruptly (due to a fatal error), the temporary file may remain on disk. They start with a tilde (~), but the file extension is the same as the actual file. Temporary files have a very different structure and you may get an error when you open them in Excel. When looping through files, exclude them.

    It's mine now.
    I don't trust the Dir() function, so I use File System Object. This makes the code longer. Besides, I like "rich" code. In addition, I pay more attention to the safe execution of the code. You can apply it, or you can stay with yours, provided that you correct the mistakes I mentioned.
    Please Login or Register  to view this content.
    Artik
    Last edited by Artik; 07-08-2021 at 09:59 PM.

  6. #6
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Add or Remove Passwords from Files

    @Artik This is amazing!
    Its funny you should say that about your bit of code,
    Please Login or Register  to view this content.
    I did have to work on it after I posted my "Solution". See below:

    Thank you very much for taking the time and effort to work this all out and for the concise explanation! Very much appreciated.
    I will take this all in. I find after I figure a bit of code out (or get the answer in this case) I can take the time to do a deeper learning dive.

    Thanks again,

    Joe

    Please Login or Register  to view this content.

+ 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. Replies: 3
    Last Post: 02-08-2021, 06:56 PM
  2. macro to remove worksheet-level passwords
    By DavidH in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2013, 01:33 PM
  3. Autocomplete passwords to linked files
    By Brokovich in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2007, 04:23 AM
  4. [SOLVED] I need to remove passwords from a list of files.
    By Conan Kelly in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2005, 01:50 PM
  5. suppressing passwords to edit linked files
    By tjtjjtjt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2005, 09:05 PM
  6. updateLink; files with passwords , VBA help!
    By tp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-13-2005, 05:05 AM

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