Word VBA Macros – SaveAs (PDF or New File Name)

star star star star star

This Word macro will save the ActiveDocument with a new file name that includes the current time:

Sub SaveMewithDateName() 'saves active doc in current folder as a filtered html and named upon current time Dim strTime As String strTime = Format(Now, "hh-mm") ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strTime, FileFormat:=wdFormatFilteredHTML End Sub

Create and SaveAs

This VBA macro will create a new document and save as using the current date and time:

Sub CreateAndSaveAs() 'creates a new doc and saves as a filtered html [In the default folder and named upon current time] Dim strTime As String Dim strPath As String Dim oDoc As Document strPath = ActiveDocument.Path & Application.PathSeparator strTime = Format(Now, "yyyy-mm-dd hh-mm") Set oDoc = Documents.Add 'create a new doc and asign it to oDoc variable 'write some text in the new doc reffering to it using oDoc variable oDoc.Range.InsertBefore "Visit https://www.automateexcel.com/vba-code-library" oDoc.SaveAs FileName:=strPath & strTime, FileFormat:=wdFormatFilteredHTML oDoc.Close wdDoNotSaveChanges 'close doc End Sub

SaveAs PDF

This macro will save the Word document as a PDF:

Sub MacroSaveAsPDF() 'macro saves pdf either in the same folder where active doc is or in documents folder if file is not yet saved ' Dim strPath As String Dim strPDFname As String strPDFname = InputBox("Enter name for PDF", "File Name", "example") If strPDFname = "" Then 'user deleted text from inputbox, add default name strPDFname = "example" End If strPath = ActiveDocument.Path If strPath = "" Then 'doc is not saved yet strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Else 'just add \ at the end strPath = strPath & Application.PathSeparator End If ActiveDocument.ExportAsFixedFormat OutputFileName:= _ strPath & strPDFname & ".pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocProps:=True, _ CreateBookmarks:=wdExportCreateWordBookmarks, _ BitmapMissingFonts:=True End Sub

This function will also Save any word document as a PDF:

Sub MacroSaveAsPDFwParameters(Optional strPath As String, Optional strFilename As String) 'strPath, if passed, must include path separator ["\"] If strFilename = "" Then strFilename = ActiveDocument.Name End If 'extract just file name without extension If InStr(1, strFilename, ".") > 0 Then strFilename = Left$(strFilename, InStrRev(strFilename, ".") - 1) End If If strPath = "" Then If ActiveDocument.Path = "" Then 'doc is not saved yet, we will use default path strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Else ' use path of active doc strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator End If End If On Error GoTo EXITHERE ActiveDocument.ExportAsFixedFormat OutputFileName:= _ strPath & strFilename & ".pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocProps:=True, _ CreateBookmarks:=wdExportCreateWordBookmarks, _ BitmapMissingFonts:=True Exit Sub EXITHERE: MsgBox "Error: " & Err.Number & " " & Err.Description End Sub

You can enter the file path and file name to indicate which file to save as a PDF:

Sub CallSaveAsPDF() Call MacroSaveAsPDFwParameters("c:/Documents", "example.docx") End Sub