+ Reply to Thread
Results 1 to 8 of 8

setting file attributes on close

Hybrid View

  1. #1
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: setting file attributes on close

    After closing:

        CreateObject("scripting.filesystemobject").getfile("E:\file.xls").attributes = 3
    Last edited by snb; 09-20-2010 at 09:44 AM.



  2. #2
    Registered User
    Join Date
    04-27-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: setting file attributes on close

    sorry, i'm not very great with excel. where do i have to put this in vba? in workbook general?

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: setting file attributes on close

    Put this code in the 'ThisWorkbook module in teh VBEditor (alt-F11)
    The file will be Hidden & Readonly.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If ThisWorkbook.Saved Then CreateObject("scripting.filesystemobject").getfile(ThisWorkbook.FullName).Attributes = 3
    End Sub

  4. #4
    Registered User
    Join Date
    04-27-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Thumbs up Re: setting file attributes on close

    Thanx I was about to post that I figured it out already. Just one more question:
    attributes = 1 file is read only
    attributes = 2 file is hidden
    attributes = 3 file is read only and hidden.
    Am I correct? and is there more options?

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: setting file attributes on close

    You are correct, more options you'll find in the VBEditor's help, look for 'attributes'.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: setting file attributes on close

    Function SetAttributes(sName As String, _
                           ByVal iAttr As VbFileAttribute, _
                           bSetClear As Boolean) As Boolean
        ' shg 2009-0202
        ' Requires a reference to Microsoft Scripting Runtime
        
        ' Sets or clears properties of the specified file or folder, e.g.,
        '       SetAttributes("C:\myPath\MyFile", vbReadOnly, False)
        '       SetAttributes("C:\myPath\MyFile", vbReadOnly + vbArchive, True)
        ' Returns True if successful
    
        Dim fso     As Scripting.FileSystemObject
        Dim obj     As Object           ' set to file or folder
        Dim jAttr   As Long             ' initial properties of the file or folder
    
        ' limit attributes to those that are r/w
        iAttr = iAttr And (vbReadOnly Or vbHidden Or vbSystem Or vbArchive)
        
        Set fso = New FileSystemObject
        
        If fso.FolderExists(sName) Then
            Set obj = fso.GetFolder(sName)
        
        ElseIf fso.FileExists(sName) Then
            Set obj = fso.GetFile(sName)
        
        Else
            Exit Function   '------------------------------------------------------>
        End If
    
        jAttr = obj.Attributes
        If bSetClear Then
            obj.Attributes = (jAttr Or iAttr)
            SetAttributes = (obj.Attributes And iAttr) = iAttr
        Else
            obj.Attributes = (jAttr And Not iAttr)
            SetAttributes = Not (obj.Attributes And iAttr)
        End If
    End Function
    Note that a reference is required.

    Examples:

    SetAttributes("C:\myPath\MyFile", vbReadOnly, False) ' clear read-only
    SetAttributes ("C:\myPath\MyFile", vbReadOnly + vbArchive, True) ' set read-only and archive
    See VBE help for Attributes Property for a list
    Last edited by shg; 09-21-2010 at 08:45 AM.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    04-27-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Thumbs up Re: setting file attributes on close

    Thank you very much for your help! This is solved!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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