• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Replace file in same path and with same name

hsm123

New Member
Requirements:
  1. Protect all sheets
  2. Hide all sheets except PERMISSIONS sheet
  3. Add password to the file and save it in the same path with same name without prompt
The below shows an error saying to replace the file extension. I am currently using the binary format and to save the file in binary format only.


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim a110w As Variant
Dim path As String
Dim fName As String

a110w = "123" 'P
path = ThisWorkbook.path
fName = ThisWorkbook.FullName


      
      ThisWorkbook.Unprotect Password:=a110w 'Unprotect worbook structure
      
     For Each Worksheet In Sheets 'Protect all sheets
      
          Worksheet.Protect Password:=a110w
     Next

     For Each Worksheet In Sheets 'Hide all sheets except PERMISSIONS sheet
      
         If Not Worksheet.Name = "Permissions" Then Worksheet.Visible = xlVeryHidden
      Next
      
      
Application.DisplayAlerts = False
      
ActiveWorkbook.SaveAs Filename:=path & fName, FileFormat:=50, Password:=a110w
ActiveWorkbook.Close savechanges:=True


      
Application.DisplayAlerts = True

      

End Sub
 
Possibly...
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim sh As Worksheet
    Dim fName As String
    Const a110w As String = "123"

    With Me
        fName = Left(.FullName, InStrRev(.FullName, ".") - 1)
        .Unprotect Password:=a110w
       
        For Each sh In .Sheets
            If Not sh.Name = "Permissions" Then sh.Visible = xlVeryHidden
            sh.Protect Password:=a110w
        Next sh
       
        .SaveAs Filename:=fName, FileFormat:=50, Password:=a110w
    End With
End Sub
 
Possibly...
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim sh As Worksheet
    Dim fName As String
    Const a110w As String = "123"

    With Me
        fName = Left(.FullName, InStrRev(.FullName, ".") - 1)
        .Unprotect Password:=a110w
      
        For Each sh In .Sheets
            If Not sh.Name = "Permissions" Then sh.Visible = xlVeryHidden
            sh.Protect Password:=a110w
        Next sh
      
        .SaveAs Filename:=fName, FileFormat:=50, Password:=a110w
    End With
End Sub
Apologies for coming up after a long time. Just found out the code does not work properly if the file name is "abcd_v1.0". Is there a way around we could fix this issue?
Basically to make a save as irrespective of the file name containing a special character or not.
 
Back
Top