Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2018, 03:42 PM
JohnTravolski JohnTravolski is offline VBA Code to Save current Document as PDF and as DOCX Windows 10 VBA Code to Save current Document as PDF and as DOCX Office 2016
Novice
VBA Code to Save current Document as PDF and as DOCX
 
Join Date: Aug 2017
Posts: 4
JohnTravolski is on a distinguished road
Exclamation 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.
Reply With Quote
  #2  
Old 02-03-2018, 09:36 PM
gmayor's Avatar
gmayor gmayor is offline VBA Code to Save current Document as PDF and as DOCX Windows 10 VBA Code to Save current Document as PDF and as DOCX Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 02-03-2018, 11:04 PM
JohnTravolski JohnTravolski is offline VBA Code to Save current Document as PDF and as DOCX Windows 10 VBA Code to Save current Document as PDF and as DOCX Office 2016
Novice
VBA Code to Save current Document as PDF and as DOCX
 
Join Date: Aug 2017
Posts: 4
JohnTravolski is on a distinguished road
Default

Fantastic. It works exactly the way that I hoped it would. Thank you so much!
Reply With Quote
Reply



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
VBA Code to Save current Document as PDF and as DOCX Save highlights within the current selected regions into a fixed document puff Word VBA 3 04-19-2017 10:34 AM
VBA Code to Save current Document as PDF and as DOCX 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft