View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Diddy Diddy is offline
external usenet poster
 
Posts: 155
Default Loop through files in a folder and unprotect sheets

Hi Barb,

Thanks for replying, I really appreciate your help :-)

I'm really fumbling around in the dark with this. I've tried altering your
code and get a run time error 1004 could not find at this line

Set myWB = Workbooks.Open(myName)

from the following mangled up bit of code
Option Compare Text
Option Explicit

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260

Type BrowseInfo
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszINSTRUCTIONS As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type

Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As Long, _
ByVal pszBuffer As String) As Long
Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _
lpBrowseInfo As BrowseInfo) As Long

Function BrowseFolder(Optional Caption As String = "") As String

Dim BrowseInfo As BrowseInfo
Dim FolderName As String
Dim ID As Long
Dim Res As Long

With BrowseInfo
.hOwner = 0
.pidlRoot = 0
.pszDisplayName = String$(MAX_PATH, vbNullChar)
.lpszINSTRUCTIONS = Caption
.ulFlags = BIF_RETURNONLYFSDIRS
.lpfn = 0
End With
FolderName = String$(MAX_PATH, vbNullChar)
ID = SHBrowseForFolderA(BrowseInfo)
If ID Then
Res = SHGetPathFromIDListA(ID, FolderName)
If Res Then
BrowseFolder = Left$(FolderName, InStr(FolderName, _
vbNullChar) - 1)
End If
End If

End Function

'|||||||||||||||||||||||||| END OF CHIP PEARSON'S CODE
||||||||||||||||||||||||||||||||
'Barb Reinhardt's Code altered by me

Sub FingersCrossed()

'http://www.microsoft.com/office/community/en-us/default.mspx?&lang=en&cr=US&guid=&sloc=en-us&dg=microsoft.public.excel.programming&p=1&tid=7 fdb3cee-fee5-4a2e-8ac7-d241d7a138d4&mid=20ef6639-91a7-4da7-8152-754bb57f77b2Sub Test()
' Display the names in C:\ that represent directories.
Dim myWB As Workbook
Dim AutoSecurity As MsoAutomationSecurity
'Dim myPath As String
Dim myName As String
Dim Path As String
Dim Prompt As String
Dim Title As String

Path = BrowseFolder("Select A Folder")
If Path = "" Then
Prompt = "You didn't select a folder. The procedure has been
canceled."
Title = "Procedure Canceled"
MsgBox Prompt, vbCritical, Title
Else
Prompt = "You selected the following path:" & vbNewLine & Path
Title = "Procedure Completed"
MsgBox Prompt, vbInformation, Title
End If
'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
'myPath = "Z:/myFolderPath/*.xls" ' Set the path.
' Retrieve the first entry.
myName = Dir(Path)
Do
Debug.Print myName

AutoSecurity = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityLow
Set myWB = Workbooks.Open(myName)
Call UnprotectWB(myWB)
Application.AutomationSecurity = AutoSecurity
On Error Resume Next

myName = Dir ' Get next entry.
Loop While myName < ""

End Sub
Sub UnprotectWB(myWB As Workbook)
Dim myWS As Worksheet
'If worksheets are password protected, this won't work

myWB.Unprotect
For Each myWS In myWB.Worksheets
myWS.Unprotect
Next myWS
End Sub

I think this is where I have been having problems previously when trying to
join up bits of code. I don't really understand where the code is holding the
information about the browsed for folder.

I'm learning lots trying this out but I don't seem to be getting anywhere -
Help!

Sorry about the long post - I couldn't think how to describe what I had done

Many thanks

--
Deirdre