PDA

View Full Version : Using VBA to change File Folder Properties



Coleman34
08-26-2014, 07:30 AM
So I am trying to select a folder and have VBA toggle the read only option on that folder. I am at a loss at the moment, probably brain lock. Any constructive help would be appreciated as I am relatively new to VBA functionality of this level.

Public Function ChangeReadOnly()
Dim oFSO
Dim oFolder
Dim oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("\myPath\targetFolder")


For Each oFile In oFolder
FileAtt = VBA.GetAttr(oFile.Path)
If FileAtt And vbReadOnly Then
Call VBA.SetAttr(oFile.Path, FileAtt = Not (vbReadOnly))
End If
End Function

mancubus
08-26-2014, 07:43 AM
check this out:
http://www.techonthenet.com/excel/formulas/setattr.php

Coleman34
08-26-2014, 08:11 AM
Thank you. I was poking around the resource you provided. It seems that the read only attribute can't be toggled. I can try and set it as vbNormal. As long as this is a possibility and I'm not chasing ghosts, I will keep working. Anymore input would be very welcome.

Edit: Great information in that link for file manipulation

I have found: set the vbReadOnly to 0 and it toggles the attribute off

example: SetAttr "C:\myPath\myDoc.docx", vbReadOnly = 0

However I can't seem to apply this to a File Folder. Not sure if anyone has information on that specifically.

snb
08-26-2014, 08:55 AM
To make a folder - G:\OF- (16) hidden (18)


Sub M_snb()
CreateObject("scripting.filesystemobject").getfolder("G:\OF").Attributes = 18
End Sub