Skip to content

Instantly share code, notes, and snippets.

@Koushikphy
Created April 22, 2022 13:38
Show Gist options
  • Save Koushikphy/29e37d7ed9cc1555e6b3b04e2ef6019a to your computer and use it in GitHub Desktop.
Save Koushikphy/29e37d7ed9cc1555e6b3b04e2ef6019a to your computer and use it in GitHub Desktop.
Save a doc in PDF with same name in same place
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 = ActiveDocument.Name
strPDFname = Left(strPDFname, InStr(strPDFname, ".") - 1)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment