Share via

Automatic save with new name for document created with template when clicking on save icon.

Anonymous
2023-12-12T20:14:14+00:00

I have a template in word which receives input from the customer. My intention is that once they have finished editing it and have clicked on save, I create a new file name according to their input, and save it in a specific folder.

What I haven't managed to do is to find a trigger to activate the code after save is clicked.

Currently when I click save I get the box with the default file name (first line).

I'm attaching a screen shot of what I get at the moment (error and greyed out filename)

My code is

Private WithEvents App As Word.Application

Private WithEvents Doc As Word.Document

Private Sub Document_New()

' Set up application-level event handlers

Set App = Word.Application

End Sub

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

End Sub

Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

' use ThisDocument to save to same folder as template

strPath = ThisDocument.Path & ""

If Right(strPath, 1) <> "" Then

strPath = strPath & ""

End If

Doc.SaveAs2 FileName:="c:\test\newfilename.docx"

Doc.SelectContentControlsByTitle("DateTime")(1).Range.Text = Now()

Application.Quit SaveChanges:=wdSaveChanges

End Sub

[faulty image deleted by moderator]

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2023-12-13T02:39:37+00:00

    @Vince R.

    This is nonsense.

    Because you do not know how to do this is not a reason to chase someone away from this forum which has been answering questions like this for more than a decade.

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2023-12-13T02:44:23+00:00

    @Caroline

    For some reason new people here have trouble posting images in the initial post but can in a reply. You are welcome to do that.

    Ignore Vince. Someone here will help you and anyone you would get on the other forum he suggested will help you here. I will be back with some helpful information.

    ThisDocument means the template holding the code, not the new document created from the template. A new document created from the template would be ActiveDocument.

    The way a template is intended to work in Word is that the user double-clicks on it or chooses it using File > New and a brand new document based on the template is created. You do not need to create a document with your code, it happens by this basic Word process. Templates in Microsoft Word "Template" is a jargon term of art in Word that has a somewhat different meaning from the meaning in plain English or in engineering jargon.

    If the template has a title document property, that is the start for a name by default.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-12-13T01:59:34+00:00

    Hello! Caroline Spector

    Thank you for posting in Microsoft Community

    Just to set your expectation that we can only do basic troubleshooting steps for Microsoft 365 office products, OneDrive, and Outlook. And it appears to be that your concern is far more advance and in need of an expert.

    I would advise to post your concern in this link: Questions - Microsoft Q&A

    I hope this help!

    Thank you,
    Vince

    Microsoft Community Moderator

    Was this answer helpful?

    0 comments No comments