PDA

View Full Version : Template select form



scrapion
02-24-2010, 09:48 AM
I am looking for coding that will enable word to start with a Form that allows me to select one of a number of templates I have in a directory.

I have used a msgbox code that asks if i want to chose a letter in a slecified directory, however, it would be more efficient for me to have a form that is linked to a number of word templated in a directory.

Any ideas?:think:

lucas
02-24-2010, 10:09 AM
What if you combine all your tempates and just delete all except the one you select.

See attachment provided by Gerry(fumei).

scrapion
02-24-2010, 10:32 AM
Thanks Lucas.

What I am looking for is a method to invoke a Reception Window (Form) that will display a number of options that open diferent Templates from a given directory.

Thank very much for your input.

lucas
02-24-2010, 10:45 AM
Isn't that what the native select templates dialog does?

Why reinvent the wheel?

lucas
02-24-2010, 11:17 AM
You could use a dialog like this:

Option Explicit

Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OpenFileName) As Long

Private Type OpenFileName
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Function BrowseForFile(sInitDir As String, _
Optional ByVal sFileFilters As String, _
Optional sTitle As String = "Open File", _
Optional lParentHwnd As Long) As String

Dim tFileBrowse As OpenFileName
Const clMaxLen As Long = 254

tFileBrowse.lStructSize = Len(tFileBrowse)

'Replace friendly deliminators with nulls
sFileFilters = Replace(sFileFilters, "|", vbNullChar)
sFileFilters = Replace(sFileFilters, ";", vbNullChar)
If Right$(sFileFilters, 1) <> vbNullChar Then
'Add final delimiter
sFileFilters = sFileFilters & vbNullChar
End If

'Select a filter
tFileBrowse.lpstrFilter = sFileFilters & _
"All Files (*.*)" & vbNullChar & "*.*" _
& vbNullChar
'create a buffer for the file
tFileBrowse.lpstrFile = String(clMaxLen, " ")
'set the maximum length of a returned file
tFileBrowse.nMaxFile = clMaxLen + 1
'Create a buffer for the file title
tFileBrowse.lpstrFileTitle = Space$(clMaxLen)
'Set the maximum length of a returned file title
tFileBrowse.nMaxFileTitle = clMaxLen + 1
'Set the initial directory
tFileBrowse.lpstrInitialDir = sInitDir
'Set the parent handle
tFileBrowse.hwndOwner = lParentHwnd
'Set the title
tFileBrowse.lpstrTitle = sTitle

'No flags
tFileBrowse.flags = 0

'Show the dialog
If GetOpenFileName(tFileBrowse) Then
BrowseForFile = Trim$(tFileBrowse.lpstrFile)
If Right$(BrowseForFile, 1) = vbNullChar Then
'Remove trailing null
BrowseForFile = Left$(BrowseForFile, _
Len(BrowseForFile) - 1)
End If
End If
End Function

Sub Test()
' this will return any file
' MsgBox BrowseForFile("c:\Temp")
' this will return only .xls files
MsgBox BrowseForFile("c:\temp", "Word Template (*.dot);*.dot", _
"Open Workbook")

End Sub

fumei
02-24-2010, 11:18 AM
Perhaps if you explain in more detail. I woul dhave to agree with Steve that on the face of it, you seem to re-inventing.

However, it would be easy to build a userform with X number of templates listed. User selects one, clicks OK, a document is cloned from that template.

Although, you couldjust as easily use the native dialog to do this...OR...put your list of templates as menu items. Again, the user selects one, and the template is invoked.