Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default prompt for path to loop through files

I have the code below and it works fine but I would like it to prompt for the
file path as each month the path changes slightly.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object

Application.ScreenUpdating = False
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder("c:\new_files\Jan")

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Set oFSO = Nothing
Application.ScreenUpdating = True
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default prompt for path to loop through files

VBANovice,

If you are looking for a way to get the folder path as per the user's
selection, then you'll need some API calls. If this is your intention, then
you'll find SHGetPathFromIDListA and SHBrowseForFolderA useful. Chip has
this laid out on his website at the following link if you don't want to use
MSDN to piece the API calls (and their associated structures) together
yourself: http://www.cpearson.com/Excel/BrowseFolder.aspx. If this is not
what you are looking for, then please be more specific as to how you want to
prompt the user and how you intend on using the user's result in your code.

Best,

Matthew Herbert

"VBANovice" wrote:

I have the code below and it works fine but I would like it to prompt for the
file path as each month the path changes slightly.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object

Application.ScreenUpdating = False
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder("c:\new_files\Jan")

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Set oFSO = Nothing
Application.ScreenUpdating = True
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default prompt for path to loop through files

Try out the below

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object

Application.ScreenUpdating = False
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder(GetSelectedFolder)

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

' UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Set oFSO = Nothing
Application.ScreenUpdating = True
End Sub

Function GetSelectedFolder() As String
Dim objShell As Object, objTemp As Object
Set objShell = CreateObject("Shell.Application")
Set objTemp = objShell.BrowseForFolder(0, "Select folder", ssfWINDOWS)
If Not objTemp Is Nothing Then GetSelectedFolder = objTemp.Self.Path
End Function

--
Jacob


"VBANovice" wrote:

I have the code below and it works fine but I would like it to prompt for the
file path as each month the path changes slightly.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object

Application.ScreenUpdating = False
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder("c:\new_files\Jan")

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Set oFSO = Nothing
Application.ScreenUpdating = True
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default prompt for path to loop through files


My method posted below plus a couple of curiosity questions for Jacob.

Why use Shell when there is a built in FileDialog? I have seen it used a lot
and wonder if there is a specific advantage.

What is the variable ssfWINDOWS. It comes up as Compile error: Variable not
defined.

Anyway for the OP this is another option.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fd As FileDialog
Dim myPath As Variant


Application.ScreenUpdating = False
Set fd = Application.FileDialog(msoFileDialogFolderPicker)

With fd

.AllowMultiSelect = False

'Edit following line to where you want to start
.InitialFileName = "c:\new_files"
If .Show Then
myPath = .SelectedItems(1)
Else
MsgBox "User cancelled" & vbLf & vbLf & _
"Processing terminated"
Exit Sub
End If

End With

Set fd = Nothing

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder(myPath)

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file

Set oFSO = Nothing
Set Folder = Nothing

Application.ScreenUpdating = True
End Sub

--
Regards,

OssieMac

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default prompt for path to loop through files

--You can use both methods..The point is its better to have this as a
separate function so that your code doesnt look crowded and can be re-used.
It should have been as below...

--(Sorry both ssfWINDOWS is not specified which should be the initial
folder) The below would restrict the user to have access only to the
specified folder or within the folder specified..

Function GetSelectedFolder2() As String
Dim objShell As Object, objTemp As Object
Set objShell = CreateObject("Shell.Application")
Set objTemp = objShell.BrowseForFolder(0, "Select folder", 0, "d:\")
If Not objTemp Is Nothing Then GetSelectedFolder = objTemp.Self.Path
End Function


Or


Function GetSelectedFolder1(Optional strPath As String) As String
Dim objFldr As FileDialog
Set objFldr = Application.FileDialog(msoFileDialogFolderPicker)
With objFldr
.Title = "Select a folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show < -1 Then GetSelectedFolder = "": Exit Function
GetSelectedFolder = .SelectedItems(1)
End With
Set objFldr = Nothing
End Function

You can use at your convenience...

--
Jacob


"OssieMac" wrote:


My method posted below plus a couple of curiosity questions for Jacob.

Why use Shell when there is a built in FileDialog? I have seen it used a lot
and wonder if there is a specific advantage.

What is the variable ssfWINDOWS. It comes up as Compile error: Variable not
defined.

Anyway for the OP this is another option.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fd As FileDialog
Dim myPath As Variant


Application.ScreenUpdating = False
Set fd = Application.FileDialog(msoFileDialogFolderPicker)

With fd

.AllowMultiSelect = False

'Edit following line to where you want to start
.InitialFileName = "c:\new_files"
If .Show Then
myPath = .SelectedItems(1)
Else
MsgBox "User cancelled" & vbLf & vbLf & _
"Processing terminated"
Exit Sub
End If

End With

Set fd = Nothing

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder(myPath)

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file

Set oFSO = Nothing
Set Folder = Nothing

Application.ScreenUpdating = True
End Sub

--
Regards,

OssieMac



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default prompt for path to loop through files

thanks everyone, lots of good solutions provided. this one worked perfectly
for what I'm trying to do.

"OssieMac" wrote:


My method posted below plus a couple of curiosity questions for Jacob.

Why use Shell when there is a built in FileDialog? I have seen it used a lot
and wonder if there is a specific advantage.

What is the variable ssfWINDOWS. It comes up as Compile error: Variable not
defined.

Anyway for the OP this is another option.

Sub LoopOWC()
Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fd As FileDialog
Dim myPath As Variant


Application.ScreenUpdating = False
Set fd = Application.FileDialog(msoFileDialogFolderPicker)

With fd

.AllowMultiSelect = False

'Edit following line to where you want to start
.InitialFileName = "c:\new_files"
If .Show Then
myPath = .SelectedItems(1)
Else
MsgBox "User cancelled" & vbLf & vbLf & _
"Processing terminated"
Exit Sub
End If

End With

Set fd = Nothing

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder(myPath)

For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path

UploadData

ActiveWorkbook.Close SaveChanges:=True
End If
Next file

Set oFSO = Nothing
Set Folder = Nothing

Application.ScreenUpdating = True
End Sub

--
Regards,

OssieMac

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
prompt a msg in loop code tkraju via OfficeKB.com Excel Programming 5 April 8th 09 05:59 AM
Registry path for links startup prompt XP Excel Programming 1 November 4th 08 01:40 PM
Prompt for Save Path - Use Cell Content for File Name Chris T-M Excel Programming 1 September 10th 08 09:59 PM
Excel XP SP3 and linked files prompt [email protected] Excel Programming 0 July 6th 05 09:46 PM
Excel prompt to save unmodified files Alan Excel Discussion (Misc queries) 3 March 17th 05 06:25 PM


All times are GMT +1. The time now is 09:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"