PDA

View Full Version : Solved: "File New" from a specific folder



Gavint
09-05-2006, 07:58 PM
Is there a way to replicate or modify the "File New" feature in Word to look only at a specific folder?

In Word's Workgroup template directory I have around 12 subfolders (all required) and I would like to have a macro (or most likely 12) that will replicate the creation of a new document from templates in a specified folder.

Alternatively, with the current setup I need to click on the "More" tab to show extra folders. This actually displays all of the subfolders folders nicely. Perhaps there is away to replicate this view in a simple macro?

fumei
09-05-2006, 08:50 PM
File > New will look in the folders defined by the settings of File Locations. User templates, and Workgroup Templates. As far as I know, you can not get it to open any other folder.

However, that may not really be the issue.
I would like to have a macro (or most likely 12) that will replicate the creation of a new document from templates in a specified folder.What exactly do you measn by that?? What does replicate the creation fo a new document from templates.

File > New gives you a choice. What are you really trying to do?

If you have that many templates, and choosing which one is a problem, I would suggest a userform. Build the userform with comboboxes listing the various templates. The user selects which templates, and the OK button makes the new document from the selected template. Rewrite the File > New code to display the userform.

In Normal.dot put this in a code module:Sub FileNew()
userform1.Show
End SubThis is assuming the userform is named userform1 - which of course it should NOT be. But assuming it is (because it is the default name), then File > New would display the userform. It would NOT make a new document, or display the standard dialog.

Essentially, what I am saying is, if this is a problem, build your own dialog (userform) listing what ever the heck you want. You can fill comboboxes with the template names. Say 12 comboboxes with each one listing the templates from your folders.

Other than that, I am not sure what to suggest.

Gavint
09-05-2006, 10:50 PM
Thanks for the quick reply.


What exactly do you measn by that?? What does replicate the creation fo a new document from templates.

File > New gives you a choice. What are you really trying to do?

I think you hit the nail on the head - I don't need to replicate "File New" exactly, just the functionality. That is, there are a lot of templates and I want an easy way of making them available in an orderly fashion.

I am confident creating forms and like the idea of using a combo style box - how though do you populate the window with the list of available templates?

Considerations are 1. there is a lot (maybe 100 plus templates for some folders) and 2. the list may change

Gavint
09-06-2006, 02:32 AM
I'm going to mark this one as solved.

Fumei put me on the right track with user forms and I found this little gem below when searching for info on populating listboxs.

This code populates a list box with all of the templates in any given folder and with a bit of extra code the user selection can be launched as a new document.

:bow:

Is this what you are trying to do?

Create a UserForm with two List Boxes and one Command Button. ListBox2 will hold the paths and can be hidden. ListBox1 will show the file names for the user to select. Add this code to the UserForm.


Option Explicit

Private Sub CommandButton1_Click()

On Error Resume Next
Documents.Open FileName:=Me.ListBox2.List(Me.ListBox1.ListIndex)
On Error GoTo 0
Unload Me

End Sub


Create a Module and add this code.

Option Explicit

Sub ListDocuments()

Dim Col As Collection
Dim StartFld As Folder
Dim FSO As FileSystemObject
Dim i As Long
Dim Path As String
Dim Files As Collection
Dim Paths As Collection
Dim FileName As String

'This is the startup folder
'Change this folder to whatever root folder you want
Path = "C:\MyDir"

Set FSO = New FileSystemObject
Set Col = New Collection
Set Files = New Collection
Set Paths = New Collection
Set StartFld = FSO.GetFolder(Path)

'Create a list of folders
GetFolder Col, StartFld

