After closing:
![]()
CreateObject("scripting.filesystemobject").getfile("E:\file.xls").attributes = 3
After closing:
![]()
CreateObject("scripting.filesystemobject").getfile("E:\file.xls").attributes = 3
Last edited by snb; 09-20-2010 at 09:44 AM.
sorry, i'm not very great with excel. where do i have to put this in vba? in workbook general?
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
ThanxI 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?
You are correct, more options you'll find in the VBEditor's help, look for 'attributes'.
Note that a reference is required.![]()
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
Examples:
See VBE help for Attributes Property for a list![]()
SetAttributes("C:\myPath\MyFile", vbReadOnly, False) ' clear read-only SetAttributes ("C:\myPath\MyFile", vbReadOnly + vbArchive, True) ' set read-only and archive
Last edited by shg; 09-21-2010 at 08:45 AM.
Entia non sunt multiplicanda sine necessitate
Thank you very much for your help! This is solved!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks