Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() I have this code and I want it to open a file in a directory that I choose (it does that already), and then I wunt it to create a sub-folder in the folder I chose with the original folders name plus a number. (example) I choose a folder named project_test then it converts the file, in the folder, detail.htm to detail.htm.wk4. Then it creates the sub-folder project_test0001 then saves detail.htm.wk4 in it. Then the next time I run ConvertFiles ,when it creates the sub-folder, it creates project_test0002, and when it has reached the tenth time it creates it as project_test0010 etc. Code: -------------------- Sub ConvertFiles() ' ' Application.DisplayAlerts = False ' Dim vrtSelectedItem As Variant Dim FileToOpen As String 'Declare a variable as a FileDialog object. Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) fd.Title = "Cool Application" fd.InitialFileName = "Working" If fd.Show = -1 Then For a = 1 To fd.SelectedItems.Count MsgBox fd.SelectedItems(a) Dim NextFile As String NextFile = Dir(fd.SelectedItems(a) & "\*detail*.htm") Do While NextFile < "" Workbooks.Open Filename:=NextFile ActiveWorkbook.SaveAs Filename:= _ NextFile & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False ActiveWorkbook.Save ActiveWorkbook.Close NextFile = Dir() Loop Next End If Application.DisplayAlerts = True End Sub -------------------- -- tim64 ------------------------------------------------------------------------ tim64's Profile: http://www.excelforum.com/member.php...o&userid=23295 View this thread: http://www.excelforum.com/showthread...hreadid=379459 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Backup the workbooks before testing this macro Excel version I have is 2000 so couldnot test the macro Sub ConvertFiles() ' ' Application.DisplayAlerts = False Dim T As Integer ' T = 1 Dim vrtSelectedItem As Variant Dim FileToOpen As String 'Declare a variable as a FileDialog object. Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) fd.Title = "Cool Application" fd.InitialFileName = "Working" If fd.Show = -1 Then For a = 1 To fd.SelectedItems.Count MsgBox fd.SelectedItems(a) Dim NextFile As String T = 1 NextFile = Dir(fd.SelectedItems(a) & "\*detail*.htm") Do While NextFile < "" Workbooks.Open Filename:=NextFile If T < 10 Then MkDir ActiveWorkbook.Path & "\ PROJECT_TEST000" & T ActiveWorkbook.SaveAs Filename:= _ ActiveWorkbook.Path & "\ PROJECT_TEST000" & T & "\" & ActiveWorkbook.Name & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False Else MkDir ActiveWorkbook.Path & "\ PROJECT_TEST00" & T ActiveWorkbook.SaveAs Filename:= _ ActiveWorkbook.Path & "\ PROJECT_TEST00" & T & "\" & ActiveWorkbook.Name & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False End If ActiveWorkbook.Save ActiveWorkbook.Close NextFile = Dir() T = T + 1 Loop Next End If Application.DisplayAlerts = True End Sub -- anilsolipuram ------------------------------------------------------------------------ anilsolipuram's Profile: http://www.excelforum.com/member.php...o&userid=16271 View this thread: http://www.excelforum.com/showthread...hreadid=379459 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() it looks good, but I don't have a folder named project_test that wa for an example, I'll have hundreds of different folders I'll use thi in and, I'll have between 1 and 10 files in then with the name "detail in it and, I wunt them all to go to the new folder that is created. Als I'll probly have hundreds of folders created because I'm going to us this alot so it will be like project_test1839(example) eventually. Sorry I didn't clerify that earlie -- tim6 ----------------------------------------------------------------------- tim64's Profile: http://www.excelforum.com/member.php...fo&userid=2329 View this thread: http://www.excelforum.com/showthread.php?threadid=37945 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Backup your workbooks before using this macro Try this macro Sub ConvertFiles() ' ' Application.DisplayAlerts = False Dim T As Integer ' T = 1 Dim vrtSelectedItem As Variant Dim FileToOpen As String 'Declare a variable as a FileDialog object. Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) fd.Title = "Cool Application" fd.InitialFileName = "Working" If fd.Show = -1 Then For a = 1 To fd.SelectedItems.Count MsgBox fd.SelectedItems(a) Dim NextFile As String T = 1 NextFile = Dir(fd.SelectedItems(a) & "\*detail*.htm") Dim ar1, ar2 As Variant ar1 = Split(fd.SelectedItems(a), "\") ar2 = ar1(UBound(ar1)) Do While NextFile < "" Workbooks.Open Filename:=NextFile If T < 10 Then MkDir ActiveWorkbook.Path & "\" & ar2 & "000" & T ActiveWorkbook.SaveAs Filename:= _ ActiveWorkbook.Path & "\" & ar2 & "000" & T & "\" & ActiveWorkbook.Name & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False Else MkDir ActiveWorkbook.Path & "\" & ar2 & "00" & T ActiveWorkbook.SaveAs Filename:= _ ActiveWorkbook.Path & "\" & ar2 & "00" & T & "\" & ActiveWorkbook.Name & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False End If ActiveWorkbook.Save ActiveWorkbook.Close NextFile = Dir() T = T + 1 Loop Next End If Application.DisplayAlerts = True End Sub -- anilsolipuram ------------------------------------------------------------------------ anilsolipuram's Profile: http://www.excelforum.com/member.php...o&userid=16271 View this thread: http://www.excelforum.com/showthread...hreadid=379459 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() thats almost to what I want but, when ConvertFiles runs I only want i to create one sub-folder per run and I want all the converted files t go into it. (example) I have four files detail1.htm, detail2.htm detail3.htm, detail4.htm and, in the folder project_test there ar sub-folders project_test0001 - project_test3829. So when I ru ConvertFiles it converts the four files then it saves them to th folder project_test3830 (after it makes it). So the next time I ru ConvertFiles it saves the files in project_test3831 etc -- tim6 ----------------------------------------------------------------------- tim64's Profile: http://www.excelforum.com/member.php...fo&userid=2329 View this thread: http://www.excelforum.com/showthread.php?threadid=37945 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() I added 1 more function to the end Sub ConvertFiles() ' ' Dim temp, temp1, temp2, temp3, temp4 As Variant Application.DisplayAlerts = False Dim t As Integer ' t = 1 Dim vrtSelectedItem As Variant Dim FileToOpen As String 'Declare a variable as a FileDialog object. Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) fd.Title = "Cool Application" fd.InitialFileName = "Working" If fd.Show = -1 Then For a = 1 To fd.SelectedItems.Count Dim NextFile As String t = 1 NextFile = Dir(fd.SelectedItems(a) & "\*detail*.htm") Dim ar1, ar2 As Variant ar1 = Split(fd.SelectedItems(a), "\") ar2 = ar1(UBound(ar1)) temp = last_filename(ActiveWorkbook.Path & "\", ar2) temp1 = Split(temp, ar2) temp2 = CInt(temp1(1)) temp3 = temp1 + 1 temp4 = Format(temp3, "000#") MkDir ActiveWorkbook.Path & "\" & ar2 & temp4 Do While NextFile < "" Workbooks.Open Filename:=NextFile ActiveWorkbook.SaveAs Filename:= _ ActiveWorkbook.Path & "\" & ar2 & temp4 & "\" & ActiveWorkbook.Name & ".wk4", _ FileFormat:=xlWK4, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False ActiveWorkbook.Save ActiveWorkbook.Close NextFile = Dir() t = t + 1 Loop Next End If Application.DisplayAlerts = True End Sub Function last_filename(p As Variant, ar2 As Variant) Dim t1 As Variant t = Dir(p & ar2 & "*.*", vbDirectory) While t < "" t = Dir() If (t < "") Then t1 = t End If Wend If t1 = "" Then t1 = t End If last_filename = t1 End Function -- anilsolipuram ------------------------------------------------------------------------ anilsolipuram's Profile: http://www.excelforum.com/member.php...o&userid=16271 View this thread: http://www.excelforum.com/showthread...hreadid=379459 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
open and save an entire folder | Excel Discussion (Misc queries) | |||
can“t change folder when open or save | Excel Discussion (Misc queries) | |||
Need code to save file to new folder, erase from old folder | Excel Discussion (Misc queries) | |||
"Save As" folder -- can I default this to the same folder as origi | Excel Discussion (Misc queries) | |||
I like to open a folder,auto print,save then close | Excel Programming |