#1
|
|||
|
|||
VBA Code to Save current Document as PDF and as DOCX
Would it be possible to use some VBA script in Microsoft Word to make my particular document save itself as a PDF file (in the same directory as the current DOCX) and then as a DOCX file? Basically, every time I save the document, I automatically want to have a PDF file of the most updated version saved in the same directory. It should automatically overwrite the previous version (as in, no prompts for saving over the old PDF) so that way my DOCX is always right next to an updated PDF version.
Is this possible to do with Word VBA? If so, how would you do it? Thank you very much. |
#2
|
||||
|
||||
Maybe something like the following, saved in the document's template. These macros will intercept the save and saveas commands.
Code:
Option Explicit Sub FileSaveAs() On Error Resume Next Dialogs(wdDialogFileSaveAs).Show If Not ActiveDocument.Path = "" Then SaveAsPDF lbl_Exit: Exit Sub End Sub Sub FileSave() On Error Resume Next ActiveDocument.Save SaveAsPDF lbl_Exit: Exit Sub End Sub Sub SaveAsPDF() Dim strDocName As String Dim strPath As String Dim intPos As Integer Start: 'Find position of extension in filename strDocName = ActiveDocument.Name strPath = ActiveDocument.Path & "\" intPos = InStrRev(strDocName, ".") If intPos = 0 Then ActiveDocument.Save GoTo Start End If strDocName = Left(strDocName, intPos - 1) strDocName = strPath & strDocName & ".pdf" ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocName, _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, From:=1, to:=1, _ Item:=wdExportDocumentContent, _ IncludeDocProps:=True, _ KeepIRM:=True, _ CreateBookmarks:=wdExportCreateHeadingBookmarks, _ DocStructureTags:=True, _ BitmapMissingFonts:=True, _ UseISO19005_1:=False lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Fantastic. It works exactly the way that I hoped it would. Thank you so much!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
From a docx report document save all images in a cell from the document to a folder | censura | Word VBA | 1 | 05-13-2017 12:54 AM |
Save highlights within the current selected regions into a fixed document | puff | Word VBA | 3 | 04-19-2017 10:34 AM |
Run Code on all files and save files as .docx | Plokimu77 | Word VBA | 4 | 06-05-2016 04:41 PM |
Vba code to save document as pdf using document property text and rename folder. | staicumihai | Word VBA | 1 | 12-21-2015 07:39 AM |
Extract VBA code to save in Word document | Dave T | Word VBA | 4 | 01-26-2015 08:41 PM |