Save As, VBA, Word Tips

05 – Add to Toolbar

05 Add to toolbar

Now we add a new toolbar and add the macros as buttons (so we 1-click run them).

In the next video, video 6, we’ll look at how to up the game and efficiency by assigning this macro to a shortcut key.


Templates File Path

The filepath to your templates folder is:

C:\Users\{username}\AppData\Roaming\Microsoft\Templates 

Where {username} is your logged in username, i.e. if I was logged in as Russ, the file path would be:

C:\Users\Russ\AppData\Roaming\Microsoft\Templates

Get the VBA Code and Your Templates File Path

Note: Such are the times we live in that I have to say ‘use this code at your own risk’. I use it myself and it works perfectly for me. However, I have zero control over how you use it and if something goes wrong, then that’s down to you. You can see the code in use in the videos, you can see what it does. If that works for you and you can use it, go for it; if it doesn’t, or you don’t want to do it, that’s fine — it’s your choice. But I have zero liability here and by downloading/using the code, you acknowledge that. If you don’t, don’t use it.

Click here to download the text file containing the ‘Save as’ code.

Sub save_new_filename()

'Declare variables

Dim mystr As String 'this gets the current date and time
Dim filepath As String 'this gets the file path property value for the filename
Dim filetitle As String 'this gets the title name for the filename
Dim fullstring As String 'this variable is used to collate file path, title, and date and used for the 'save as'
Dim Response As Integer 'we need this for the msgbox response
Dim xdlg As Dialog 'this is for when we need to show the save as dialog

'===================================================================
'================= Save the file? ===================
'===================================================================


'If the file hasn't been saved and there's no title property, then prompt for it.
'If the user declines, then the save as dialog shows.

If (ActiveDocument.path = "") And ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle).Value = "" Then
            
            Response = MsgBox("We need a title... Click Yes to open the dialog or No to abort.", vbYesNo + vbQuestion, "Exit?")
                Select Case Response
                    Case vbYes
                        Call show_docinformation_panel
                    Case vbNo
                        Set xdlg = Dialogs(wdDialogFileSaveAs)
                        xdlg.Show
                    End Select

    ElseIf (ActiveDocument.path = "") And ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle).Value <> "" Then
                            Set xdlg = Dialogs(wdDialogFileSaveAs)
                        xdlg.Show

End If


'========================================================================
'================= If no title exists, prompt for one ===================
'========================================================================
        
              'If there is no title property, then the message box will open and notify.
              'There are 2 responses: Yes, opens the doc info panel; no, closes the messagebox and ends this subroutine, i.e. returns to the document with no changes made

                    If ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle).Value = "" Then
                            Response = MsgBox("We need a title... Click Yes to open the dialog or No to abort.", vbYesNo + vbQuestion, "Exit?")
                                Select Case Response
                                    Case vbYes
                                            Call show_docinformation_panel
                                    Case vbNo
                                            Exit Sub
                                End Select
                    End If
        
        'There might be a case where the user clicked Yes to add a title, and then cancelled before adding it
        'In this case, this clause prevents the file from saving without a title
        
          If ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle).Value = "" Then
                MsgBox "No title entered."
                Exit Sub
          End If
         
         
'========================================================================
'========= Now we collate the above into a new filename and save ========
'========================================================================

        
    'Here we assign the properties to easier to remember items. We don't have to do this, as there's enough info above,
    'but it makes it much easier to follow what's happening
    
        filepath = ActiveDocument.path & ""
        filetitle = ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle).Value
    
        mystr = Format(Now, "yymmdd_hhmm")
            
    'Now we've defined the 3 properties or components required, we bring them all together
    
        fullstring = filepath & filetitle & "_" & mystr & ".docx"

    
    'The following line saves the new filename in the correct format for a docx file
    
    ActiveDocument.SaveAs FileName:=fullstring, FileFormat:=wdFormatXMLDocument
    
End Sub


Sub show_docinformation_panel()

On Error Resume Next

    If Application.DisplayDocumentInformationPanel = True Then
        Application.DisplayDocumentInformationPanel = False
    ElseIf Application.DisplayDocumentInformationPanel = False Then
        Application.DisplayDocumentInformationPanel = True
    End If
    
End Sub

Note: as far as I am aware, all the above code is my own/gleaned and modified for my own use from public online sources. If you believe I’ve copied someone else’s and not credited them, then let me know and I’ll be happy to change that.

Leave a Reply