View Single Post
 
Old 09-29-2020, 09:11 AM
Charles Kenyon Charles Kenyon is offline Windows 10 Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,584
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

The key language in the following macro is:
Code:
    strName = strName & " " & ActiveDocument.ContentControls(1).Range.Text 'add name from first content control

Here is a pair of macros that I use:


Code:
Sub FileSaveAs()
    ' Run as substitute for FileSave to add date to default document names
    ' Charles Kenyon 2017, 2019, 2020
    ' Appends date to Title Document property when saving
    On Error Resume Next
   
    If Len(ActiveDocument.Path) > 0 Then
      ' The document has already been saved at least once.
      ' Just save and exit the macro.
      ActiveDocument.Save
      Exit Sub
    End If
    '
    '
    Dim strName As String, dlgSave As Dialog
    Dim strPath As String   'Holder for current path
    Let strPath = Application.Options.DefaultFilePath(wdDocumentsPath)
    Set dlgSave = Dialogs(wdDialogFileSaveAs)
    strName = ActiveDocument.BuiltInDocumentProperties("Title").Value 'get name in title
    strName = strName & " " & ActiveDocument.ContentControls(1).Range.Text 'add name from first content control
    strName = strName & " " & Format((Year(Now() + 1) Mod 100), "20##") & "-" & _
        Format((Month(Now() + 1) Mod 100), "0#") & "-" & _
        Format((Day(Now()) Mod 100), "0#") 'add date
    With dlgSave
        .Name = strPath & strName
        .Show
    End With
    '   Reset save path
    Let Application.Options.DefaultFilePath(wdDocumentsPath) = strPath
    '   empty object
    Set dlgSave = Nothing
End Sub

Sub FileSave()
    FileSaveAs
End Sub

This grabs the Title from document properties and adds the content of the first content control in the document and then the date.
Reply With Quote