Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2012, 04:26 PM
sheeand sheeand is offline Appending the filename with the current date Windows 7 64bit Appending the filename with the current date Office 2010 64bit
Novice
Appending the filename with the current date
 
Join Date: May 2012
Posts: 2
sheeand is on a distinguished road
Default Appending the filename with the current date

I need a macro for a word document (or template) that, upon Saving the document will append the filename with the current date (in yyy-mm-dd format). Subsequent saves of the new document (with the appended filename) will retain that filename.

Here's what I've tried so far. I still need to tie it into the Ctrl-S button, and make RetainFileName() work.


Code:
Option Explicit
' Filename is Journal.docm whose fileNameLength=12
Public Sub Macro1()
Dim fileNameLength As Integer
fileNameLength = Len(ActiveDocument.Name)
If fileNameLength > 12 Then
  RetainFileName
Else
  NewFileName
End If
End Sub
Private Sub RetainFileName()
ActiveDocument.Save
End Sub
Private Sub NewFileName()
Dim dt As Date
Dim dd As Integer
Dim mm As Integer
Dim yyyy As Integer
Dim strDt As String
dt = Now
dd = Day(dt)
mm = Month(dt)
yyyy = Year(dt)
strDt = Str(yyyy) + "-" + Trim(Str(mm)) + "-" + Trim(Str(dd))
ActiveDocument.SaveAs2 FileName:="Journal " + strDt + ".docm", _
  FileFormat:= _
  wdFormatDocumentDefault, LockComments:=False, Password:="", _
  AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
  EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
  :=False, SaveAsAOCELetter:=False, CompatibilityMode:=14
End Sub


Last edited by macropod; 05-12-2012 at 10:21 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 05-12-2012, 11:15 PM
macropod's Avatar
macropod macropod is offline Appending the filename with the current date Windows 7 64bit Appending the filename with the current date Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Sheeand,

Probably the simplest solution is to add a 'Document_New' sub to your Journal template's 'ThisDocument' module, coded as:
Code:
Private Sub Document_New()
Dim StrPath As String
Strpath = "C:\Users\" & Environ("UserName") & "\Documents\Journals\"
ActiveDocument.SaveAs2 FileName:=StrPath & "Journal " & Format(Now, "YYYY-MM-DD"), Fileformat:=wdFormatXMLDocumentMacroEnabled
End Sub
This will automatically save the file with today's date immediately it's created. Note that I've retained the 'docm' format, though I'm not sure why you'd need it. If you're not going to have macros in it, you should use the docx format, in which case you can delete the 'MacroEnabled' string from the code.

Alternatively, you could use a '' macro coded like:
Code:
Sub FileSave()
Dim StrPath As String
StrPath = "C:\Users\" & Environ("UserName") & "\Documents\Journals\"
With ActiveDocument
  If Right(Split(.Name, ".")(0), 10) Like "[####-##-##]" Then
    .Save
  Else
    .SaveAs2 FileName:=StrPath & "Journal " & Format(Now, "YYYY-MM-DD"), Fileformat:=wdFormatXMLDocumentMacroEnabled
  End If
End With
End Sub
This will intercept save attempts via Ctrl-S.

You will, of course, need to edit the filepath in the 'StrPath' variable to suit your requirements. Note also my somewhat simpler method of adding the date string.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-14-2012, 05:18 AM
sheeand sheeand is offline Appending the filename with the current date Windows 7 64bit Appending the filename with the current date Office 2010 64bit
Novice
Appending the filename with the current date
 
Join Date: May 2012
Posts: 2
sheeand is on a distinguished road
Default

Paul - Thanks a million! Your second suggestion works for me.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending the filename with the current date Current date vertical status line: How to turn off? Pmacdaddy Project 4 01-04-2022 09:53 AM
Appending the filename with the current date Meaning of current date vs status date when saving baselines ketanco Project 1 02-08-2012 02:20 PM
Appending the filename with the current date How to insert current date into default filename ? czomberzdaniela Word 1 12-27-2011 07:18 PM
Appending the filename with the current date How to call current PC date and/or current PC year KIM SOLIS Excel 2 11-04-2011 06:09 PM
Appending the filename with the current date Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft