Salva come
Questa macro di Word salverà l'ActiveDocument con un nuovo nome file che include l'ora corrente:
Sub SaveMewithDateName() 'salva il documento attivo nella cartella corrente come html filtrato e denominato nell'ora corrente Dim strTime As String strTime = Format(Now, "hh-mm") ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strTime, FileFormat:=wdFormatFilteredHTML End Sub
Crea e salva con nome
Questa macro VBA creerà un nuovo documento e salverà come utilizzando la data e l'ora correnti:
Sub CreateAndSaveAs() 'crea un nuovo documento e lo salva come html filtrato [Nella cartella predefinita e denominato nell'ora corrente] Dim strTime As String Dim strPath As String Dim oDoc As Document strPath = ActiveDocument.Path & Application.PathSeparator strTime = Format (Ora, "aaaa-mm-gg hh-mm") Set oDoc = Documents.Add 'crea un nuovo documento e assegnalo alla variabile oDoc 'scrivi del testo nel nuovo documento facendo riferimento ad esso utilizzando la variabile oDoc oDoc.Range.InsertBefore "Visita https://easyexcel.net/vba-code-library" oDoc.SaveAs FileName:=strPath & strTime, FileFormat:=wdFormatFilteredHTML oDoc.Close wdDoNotSaveChanges 'chiudi doc End Sub
Salva come PDF
Questa macro salverà il documento Word come PDF:
Sub MacroSaveAsPDF() 'la macro salva il pdf nella stessa cartella in cui si trova il documento attivo o nella cartella dei documenti se il file non è ancora stato salvato ' Dim strPath As String Dim strPDFname As String strPDFname = InputBox("Enter name for PDF", "File Name ", "esempio") If strPDFname = "" Then 'utente ha eliminato il testo dalla casella di input, aggiungi il nome predefinito strPDFname = "esempio" End If strPath = ActiveDocument.Path If strPath = "" Then 'doc non è ancora stato salvato strPath = Options. DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Altrimenti 'aggiungi semplicemente \ alla fine strPath = strPath & Application.PathSeparator End If ActiveDocument.ExportAsFixedFormat OutputFileName:= _ strPath & strPDFname & ".pdf", _ExportFormatPDF:=w =False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocProps:=True, _ CreateBookmarks:=wdExportCreateWordBookmarks, _ BitmapMissingFonts:=True End Sub
Questa funzione salverà anche qualsiasi documento Word come PDF:
Sub MacroSaveAsPDFwParameters(Optional strPath As String, Optional strFilename As String) 'strPath, se passato, deve includere il separatore di percorso ["\"] If strFilename = "" Then strFilename = ActiveDocument.Name End If 'estrae solo il nome del file senza estensione If InStr (1, strFilename, ".") > 0 Then strFilename = Left$(strFilename, InStrRev(strFilename, ".") - 1) End If If strPath = "" Then If ActiveDocument.Path = "" Then 'doc non lo è salvato ancora, useremo il percorso predefinito strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Else ' usa il percorso del documento attivo strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator End If End If On Error GoTo EXITHERE ActiveDocument.ExportA OutputFileName:= _ strPath & strFilename & ".pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocmarkProps, _=TrueBookmarkProps:=Tru BitmapMissingFon ts:=True Exit Sub ESITHERE: MsgBox "Error: " & Err.Number & " " & Err.Description End Sub
Puoi inserire il percorso e il nome del file per indicare quale file salvare come PDF:
Sub CallSaveAsPDF() Chiama MacroSaveAsPDFwParameters("c:/Documents", "example.docx") End Sub