Default File name "feature"

R

Rob Geraghty

Hi,
In Word it is possible to put text into the "Title" property of a template
which will then become the default file name when saving a document created
with the template. However, the Title property can only contain alphanumeric
characters or spaces. It appears that anything else causes the default
filename to end.
For example:
"My Default Filename" in the title field will present "My Default
Filename.doc" (or docx for Word 2007) as the default file name.
However, if the title field contains a non-alphanumeric character, the name
will cut short e.g.
"My-Default-Filename" in the title field will present "My.doc" as the
default file name.
Hyphens, underscores, periods and some other characters are valid in Windows
file names. If there any way I can use hyphens in the title field without
the name being cut short? Is there something which will make the code
reading the title property to treat something as a literal e.g. \- to insert
a hyphen?
I suspect the only way to do this may be to insert VBA code on a file save
event or attached to a button.

I haven't been able to find any mention of this aspect of the title property
anywhere. Lots of pages exist on the web explaining how to use the title
property to make a default file name but none mention this limitation on the
content.

Rob
 
J

Jay Freedman

Hi Rob,

Your observations are correct. There is nothing like a backslash "escape" to
make the filename accept a hyphen.

There is a macro workaround at
http://word.mvps.org/faqs/macrosvba/SetDefFilename.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Wed, 3 Sep 2008 19:06:13 -0700, Rob Geraghty <Rob
 
R

Rob Geraghty

Jay Freedman said:

Hi Jay,
Thanks for your quick response. Not being experienced in VBA, it's not
obvious to me how the work-around is supposed to be used. I tried putting
the VBA code into a template linked to a macro button and yes, it changes the
title property to contain underscores, but it doesn't change the file save
behaviour. After running the code, the title property has changed to include
underscores, but the default file name is still "xxxx". In other words it is
concatenated at the first underscore.
I suspect the code has to be run when saving the file but no guidance is
provided for how to implement this and in fact the article suggests that
intercepting the save commands would be kludgy.
Rob
 
G

Graham Mayor

If you use vba to intercept the save command in the particular template to
which this will apply then the following should work with your hyphens

Sub Filesave()
Dim sTitle As String
sTitle = ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle)
If Len(sTitle) = 0 Then
MsgBox "The Document Title field is empty", vbCritical, "Title"
Dialogs(wdDialogFileSummaryInfo).Show
sTitle = ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle)
End If
If Application.Version < 12 Then
ActiveDocument.SaveAs sTitle & ".doc", wdDocument
Else
ActiveDocument.SaveAs sTitle & ".docx", wdFormatXMLDocument
End If
End Sub

When you click file > save or the save icon, this saves the document with
the name in the title property field. As presented it does not create a
backup file (if that option is set) and it will overwrite any file of the
same name without warning. The file is saved in the currently active folder.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Rob Geraghty

Graham Mayor said:
If you use vba to intercept the save command in the particular template to
which this will apply then the following should work with your hyphens

Thanks Graham. I've combined aspects of the two suggestions. The code now
prompts the user to complete the document properties then saves the file with
the title property contents as the name. I don't particularly like assigning
a value to an undeclared variable but being VBA, it works. I need to come up
with some more complete error trapping and consider how to set the folder to
save the file. As it is, it saves into the root of "My Documents".

Sub Filesave()
Dim sTitle As String

' Establish title, subject, author, and keywords values
Set dlgProp = Dialogs(wdDialogFileSummaryInfo)
' Show the dialog to allow user to change the properties
dlgProp.Show
' Set the values
dlgProp.Execute
' Save the file using the title property as the name
sTitle = ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle)
If Len(sTitle) = 0 Then
MsgBox "The Document Title field is empty", vbCritical, "Title"
Dialogs(wdDialogFileSummaryInfo).Show
sTitle = ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle)
End If
If Application.Version < 12 Then
ActiveDocument.SaveAs sTitle & ".doc", wdDocument
Else
ActiveDocument.SaveAs sTitle & ".docx", wdFormatXMLDocument
End If
End Sub
 
G

Graham Mayor

The macro I posted already prompted for the properties if the property title
field was empty?
If you are asking the users always to fill the title field, I don't really
see the advantage of using it for this purpose?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Rob Geraghty

Graham Mayor said:
The macro I posted already prompted for the properties if the property title
field was empty?
If you are asking the users always to fill the title field, I don't really
see the advantage of using it for this purpose?

Hi Graham,

The template which is to contain the macro needs existing content in the
"Title" field so that the user is prompted with the standard format of the
filename for that document type. The discussion started with wanting to put
a default file name into the title field which included characters such as
hyphens, which Word cannot do.

I don't want to prompt the user with an empty title field. I want to prompt
the user with a default title that they can easily modify to fit with a
pre-determined naming standard.

At the end of the macro, the title field in the resulting document contains
a copy of the file name matching the naming convention, and the file has been
saved using that name. Even better, the new document doesn't have the macro
in it.

If the naming standard is changed, the macro stays the same and only the
title property needs to be changed in the template. This is easier for
managers who are unfamiliar with VBA.

The only challenge now in terms of VBA is to figure out how to save the new
document in the right place.

Rob
 
G

Graham Mayor

OK - Saving in the right place? At its simplest you can add the path to the
lines

ActiveDocument.SaveAs sTitle & ".doc", wdDocument
eg
ActiveDocument.SaveAs "C:\Path\" & sTitle & ".doc", wdDocument
but the path would have to pre-exist?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Rob Geraghty

Graham Mayor said:
OK - Saving in the right place? At its simplest you can add the path to the
lines eg ActiveDocument.SaveAs "C:\Path\" & sTitle & ".doc", wdDocument
but the path would have to pre-exist?

Yep, that would work if the path was that simple, but the templates are to
be installed on a SharePoint server. As a result the UNC is long and
variable. I may have to investigate an API call to get the proper path. At
the moment the files save into the root of "My Documents" so they would have
to be manually copied to the proper destination.

Thanks for the suggestion!

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top