Autosave PDF in MS Word
This question is inspired by my use of LaTeX, where you usually share the generated PDF, not the source file. I want my .docx
documents to be source files, with PDFs being the files to share. Thus I would like to have MS Word making a PDF with the same document name every time I save the .docx
file. Is there a way to obtain such a behaviour? I'm on a mac, but it'd be nice to be able to do this on a Windows based computer as well.
Example: If I make a new document which I name mydocument.docx
, then MS Word makes a copy in the same location named mydocument.pdf
. The PDF will be saved and overwritten for each time I change and save mydocument.docx
. A somewhat make
-like action?
You can achieve this by overriding the Save
and SaveAs
macros which are called when the user activates those two functions from the editor. This should work in all version of Word that supports saving directly as PDF, which is Microsoft Word 2007 SP2 and up (including Word for Mac 2008 and up).
The full macro to acheive this is below, insert it into your active template so it will work with all documents you create.
There is an error handler which lets you know if the operation failed (most likely because you have the PDF open which gets locked for viewing). If there is any problems catching the events you could also try Application Events in place of the overrides.
' Override File -> Save
Public Sub FileSave()
ActiveDocument.Save
SaveActiveDocumentAsPdf
End Sub
' Override File -> Save As...
Public Sub FileSaveAs()
ActiveDocument.SaveAs
SaveActiveDocumentAsPdf
End Sub
Sub SaveActiveDocumentAsPdf()
On Error GoTo Errhandler
If InStrRev(ActiveDocument.FullName, ".") <> 0 Then
Dim strPath As String
strPath = Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".") - 1) & ".pdf"
ActiveDocument.SaveAs FileName:=strPath, FileFormat:=wdFormatPDF
End If
On Error GoTo 0
Exit Sub
Errhandler:
MsgBox "There was an error saving a copy of this document as PDF. " & _
"Ensure that the PDF is not open for viewing and that the destination path is writable. Error code: " & Err
End Sub