View Single Post
 
Old 08-16-2014, 04:01 AM
Charles Kenyon Charles Kenyon is offline Windows 7 64bit Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,463
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

Quote:
Originally Posted by KSReynolds View Post
My Access application opens up the new word document based on the template as a new document and then merges it. The merge works fine. How do I intercept the file save as then.... I can't believe someone else does this! Would it be code I put in the template?
The procedure I use is as follows:

  1. I do not create a new document based on a template. I open an existing document that was saved as a non-merge document.
  2. The vba attaches the data file from a standard location. It always has the same name: Names.txt.
  3. It performs a merge to a new result document and closes the original document without saving.
  4. It attaches the new document to a template.
  5. That template has a custom ribbon of procedures.
  6. At the beginning of the first procedure it saves to a designated folder and proposes a name that starts with the standard name for the document and adds the days date in the format yyyy-mm-dd.
  7. It uses the FileSaveAs dialog. My procedure displays the dialog with the suggested name. Instead, the dialog could just execute. Go here for references.
  8. The final step includes removing some special formatting (some merge fields are hot pink) and attaching the document to the normal template.
The procedure for intercepting Word commands (FileSave and FileSaveAs) can be found at Intercepting events like Save and Print on the MVP website. Those macros would be in the temporarily attached template named as FileSave and FileSaveAs.

Because my code is scattered in different templates it would be a major effort to rewrite it for what you are doing.

Here is code putting the current date as a tag at the end of the suggested name when saving a new document. I have this in a template. It is sometimes a nuisance, but most files I create I like to put the date as a tag.

Code:
Public Sub SaveWithDate(Optional strName As String)
    ' Run as substitute for FileSave to add date to default document names
    ' Put in a global template as public macro
    ' Call this from other templates
    ' If strName is supplied, it is used as the base name, otherwise the base comes from the Title Property
        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
    '
    If strName = "" Then strName = strName & ActiveDocument.BuiltInDocumentProperties("Title").Value        'get name in title
    
    Dim dlgSave As Dialog
    Set dlgSave = Dialogs(wdDialogFileSaveAs)
    strName = strName & " " & Format((Year(Now() + 1) Mod 100), "20##") & "-" & _
        Format((Month(Now() + 1) Mod 100), "0#") & "-" & _
        Format((Day(Now()) Mod 100), "0#")
    ActiveDocument.BuiltInDocumentProperties("Title").Value = strName
    ActiveDocument.BuiltInDocumentProperties("Company").Value = "Kenyon Law Office"
    With dlgSave
        .Name = strName
        .Show
        
    End With
End Sub

Sub FileSave()
    Application.Run "SaveWithDate"
End Sub

Sub FileSaveAs()
    Application.Run "SaveWithDate"
End Sub
[code]

Again, if I just wanted it to automatically save the file with the chosen name, I would use .Execute instead of .Show.
Reply With Quote