| Home | Forums | Reviews | Articles | Register |
![]() |
| Thread Tools | Rate Thread |
|
Trish Smith
Guest
Posts: n/a
|
Hi everyone,
I found a previous message about browsing to a folder which was answered but I've not been able to follow the steps that were taken. http://www.microsoft.com/office/comm...7-d241d7a138d4 Now, I'm copying this bit from the thread because I don't understand what's going on, sorry ! I've taken out some bits where it says which code is Chip Pearson's and Barb Reinhardt's 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 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 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 Dave Peterson answered and said this I'd do this: after this line: Path = BrowseFolder("Select A Folder") if right(path,1) <> "\" then path = path & "\" end if Then for the dir statement: myname = dir(mypath & "*.xls") and also to not use VBA variable names but although I've tried to make changes not sure doing right thing myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A Folder") If Right(myPath, 1) <> "\" Then 'If myPath = "" Then myFolderPath = myPath & "\" 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 & myFolderPath Title = "Procedure Completed" MsgBox Prompt, vbInformation, Title End If myName = Dir(myFolderPath & "*.xls") at the moment I get the messagebox saying I haven't selected a folder. Can anyone help please thank you -- Trish |
|
||
|
||||
|
|
|
| |
|
Bob Phillips
Guest
Posts: n/a
|
Something simpler
With Application.FileDialog(msoFileDialogFolderPicker) .AllowMultiSelect = False If .Show = -1 Then MsgBox .SelectedItems(1) End If End With -- __________________________________ HTH Bob "Trish Smith" <(E-Mail Removed)> wrote in message news:CFD74419-55C6-404E-A64F-(E-Mail Removed)... > Hi everyone, > > I found a previous message about browsing to a folder which was answered > but > I've not been able to follow the steps that were taken. > http://www.microsoft.com/office/comm...7-d241d7a138d4 > > Now, I'm copying this bit from the thread because I don't understand > what's > going on, sorry ! I've taken out some bits where it says which code is > Chip > Pearson's and Barb Reinhardt's > > 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 > > 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 > > 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 > > > Dave Peterson answered and said this > I'd do this: > > after this line: > Path = BrowseFolder("Select A Folder") > if right(path,1) <> "\" then > path = path & "\" > end if > > Then for the dir statement: > > myname = dir(mypath & "*.xls") > > and also to not use VBA variable names but although I've tried to make > changes not sure doing right thing > > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A > Folder") > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > myFolderPath = myPath & "\" > 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 & myFolderPath > Title = "Procedure Completed" > MsgBox Prompt, vbInformation, Title > End If > > myName = Dir(myFolderPath & "*.xls") > > at the moment I get the messagebox saying I haven't selected a folder. > > Can anyone help please > > thank you > > -- > Trish |
|
||
|
||||
|
Trish Smith
Guest
Posts: n/a
|
Hi Bob,
I'm afraid something simpler just confused me - just a beginner! Which bit does it replace or add to and how do I loop through files in the folder thereafter. I haven't even tried it out - chicken that I am! Thank you :-) -- Trish "Bob Phillips" wrote: > Something simpler > > With Application.FileDialog(msoFileDialogFolderPicker) > > .AllowMultiSelect = False > If .Show = -1 Then > > MsgBox .SelectedItems(1) > End If > End With > > > -- > __________________________________ > HTH > > Bob > > "Trish Smith" <(E-Mail Removed)> wrote in message > news:CFD74419-55C6-404E-A64F-(E-Mail Removed)... > > Hi everyone, > > > > I found a previous message about browsing to a folder which was answered > > but > > I've not been able to follow the steps that were taken. > > http://www.microsoft.com/office/comm...7-d241d7a138d4 > > > > Now, I'm copying this bit from the thread because I don't understand > > what's > > going on, sorry ! I've taken out some bits where it says which code is > > Chip > > Pearson's and Barb Reinhardt's > > > > 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 > > > > 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 > > > > 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 > > > > > > Dave Peterson answered and said this > > I'd do this: > > > > after this line: > > Path = BrowseFolder("Select A Folder") > > if right(path,1) <> "\" then > > path = path & "\" > > end if > > > > Then for the dir statement: > > > > myname = dir(mypath & "*.xls") > > > > and also to not use VBA variable names but although I've tried to make > > changes not sure doing right thing > > > > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A > > Folder") > > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > > myFolderPath = myPath & "\" > > 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 & myFolderPath > > Title = "Procedure Completed" > > MsgBox Prompt, vbInformation, Title > > End If > > > > myName = Dir(myFolderPath & "*.xls") > > > > at the moment I get the messagebox saying I haven't selected a folder. > > > > Can anyone help please > > > > thank you > > > > -- > > Trish > > > |
|
||
|
||||
|
Dave Peterson
Guest
Posts: n/a
|
That thread could be difficult to follow.
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 Sub test() Dim myWB As Workbook Dim AutoSecurity As MsoAutomationSecurity 'Dim mymyPath As String Dim myName As String Dim myPath As String Dim Prompt As String Dim Title As String myPath = BrowseFolder("Select A Folder") If myPath = "" 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 myPath:" & vbNewLine & myPath Title = "Procedure Completed" MsgBox Prompt, vbInformation, Title If Right(myPath, 1) <> "\" Then myPath = myPath & "\" End If myName = Dir(myPath & "*.xls") If myName <> "" Then Do Debug.Print myName AutoSecurity = Application.AutomationSecurity Application.AutomationSecurity = msoAutomationSecurityLow Set myWB = Workbooks.Open(myPath & myName) Call UnprotectWB(myWB) Application.AutomationSecurity = AutoSecurity On Error Resume Next myName = Dir ' Get next entry. If myName = "" Then Exit Do End If Loop End If End If 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 Trish Smith wrote: > > Hi everyone, > > I found a previous message about browsing to a folder which was answered but > I've not been able to follow the steps that were taken. > http://www.microsoft.com/office/comm...7-d241d7a138d4 > > Now, I'm copying this bit from the thread because I don't understand what's > going on, sorry ! I've taken out some bits where it says which code is Chip > Pearson's and Barb Reinhardt's > > 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 > > 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 > > 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 > > Dave Peterson answered and said this > I'd do this: > > after this line: > Path = BrowseFolder("Select A Folder") > if right(path,1) <> "\" then > path = path & "\" > end if > > Then for the dir statement: > > myname = dir(mypath & "*.xls") > > and also to not use VBA variable names but although I've tried to make > changes not sure doing right thing > > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A > Folder") > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > myFolderPath = myPath & "\" > 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 & myFolderPath > Title = "Procedure Completed" > MsgBox Prompt, vbInformation, Title > End If > > myName = Dir(myFolderPath & "*.xls") > > at the moment I get the messagebox saying I haven't selected a folder. > > Can anyone help please > > thank you > > -- > Trish -- Dave Peterson |
|
||
|
||||
|
Barb Reinhardt
Guest
Posts: n/a
|
It's not my code. I believe it's Chips.
Barb Reinhardt "Trish Smith" wrote: > Hi everyone, > > I found a previous message about browsing to a folder which was answered but > I've not been able to follow the steps that were taken. > http://www.microsoft.com/office/comm...7-d241d7a138d4 > > Now, I'm copying this bit from the thread because I don't understand what's > going on, sorry ! I've taken out some bits where it says which code is Chip > Pearson's and Barb Reinhardt's > > 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 > > 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 > > 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 > > > Dave Peterson answered and said this > I'd do this: > > after this line: > Path = BrowseFolder("Select A Folder") > if right(path,1) <> "\" then > path = path & "\" > end if > > Then for the dir statement: > > myname = dir(mypath & "*.xls") > > and also to not use VBA variable names but although I've tried to make > changes not sure doing right thing > > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A > Folder") > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > myFolderPath = myPath & "\" > 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 & myFolderPath > Title = "Procedure Completed" > MsgBox Prompt, vbInformation, Title > End If > > myName = Dir(myFolderPath & "*.xls") > > at the moment I get the messagebox saying I haven't selected a folder. > > Can anyone help please > > thank you > > -- > Trish |
|
||
|
||||
|
Bob Phillips
Guest
Posts: n/a
|
Where you call the BrowseFunction folder, don't bother, replace that call
with the code I gave. Try these things, you can't do any harm as long as you save your workbook first, and don't save it again if something happens. -- __________________________________ HTH Bob "Trish Smith" <(E-Mail Removed)> wrote in message news:BB310B31-F0CA-44EA-A8CE-(E-Mail Removed)... > Hi Bob, > > I'm afraid something simpler just confused me - just a beginner! > > Which bit does it replace or add to and how do I loop through files in the > folder thereafter. > > I haven't even tried it out - chicken that I am! > > Thank you :-) > > > > > -- > Trish > > > "Bob Phillips" wrote: > >> Something simpler >> >> With Application.FileDialog(msoFileDialogFolderPicker) >> >> .AllowMultiSelect = False >> If .Show = -1 Then >> >> MsgBox .SelectedItems(1) >> End If >> End With >> >> >> -- >> __________________________________ >> HTH >> >> Bob >> >> "Trish Smith" <(E-Mail Removed)> wrote in message >> news:CFD74419-55C6-404E-A64F-(E-Mail Removed)... >> > Hi everyone, >> > >> > I found a previous message about browsing to a folder which was >> > answered >> > but >> > I've not been able to follow the steps that were taken. >> > http://www.microsoft.com/office/comm...7-d241d7a138d4 >> > >> > Now, I'm copying this bit from the thread because I don't understand >> > what's >> > going on, sorry ! I've taken out some bits where it says which code is >> > Chip >> > Pearson's and Barb Reinhardt's >> > >> > 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 >> > >> > 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 >> > >> > 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 >> > >> > >> > Dave Peterson answered and said this >> > I'd do this: >> > >> > after this line: >> > Path = BrowseFolder("Select A Folder") >> > if right(path,1) <> "\" then >> > path = path & "\" >> > end if >> > >> > Then for the dir statement: >> > >> > myname = dir(mypath & "*.xls") >> > >> > and also to not use VBA variable names but although I've tried to make >> > changes not sure doing right thing >> > >> > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select >> > A >> > Folder") >> > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then >> > myFolderPath = myPath & "\" >> > 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 & myFolderPath >> > Title = "Procedure Completed" >> > MsgBox Prompt, vbInformation, Title >> > End If >> > >> > myName = Dir(myFolderPath & "*.xls") >> > >> > at the moment I get the messagebox saying I haven't selected a folder. >> > >> > Can anyone help please >> > >> > thank you >> > >> > -- >> > Trish >> >> >> |
|
||
|
||||
|
Trish Smith
Guest
Posts: n/a
|
Sorry Bob,
Still over my head, I don't understand what the code is doing enough to work out what's doing what. I'm at the stage where I can make little changes but a lot of the terminology and logic escapes me. I can see that this selects a folder but how do I then tell it to use that folder and loop through the files in it (and then do something in each file eg unprotect sheet). I was brave enough to run it this time but didn't get beyond selecting the folder. Thanks for your help :-) -- Trish "Bob Phillips" wrote: > Where you call the BrowseFunction folder, don't bother, replace that call > with the code I gave. > > Try these things, you can't do any harm as long as you save your workbook > first, and don't save it again if something happens. > > -- > __________________________________ > HTH > > Bob > > "Trish Smith" <(E-Mail Removed)> wrote in message > news:BB310B31-F0CA-44EA-A8CE-(E-Mail Removed)... > > Hi Bob, > > > > I'm afraid something simpler just confused me - just a beginner! > > > > Which bit does it replace or add to and how do I loop through files in the > > folder thereafter. > > > > I haven't even tried it out - chicken that I am! > > > > Thank you :-) > > > > > > > > > > -- > > Trish > > > > > > "Bob Phillips" wrote: > > > >> Something simpler > >> > >> With Application.FileDialog(msoFileDialogFolderPicker) > >> > >> .AllowMultiSelect = False > >> If .Show = -1 Then > >> > >> MsgBox .SelectedItems(1) > >> End If > >> End With > >> > >> > >> -- > >> __________________________________ > >> HTH > >> > >> Bob > >> > >> "Trish Smith" <(E-Mail Removed)> wrote in message > >> news:CFD74419-55C6-404E-A64F-(E-Mail Removed)... > >> > Hi everyone, > >> > > >> > I found a previous message about browsing to a folder which was > >> > answered > >> > but > >> > I've not been able to follow the steps that were taken. > >> > http://www.microsoft.com/office/comm...7-d241d7a138d4 > >> > > >> > Now, I'm copying this bit from the thread because I don't understand > >> > what's > >> > going on, sorry ! I've taken out some bits where it says which code is > >> > Chip > >> > Pearson's and Barb Reinhardt's > >> > > >> > 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 > >> > > >> > 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 > >> > > >> > 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 > >> > > >> > > >> > Dave Peterson answered and said this > >> > I'd do this: > >> > > >> > after this line: > >> > Path = BrowseFolder("Select A Folder") > >> > if right(path,1) <> "\" then > >> > path = path & "\" > >> > end if > >> > > >> > Then for the dir statement: > >> > > >> > myname = dir(mypath & "*.xls") > >> > > >> > and also to not use VBA variable names but although I've tried to make > >> > changes not sure doing right thing > >> > > >> > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select > >> > A > >> > Folder") > >> > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > >> > myFolderPath = myPath & "\" > >> > 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 & myFolderPath > >> > Title = "Procedure Completed" > >> > MsgBox Prompt, vbInformation, Title > >> > End If > >> > > >> > myName = Dir(myFolderPath & "*.xls") > >> > > >> > at the moment I get the messagebox saying I haven't selected a folder. > >> > > >> > Can anyone help please > >> > > >> > thank you > >> > > >> > -- > >> > Trish > >> > >> > >> > > > |
|
||
|
||||
|
Trish Smith
Guest
Posts: n/a
|
Hi Dave,
Definitely difficult to follow:-) This worked Yaayy thank you -- Trish "Dave Peterson" wrote: > That thread could be difficult to follow. > > 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 > > Sub test() > > Dim myWB As Workbook > Dim AutoSecurity As MsoAutomationSecurity > 'Dim mymyPath As String > Dim myName As String > Dim myPath As String > Dim Prompt As String > Dim Title As String > > myPath = BrowseFolder("Select A Folder") > If myPath = "" 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 myPath:" & vbNewLine & myPath > Title = "Procedure Completed" > MsgBox Prompt, vbInformation, Title > > If Right(myPath, 1) <> "\" Then > myPath = myPath & "\" > End If > > myName = Dir(myPath & "*.xls") > > If myName <> "" Then > Do > Debug.Print myName > > AutoSecurity = Application.AutomationSecurity > Application.AutomationSecurity = msoAutomationSecurityLow > Set myWB = Workbooks.Open(myPath & myName) > Call UnprotectWB(myWB) > Application.AutomationSecurity = AutoSecurity > On Error Resume Next > > myName = Dir ' Get next entry. > If myName = "" Then > Exit Do > End If > Loop > End If > End If > > 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 > > > > Trish Smith wrote: > > > > Hi everyone, > > > > I found a previous message about browsing to a folder which was answered but > > I've not been able to follow the steps that were taken. > > http://www.microsoft.com/office/comm...7-d241d7a138d4 > > > > Now, I'm copying this bit from the thread because I don't understand what's > > going on, sorry ! I've taken out some bits where it says which code is Chip > > Pearson's and Barb Reinhardt's > > > > 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 > > > > 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 > > > > 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 > > > > Dave Peterson answered and said this > > I'd do this: > > > > after this line: > > Path = BrowseFolder("Select A Folder") > > if right(path,1) <> "\" then > > path = path & "\" > > end if > > > > Then for the dir statement: > > > > myname = dir(mypath & "*.xls") > > > > and also to not use VBA variable names but although I've tried to make > > changes not sure doing right thing > > > > myPath = BrowseFolder("Select A Folder") 'myPath = BrowseFolder("Select A > > Folder") > > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then > > myFolderPath = myPath & "\" > > 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 & myFolderPath > > Title = "Procedure Completed" > > MsgBox Prompt, vbInformation, Title > > End If > > > > myName = Dir(myFolderPath & "*.xls") > > > > at the moment I get the messagebox saying I haven't selected a folder. > > > > Can anyone help please > > > > thank you > > > > -- > > Trish > > -- > > Dave Peterson > |
|
||
|
||||
|
Bob Phillips
Guest
Posts: n/a
|
Presumably, you already have some code to process each file? Integrate it
with that. -- __________________________________ HTH Bob "Trish Smith" <(E-Mail Removed)> wrote in message news:5A469D80-12BE-4AC5-BA11-(E-Mail Removed)... > Sorry Bob, > > Still over my head, I don't understand what the code is doing enough to > work > out what's doing what. I'm at the stage where I can make little changes > but a > lot of the terminology and logic escapes me. > > I can see that this selects a folder but how do I then tell it to use that > folder and loop through the files in it (and then do something in each > file > eg unprotect sheet). > > I was brave enough to run it this time but didn't get beyond selecting the > folder. > > Thanks for your help :-) > -- > Trish > > > "Bob Phillips" wrote: > >> Where you call the BrowseFunction folder, don't bother, replace that call >> with the code I gave. >> >> Try these things, you can't do any harm as long as you save your workbook >> first, and don't save it again if something happens. >> >> -- >> __________________________________ >> HTH >> >> Bob >> >> "Trish Smith" <(E-Mail Removed)> wrote in message >> news:BB310B31-F0CA-44EA-A8CE-(E-Mail Removed)... >> > Hi Bob, >> > >> > I'm afraid something simpler just confused me - just a beginner! >> > >> > Which bit does it replace or add to and how do I loop through files in >> > the >> > folder thereafter. >> > >> > I haven't even tried it out - chicken that I am! >> > >> > Thank you :-) >> > >> > >> > >> > >> > -- >> > Trish >> > >> > >> > "Bob Phillips" wrote: >> > >> >> Something simpler >> >> >> >> With Application.FileDialog(msoFileDialogFolderPicker) >> >> >> >> .AllowMultiSelect = False >> >> If .Show = -1 Then >> >> >> >> MsgBox .SelectedItems(1) >> >> End If >> >> End With >> >> >> >> >> >> -- >> >> __________________________________ >> >> HTH >> >> >> >> Bob >> >> >> >> "Trish Smith" <(E-Mail Removed)> wrote in message >> >> news:CFD74419-55C6-404E-A64F-(E-Mail Removed)... >> >> > Hi everyone, >> >> > >> >> > I found a previous message about browsing to a folder which was >> >> > answered >> >> > but >> >> > I've not been able to follow the steps that were taken. >> >> > http://www.microsoft.com/office/comm...7-d241d7a138d4 >> >> > >> >> > Now, I'm copying this bit from the thread because I don't >> >> > understand >> >> > what's >> >> > going on, sorry ! I've taken out some bits where it says which code >> >> > is >> >> > Chip >> >> > Pearson's and Barb Reinhardt's >> >> > >> >> > 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 >> >> > >> >> > 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 >> >> > >> >> > 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 >> >> > >> >> > >> >> > Dave Peterson answered and said this >> >> > I'd do this: >> >> > >> >> > after this line: >> >> > Path = BrowseFolder("Select A Folder") >> >> > if right(path,1) <> "\" then >> >> > path = path & "\" >> >> > end if >> >> > >> >> > Then for the dir statement: >> >> > >> >> > myname = dir(mypath & "*.xls") >> >> > >> >> > and also to not use VBA variable names but although I've tried to >> >> > make >> >> > changes not sure doing right thing >> >> > >> >> > myPath = BrowseFolder("Select A Folder") 'myPath = >> >> > BrowseFolder("Select >> >> > A >> >> > Folder") >> >> > If Right(myPath, 1) <> "\" Then 'If myPath = "" Then >> >> > myFolderPath = myPath & "\" >> >> > 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 & >> >> > myFolderPath >> >> > Title = "Procedure Completed" >> >> > MsgBox Prompt, vbInformation, Title >> >> > End If >> >> > >> >> > myName = Dir(myFolderPath & "*.xls") >> >> > >> >> > at the moment I get the messagebox saying I haven't selected a >> >> > folder. >> >> > >> >> > Can anyone help please >> >> > >> >> > thank you >> >> > >> >> > -- >> >> > Trish >> >> >> >> >> >> >> >> >> |
|
||
|
||||
|
|
|
| |
![]() |
| Thread Tools | |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Outlook 2003: selecting a mail folder automaticallyhighlights/selects/opens the first message in that folder | vtyau.online@gmail.com | Microsoft Outlook | 2 | 5th Mar 2008 10:14 PM |
| Selecting folder in Favorites highlight folder in All Mail Folder. | =?Utf-8?B?cmljaGdvZmY=?= | Microsoft Outlook Discussion | 0 | 7th Jan 2005 11:13 PM |
| How to browse folder starting from my application folder? | =?Utf-8?B?RmVpIExp?= | Microsoft C# .NET | 3 | 2nd Nov 2004 03:06 PM |
| Browse Folder ( Folder Options ) won't change after new selection | marca | Windows XP Security | 0 | 14th Jul 2004 05:50 AM |
| Browse Folder dialog with option to make New Folder | Bradley C. Hammerstrom | Microsoft Access Getting Started | 2 | 6th Jan 2004 07:33 AM |
Powered by vBulletin®. Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2010, Crawlability, Inc. |




