Trying to get an autoname macro going in Word 2013

HexiumVII

Senior member
Dec 11, 2005
661
7
81
I'm trying to get a Word form to automatically insert the name and date into the filename when I save it. I don't know much programming but have pieced together the following:

Sub FileSave() ' ' FileSave Macro ' Saves the active document or template ' With Application.Dialogs(wdDialogFileSaveAs) SendKeys "%n" & " " & ActiveDocument.ContentControls(1).Range.Text & " " & ActiveDocument.ContentControls(2).Range.Text & " " & ActiveDocument.ContentControls(14).Range.Text & " " & Format(Date, "mm-dd-yy") .Show

End With End Sub

It mostly works but the macro seems to evoke on every word doc if I try to save. I get an error as it can't find the form fields. How do I get it to only activate on the template form i have?

Also a really really weird bug is that it will randomly delete 0-3 characters in the beginning. For example if the first name is Michael, it will sometimes show ichael or chael. It would be totally random how many it would remove. That is why i put three spaces in the beginning to just help quell the bug a little.

Any tips would be much appreciated!
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
You have a few options here. You can just have it automatically save it without prompting the dialog (although you may do this so the user can make edits), or you can check the name of the file for a key word from the document.

For example, if the template always has "template" in the name, then you can do this:

Code:
If InStr(ActiveDocument.Name, "template") > 0 Then
    'Do some code here
End If

To do the saving behind the scenes, use ActiveDocument.SaveAs2.