Solved

Remove Excel Workbook Password Programatically

Posted on 2008-06-19
2
429 Views
Last Modified: 2010-08-05
I have code which protects a workbook with a password so it cannot be opened without which works great. Now I am trying to reverse the process but when I save the workbook again with no password argument it just keeps the password on. I tried using a blank string and got an error.

How can I remove the password of a workbook?


'This function unprotects the specified sheet
Private Sub UnprotectSheet()
Dim excelApplication As Excel.Application
Dim workbook As Excel.workbook
Dim strTemporaryPath As String
    
    strFileName = "hi"
    strPath = "C:\Documents and Settings\bejhan.jetha\Desktop"
    strPassword = "pass"
    
    strTemporaryPath = "C:\Documents and Settings\bejhan.jetha\Desktop\Temp\"
    
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
 
    'Open the excel spreadsheet
    Set excelApplication = New Excel.Application
    Set workbook = excelApplication.Workbooks.Open(strPath & strFileName & ".xls", , , , strPassword)
    
    'Resave it unprotected
    workbook.SaveAs strTemporaryPath & strFileName
    
    workbook.Close
    Set workbook = Nothing
    
    excelApplication.Quit
    Set excelApplication = Nothing
    
    'Delete the original
    Kill strPath & strFileName & ".xls"
    
    'Copy the sheet back to the original directory
    FileCopy strTemporaryPath & strFileName & ".xls", strPath & strFileName & ".xls"
    
    'Delete the temporary
    Kill strTemporaryPath & strFileName & ".xls"
End Sub

Open in new window

0
Comment
Question by:bejhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
bandriese earned 125 total points
ID: 21827736
I see no one has answered this question, so I give it a try.

You might have tried this already, but try setting the password to nothing. As in

strPassword = ""
    ~or~
strPassword = Null

I don't see anything in the code that makes the password go away. There may be a function or property for this without using the suggestion above.  I suspect that what's happening is once the password has been set it will remain there unless specificaly removed.
0
 
LVL 1

Author Comment

by:bejhan
ID: 21831742
Hmm, I thought I tried using a blank string and got an error. Must have put it as the wrong argument. I guess that's what happens when you stare at the screen for too long :P. Just for the record Null doesn't do anything. The blank string did the trick.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question