AutoNew to change Title and default file name

B

Bill

I am trying to use AutoNew() to automatically set the title of the new
documents based upon the Normal.dot template, and thereby changing the
default save as name to this new title...

Sub AutoNew()
fname = Format(Now(), "yyyyMMdd ")
Dim dp As Object
Set dp = ActiveDocument.BuiltInDocumentProperties
dp("Title") = fname
End Sub

I have a couple of issues.

1. AutoNew isn't firing from Normal.dot, I've used the identical code in
other templates, but for some reason it's not working from Normal.dot

2. The properties box shows that the Title is being updated to the correct
format (YYYYMMdd), but Word isn't using this as the file name. Sometimes it
uses the first few words of the document, other times it uses a date, but the
wrong one(?!). I have NO idea why the incorrect date would be used because
A. there is no default title name in the template, B. that particular date is
not found anywhere in the document and C. that date was last saturday, when
none of these documents were touched.

My ultimate goal in using this macro is to have the current date in YYYYMMdd
format when the file is saved/save as. However, simply plugging that in
using FileSave() or FileSaveAs() doesn't work (based onthe examples I've
seen) because the user will need to add additional file name information
after the date.

If anyone can help me out or suggest a better/easier/more efficient method
it is appreciated. Thanks
 
J

Jean-Guy Marcil

Bill was telling us:
Bill nous racontait que :
I am trying to use AutoNew() to automatically set the title of the new
documents based upon the Normal.dot template, and thereby changing the
default save as name to this new title...

Sub AutoNew()
fname = Format(Now(), "yyyyMMdd ")
Dim dp As Object
Set dp = ActiveDocument.BuiltInDocumentProperties
dp("Title") = fname
End Sub

I have a couple of issues.

1. AutoNew isn't firing from Normal.dot, I've used the identical
code in other templates, but for some reason it's not working from
Normal.dot

2. The properties box shows that the Title is being updated to the
correct format (YYYYMMdd), but Word isn't using this as the file
name. Sometimes it uses the first few words of the document, other
times it uses a date, but the wrong one(?!). I have NO idea why the
incorrect date would be used because A. there is no default title
name in the template, B. that particular date is not found anywhere
in the document and C. that date was last saturday, when none of
these documents were touched.

My ultimate goal in using this macro is to have the current date in
YYYYMMdd format when the file is saved/save as. However, simply
plugging that in using FileSave() or FileSaveAs() doesn't work (based
onthe examples I've seen) because the user will need to add
additional file name information after the date.

If anyone can help me out or suggest a better/easier/more efficient
method it is appreciated. Thanks

If you are working from Normal.dot, then the "New" macro will not fire when
Word is first launched, only when you create documents from Normal.dot once
Word is already open.

However, you can trick Word into using the "New" event if you force Word to
create a document from VBA when launching

Try this code in the ThisDocument module.
Also, note the way I create the DocumentPorperty, if you do it the way you
were trying, somehow Word does not realize that a value is in the property,
even though you can see it if you display the dialogue.

Option Explicit

Private Sub Document_New()

With Dialogs(wdDialogFileSummaryInfo)
.Title = CStr(Format(Now(), "yyyyMMdd "))
.Execute
End With

End Sub


Sub AutoExec()

If Documents.Count = 0 Then
Application.Documents.Add
End If

End Sub
 
B

Bill

OK, this is weird...

Your code worked in that when I create a new doc based on normal.dot the
Title is set to the correct yyyyMMdd. HOWEVER, when I go to Save As it is
not bringing the correct date over. Title is 20090805, but when I save as
the name is "20090801.doc." The document has absolutely nothing on it, I
simply opened it up, checked the properties pane, and hit Ctrl+Shift+S. I
have no other macros in normal.dot and I have nothing in ThisDocument other
than your code... What on earth could be causing this?
 
B

Bill

Weirder and weirder...

I set the date on my computer to tomorrow and everything worked as it
should, changed it back to today and the same problem as described above
happens (correct date in title and from alt+shift+d, but incorrect date on
'save as'). I set the date to yesterdays date and it works correctly... ?8o
 
S

Stefan Blom

Hmm, I can't reproduce this behavior. Jean-Guy's macros seem to work fine
for me.

Have you tried restarting Windows?

Have you updated to the most recent service packs (Windows and Office)?
 

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