Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-15-2017, 05:09 AM
miyoung miyoung is offline Save as PDF in specific location with filename using todays date Windows 10 Save as PDF in specific location with filename using todays date Office 2010 64bit
Novice
Save as PDF in specific location with filename using todays date
 
Join Date: Aug 2017
Posts: 2
miyoung is on a distinguished road
Default Save as PDF in specific location with filename using todays date


Hello,

I'm pretty new to VBA and trying to work out how to achieve the following:

I have a one page word document which I need to update on a weekly basis once a week (say for example every Tuesday). I want to save this as a PDF to a particular file location on my companies network, which is a mapped drive (For example, W:\Tenants\Rent\).

I would like the file name to be just the date in YYYY-MM-DD format. (for example, 2017-08-15.pdf).

I would like to be prompted if the file already exists to check if it is okay to overwrite the existing file.

I've found a host of similar related questions, but not any that match my requirements, or that I can get to work.

Any help with some code would be more than appreciated!

I've found various code, such as the following, which save the Word document as a PDF, but it only saves in the folder that the original Word document is in, and the filename is not changed as I would like it to.

Sub Print_to_PDF()
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Replace(ActiveDocument.FullName, ".docx", ".pdf"), _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub

Mark
Reply With Quote
  #2  
Old 08-15-2017, 08:03 AM
gmayor's Avatar
gmayor gmayor is offline Save as PDF in specific location with filename using todays date Windows 10 Save as PDF in specific location with filename using todays date Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

If you just want to save with the data as the filename then that is easy enough. The following will warn if the folder is missing of the file exists.

Code:
Sub Print_to_PDF()
Dim strName As String
Dim fso As Object
Const strPath As String = "W:\Tenants\Rent\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(strPath) Then
        MsgBox "The folder " & strPath & " does not exist."
        GoTo lbl_Exit
    End If
    strName = strPath & Format(Date, "yyyy-mm-dd") & ".pdf"
    If fso.FileExists(strName) Then
        If MsgBox("File " & strName & " exists." & vbCr & vbCr & _
                  "Do you want to overwrite", vbYesNo) = vbNo Then
            GoTo lbl_Exit
        End If
    End If

    ActiveDocument.ExportAsFixedFormat OutputFileName:=strName, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=True, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=False, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateNoBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
lbl_Exit:
    Set fso = Nothing
    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 08-15-2017, 09:15 AM
miyoung miyoung is offline Save as PDF in specific location with filename using todays date Windows 10 Save as PDF in specific location with filename using todays date Office 2010 64bit
Novice
Save as PDF in specific location with filename using todays date
 
Join Date: Aug 2017
Posts: 2
miyoung is on a distinguished road
Default

That is great, thank you so much, it works exactly how I wanted it.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Macro to Save Word File as PDF in Specific Location ekimisme Word VBA 1 06-07-2017 10:40 PM
Save as PDF in specific location with filename using todays date Save to PDF with predetermined filename and specific folder path cutemich Word VBA 1 05-09-2017 06:27 PM
Save as PDF in specific location with filename using todays date Save document multiple times with date as filename JonniBravo Word 6 03-13-2017 11:35 AM
Save as PDF in specific location with filename using todays date Macro: How to get this macro to save to a specific location LOUF Word VBA 1 12-07-2015 06:47 PM
Highlight row based on todays date problem manxman79 Excel 6 11-17-2014 09:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:47 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