VBA Code to add password to open excel file

s0nicstang

Board Regular
Joined
Jan 7, 2009
Messages
73
Im using VBA to create a new workbook and copy paste info into that work book. This work book will then be emailed off to someone else, but I would like to add a password to the file so it can only be open with the password. In 07 you can do this by going to the encrypt document option, but I would like the code to do it when it creates the file. Is this possible?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Im using VBA to create a new workbook and copy paste info into that work book. This work book will then be emailed off to someone else, but I would like to add a password to the file so it can only be open with the password. In 07 you can do this by going to the encrypt document option, but I would like the code to do it when it creates the file. Is this possible?


Nevermind figured it out that was easy :)
 
Upvote 0
It always helps if you can post what you did. The forum is used by a lot of folks as a searchable repository, so having answers might help someone in the future.
 
Upvote 0
Good point, Smitty.

It took me a day or two to find this information when I first needed it in 2005 and its refinment over time is also due to the forum. I'm happy to provide a small return on the great help I've always gotten.

Code:
'----------------------------------------------------------------------------
Sub zProtect_All(bProtect)
'
' ProtectAll Macro
' Macro recorded 8/23/2005 at TSI
' Help over the net from a guy posting an Irish flag
' Prompting 4 pwd suggested by Crimson B1ade in SEP08
'
Dim ws As Worksheet
Dim cPwd As String
Dim bProtected As Boolean,  bUpdating As Boolean
'
    cPwd = InputBox(Prompt:="Enter password to change protection status of worksheets: ", _
                    Title:="Password Input")

    'Preserve initial worksheet status; maybe store in an array or Public mVars?
    bProtected = ActiveSheet.Protect
    bUpdating = Application.ScreenUpdating 
    
    Application.ScreenUpdating = False
    If bProtect Then
        For Each ws In ActiveWorkbook.Worksheets
            ws.Protect Password:=cPwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, UserInterFaceOnly:=True
        Next ws
        ActiveWorkbook.Protect Password:=cPwd, Structure:=True, Windows:=False
    
    Else
        'ActiveWorkbook.Unprotect Password:="cPwd"
        For Each ws In ActiveWorkbook.Worksheets
            ws.Unprotect Password:=cPwd
        Next ws
    
    
    End If
    Application.ScreenUpdating = bUpdating

End Sub
'----------------------------------------------------------------------------
 
Upvote 0
Sure thing, it was actually quite easy


ActiveWorkbook.SaveAs Filename:="FilePath", Password:="password"
 
Upvote 0
Sure thing, it was actually quite easy


ActiveWorkbook.SaveAs Filename:="FilePath", Password:="password"

hey sOnicstang - for someone as not as VBA savvy as you, how would I include the above password protect to my VBA that splits one workbook into multiple workbooks by the tabs? (I want to be able to add a password protect to all of the workbooks this macro will create.) See code below:

Sub Splitbook()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
Sub Splitbook()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
With Application
  .ScreenUpdating = False
  .DisplayAlerts = False
End With
    For Each xWs In ThisWorkbook.Sheets
        xWs.Copy
        ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx", Password:="password"
        ActiveWorkbook.Close False
    Next
With Application
  .DisplayAlerts = True
  .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Try
Code:
Sub Splitbook()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
With Application
  .ScreenUpdating = False
  .DisplayAlerts = False
End With
    For Each xWs In ThisWorkbook.Sheets
        xWs.Copy
        ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx", Password:="password"
        ActiveWorkbook.Close False
    Next
With Application
  .DisplayAlerts = True
  .ScreenUpdating = True
End With
End Sub

Thank you so much! It worked! :)
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top