Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2017, 03:22 AM
gmayor's Avatar
gmayor gmayor is offline Saving and creating PDF at once Windows 10 Saving and creating PDF at once Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
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 ofgmayor has much to be proud of
Default

What you require is not straightforward - especially with variable dates, but the following will be close (albeit it will start renumbering if the date changes) and the first PDF will not be numbered.



Code:
Option Explicit

Sub SaveAsDocX_PDF()
Dim strName As String
Dim strPDF As String
Dim strPath As String
ActiveDocument.Save
If Not ActiveDocument.Path = "" Then
    strName = ActiveDocument.Name
    strPath = ActiveDocument.Path
    strPDF = Format(Date, "yyyymmdd_") & Left(strName, InStrRev(strName, Chr(46)) - 1)
    strPDF = PDFNameUnique(strPath, strPDF)
    ActiveDocument.SaveAs FileName:=strPath & "\" & strPDF, _
                          FileFormat:=wdFormatPDF
Else
    MsgBox "Document not saved!"
End If
End Sub

Private Function PDFNameUnique(strPath As String, _
                               strFilename As String) As String
'Graham Mayor - http://www.gmayor.com - Last updated - 03 Nov 2017
Dim lngF As Long: lngF = 1
Dim lngName As Long: lngName = Len(strFilename)
Dim strExtension As String
    Do Until Right(strPath, 1) = Chr(92)
        strPath = strPath & Chr(92)
    Loop
    strExtension = ".pdf"
    strFilename = Left(strFilename, lngName)
    Do While FileExists(strPath & strFilename & strExtension) = True
        strFilename = Left(strFilename, lngName) & Format(lngF, "00")
        lngF = lngF + 1
    Loop
    PDFNameUnique = strFilename & strExtension
lbl_Exit:
    Exit Function
End Function

Private Function FileExists(strFullName As String) As Boolean
'Graham Mayor
'strFullName is the name with path of the file to check
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(strFullName) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Set fso = Nothing
    Exit Function
End Function
You may find http://www.gmayor.com/SaveVersionsAdd-In.htm and http://www.gmayor.com/save_numbered_versions.htm useful.
__________________
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
  #2  
Old 11-06-2017, 04:52 AM
DeRoelO DeRoelO is offline Saving and creating PDF at once Windows 10 Saving and creating PDF at once Office 2016
Novice
Saving and creating PDF at once
 
Join Date: Nov 2017
Posts: 3
DeRoelO is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
What you require is not straightforward - especially with variable dates, but the following will be close (albeit it will start renumbering if the date changes) and the first PDF will not be numbered.

Code:
Option Explicit

Sub SaveAsDocX_PDF()
Dim strName As String
Dim strPDF As String
Dim strPath As String
ActiveDocument.Save
If Not ActiveDocument.Path = "" Then
    strName = ActiveDocument.Name
    strPath = ActiveDocument.Path
    strPDF = Format(Date, "yyyymmdd_") & Left(strName, InStrRev(strName, Chr(46)) - 1)
    strPDF = PDFNameUnique(strPath, strPDF)
    ActiveDocument.SaveAs FileName:=strPath & "\" & strPDF, _
                          FileFormat:=wdFormatPDF
Else
    MsgBox "Document not saved!"
End If
End Sub

Private Function PDFNameUnique(strPath As String, _
                               strFilename As String) As String
'Graham Mayor - http://www.gmayor.com - Last updated - 03 Nov 2017
Dim lngF As Long: lngF = 1
Dim lngName As Long: lngName = Len(strFilename)
Dim strExtension As String
    Do Until Right(strPath, 1) = Chr(92)
        strPath = strPath & Chr(92)
    Loop
    strExtension = ".pdf"
    strFilename = Left(strFilename, lngName)
    Do While FileExists(strPath & strFilename & strExtension) = True
        strFilename = Left(strFilename, lngName) & Format(lngF, "00")
        lngF = lngF + 1
    Loop
    PDFNameUnique = strFilename & strExtension
lbl_Exit:
    Exit Function
End Function

Private Function FileExists(strFullName As String) As Boolean
'Graham Mayor
'strFullName is the name with path of the file to check
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(strFullName) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Set fso = Nothing
    Exit Function
End Function
You may find http://www.gmayor.com/SaveVersionsAdd-In.htm and http://www.gmayor.com/save_numbered_versions.htm useful.
Hi,

First thanks for your help, this is already really close to what i would like to get. Now i hope someone knows how to 'fix' the restart of versions when saved on an other date. I understand now that the code checks for an exisiting file name.

I could also consider to manualy edit the version, to call my files example_0.0X.docx and the macro would make it 20171106_example_0.0X1.PDF.

So i suppose this post could be considered solved, thank you very much.

Last edited by DeRoelO; 11-06-2017 at 05:04 AM. Reason: More information added
Reply With Quote
Reply

Tags
pdf creator, saving, saving options



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving and creating PDF at once Problems with creating and saving word templates smd89 Word 2 03-30-2016 06:38 AM
Problem with saving as .PDF benoitbri Word 5 02-12-2016 07:31 AM
Saving and creating PDF at once Saving as pdf danielraviolo Word 5 06-19-2015 09:46 PM
Saving VBA skib PowerPoint 0 02-18-2011 12:59 AM
Saving backup prestoaa Outlook 0 12-13-2010 08:35 PM

Other Forums: Access Forums

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


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