For i = 1 To Col.Count
Path = Col(i)
FileName = Dir(Path & "\*.doc", vbNormal)
Do Until FileName = ""
Files.Add FileName
Paths.Add Path & "\" & FileName
FileName = Dir()
Loop
FileName = Dir(Path & "\*.dot", vbNormal)
Do Until FileName = ""
Files.Add Replace(FileName, Path & "\", "", 1)
Paths.Add FileName
FileName = Dir()
Loop
Next i
Load UserForm1
With UserForm1
For i = 1 To Files.Count
.ListBox1.AddItem Files(i)
.ListBox2.AddItem Paths(i)
Next i
.Show
End With

End Sub

Sub GetFolder(ByRef Col As Collection, ByVal Fld As Folder)

Dim SubF As Folder

Col.Add Fld.Path & IIf(Fld.IsRootFolder, "", Application.PathSeparator)
For Each SubF In Fld.SubFolders
GetFolder Col, SubF
Next SubF
Set SubF = Nothing

End Sub


Set a Reference to Microsoft Scripting Runtime (Tools | References).

Change this:

Path = "C:\MyDir"

To the root folder to be searched. All sub folders will be searched for any .doc or .dot files.

Run the macro ListDocuments. From Word select Tools | Macro | Macros... or you can attach this to a toolbar button or some other control.

Refer to the attachment for more information.

fumei
09-12-2006, 09:05 AM
Normally I would not suggest doing something like this. It seems a fair bit of work for a return that does not seem to match. However, in your case - with so many files/folders - yup, it seems reasonable.

Gavint
09-12-2006, 04:14 PM
Actually, the end result I put together is perfect for what I need. I only utilized the part of the code I needed and when combined with a custom form I get a file launching utility that is very useful.

The user form itself has a series of radio buttons (representing the various folders that the templates live in), a series of checkboxes (allowing filtering of the various template types (eg fax, letter, form etc) and even a keyword search. The resulting files appearing in the list box for easy launching.

Sub Generate()

Dim Col As Collection
Dim StartFld As FOLDER
Dim FSO As FileSystemObject
Dim i As Long
Dim Path As String
Dim Files As Collection
Dim Paths As Collection
Dim FileName As String
Dim Keyword As String

'set startup folders
If frmIP.optD = True Then
Path = "M:\Templates\Designs"
ElseIf frmIP.optFD = True Then
Path = "M:\Templates\Foreign Designs"
ElseIf frmIP.optP = True Then
Path = "M:\Templates\Patents"
ElseIf frmIP.optFP = True Then
Path = "M:\Templates\Foreign Patents"
ElseIf frmIP.optT = True Then
Path = "M:\Templates\Trade Marks"
ElseIf frmIP.optFT = True Then
Path = "M:\Templates\Foreign Trade Marks"
ElseIf frmIP.optG = True Then
Path = "M:\Templates\General"
ElseIf frmIP.optO = True Then
Path = "M:\Templates\Other IP"
End If

' define attributes
Keyword = frmIP.TextBox1
Keyword = "*" & Keyword & "*"

Set FSO = New FileSystemObject
Set Col = New Collection
Set Files = New Collection
Set Paths = New Collection
Set StartFld = FSO.GetFolder(Path)

'Create a list of folders
GetFolder Col, StartFld

For i = 1 To Col.Count
Path = Col(i)
'Miscellaneous
If frmIP.CheckBoxM = True Then
FileName = Dir(Path & "\*_Assign_*.dot" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add FileName
Paths.Add Path & "\" & FileName
FileName = Dir()
Loop
FileName = Dir(Path & "\*_Cvr_*.dot" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add FileName
Paths.Add Path & "\" & FileName
FileName = Dir()
Loop
FileName = Dir(Path & "\*_Lbl_*.dot" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add FileName
Paths.Add Path & "\" & FileName
FileName = Dir()
Loop
FileName = Dir(Path & "\*_Info_*.dot" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add FileName
Paths.Add Path & "\" & FileName
FileName = Dir()
Loop
End If
'Forms
If frmIP.CheckBoxF = True Then
FileName = Dir(Path & "\*_Frm_*" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add Replace(FileName, Path & "\", "", 1)
Paths.Add FileName
FileName = Dir()
Loop
End If
'IPO
If frmIP.CheckBoxI = True Then
FileName = Dir(Path & "*_IPO_*" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add Replace(FileName, Path & "\", "", 1)
Paths.Add FileName
FileName = Dir()
Loop
End If
'Letters
If frmIP.CheckBoxL = True Then
FileName = Dir(Path & "\*_Let_*" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add Replace(FileName, Path & "\", "", 1)
Paths.Add FileName
FileName = Dir()
Loop
End If
'Sets
If frmIP.CheckBoxFS = True Then
FileName = Dir(Path & "\*_Set_*" & Keyword, vbNormal)
Do Until FileName = ""
Files.Add Replace(FileName, Path & "\", "", 1)
Paths.Add FileName
FileName = Dir()
Loop
End If
Next i
'Load frmIP
With frmIP
For i = 1 To Files.Count
.ListBox1.AddItem Files(i)
.ListBox2.AddItem Paths(i)
Next i
' .Show
End With

End Sub

Sub GetFolder(ByRef Col As Collection, ByVal Fld As FOLDER)

Dim SubF As FOLDER

Col.Add Fld.Path & IIf(Fld.IsRootFolder, "", Application.PathSeparator)
For Each SubF In Fld.SubFolders
GetFolder Col, SubF
Next SubF
Set SubF = Nothing

End Sub

fumei
09-13-2006, 01:13 PM
Hi.

Thanks for posting your final result. Good on you.