Inhoudsopgave
Opslaan als
Deze Word-macro slaat het ActiveDocument op met een nieuwe bestandsnaam die de huidige tijd bevat:
Sub SaveMewithDateName() 'slaat het actieve document op in de huidige map als een gefilterde html en heeft de naam op de huidige tijd Dim strTime As String strTime = Format(Now, "hh-mm") ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strTime, FileFormat:=wdFormatFilteredHTML End Sub
Maken en opslaan als
Deze VBA-macro zal een nieuw document maken en opslaan als de huidige datum en tijd:
Sub CreateAndSaveAs() 'creëert een nieuw document en slaat het op als een gefilterde html [In de standaardmap en genoemd naar de huidige tijd] Dim strTime As String Dim strPath As String Dim oDoc As Document strPath = ActiveDocument.Path & Application.PathSeparator strTime = Format (Nu, "jjjj-mm-dd hh-mm") Stel oDoc = Documents.Toevoegen 'maak een nieuw document en wijs het toe aan de oDoc-variabele' schrijf wat tekst in het nieuwe document dat ernaar verwijst met behulp van de oDoc-variabele oDoc.Range.InsertBefore "Bezoek https://easyexcel.net/vba-code-library" oDoc.SaveAs FileName:=strPath & strTime, FileFormat:=wdFormatFilteredHTML oDoc.Close wdDoNotSaveChanges 'close doc End Sub
Opslaan als PDF
Deze macro slaat het Word-document op als PDF:
Sub MacroSaveAsPDF() 'macro slaat pdf op in dezelfde map waar het actieve document zich bevindt of in de documentenmap als het bestand nog niet is opgeslagen' Dim strPath As String Dim strPDFname As String strPDFname = InputBox ("Voer naam in voor PDF", "Bestandsnaam ", "example") If strPDFname = "" Dan 'verwijderde gebruiker tekst uit invoervak, voeg standaardnaam toe strPDFname = "example" End If strPath = ActiveDocument.Path If strPath = "" Dan is 'doc nog niet opgeslagen strPath = Options. DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Else 'voeg gewoon \ toe aan het einde strPath = strPath & Application.PathSeparator End If ActiveDocument.ExportAsFixedFormat OutputFileName:= _ strPath & strPDFname & ".pdf", _Export OpenFormat:=wd =False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocProps:=True, _ CreateBookmarks:=wdExportCreateWordBookmarks, _ BitmapMissingFonts:=True End Sub
Deze functie zal ook elk Word-document opslaan als een PDF:
Sub MacroSaveAsPDFwParameters(Optioneel strPath As String, Optioneel strFilename As String) 'strPath, indien doorgegeven, moet padscheidingsteken ["\"] bevatten If strFilename = "" Dan strFilename = ActiveDocument.Name End If 'extract alleen bestandsnaam zonder extensie If InStr (1, strFilename, ".") > 0 Then strFilename = Left$(strFilename, InStrRev(strFilename, ".") - 1) End If If strPath = "" Then If ActiveDocument.Path = "" Dan is 'doc niet nog opgeslagen, zullen we het standaardpad gebruiken strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator Else ' use path of active doc strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator End If End If On Error Ga naar EXITHERE ActiveFixedFormat.ExportAs OutputFileName:= _ strPath & strFilename & ".pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ IncludeDocProps:=Tmarke,=w Create BitmapOntbrekende Fon ts:=True Exit Sub EXITHERE: MsgBox "Fout: " & Err.Number & " " & Err.Beschrijving End Sub
U kunt het bestandspad en de bestandsnaam invoeren om aan te geven welk bestand u als PDF wilt opslaan:
Sub CallSaveAsPDF() Roep MacroSaveAsPDFwParameters("c:/Documents", "example.docx") End Sub