Macro Word VBA - SaveA (PDF hoặc Tên tệp mới)

Lưu thành

Macro Word này sẽ lưu ActiveDocument với tên tệp mới bao gồm thời gian hiện tại:

Sub SaveMewithDateName () 'lưu tài liệu đang hoạt động trong thư mục hiện tại dưới dạng html được lọc và được đặt tên theo thời gian hiện tại Dim strTime As String strTime = Format (Now, "hh-mm") ActiveDocument.SaveAs FileName: = ActiveDocument.Path & "\" & strTime, FileFormat: = wdFormatFilteredHTML End Sub

Tạo và Lưu

Macro VBA này sẽ tạo một tài liệu mới và lưu dưới dạng ngày và giờ hiện tại:

Sub CreateAndSaveAs () 'tạo một tài liệu mới và lưu dưới dạng html đã lọc [Trong thư mục mặc định và được đặt tên theo thời gian hiện tại] Dim strTime As String Dim strPath As String Dim oDoc As Document strPath = ActiveDocument.Path & Application.PathSeparator strTime = Format (Bây giờ, "yyyy-mm-dd hh-mm") Đặt oDoc = Documents.Add 'tạo một tài liệu mới và gán nó vào biến oDoc' viết một số văn bản trong tài liệu mới cung cấp cho nó bằng cách sử dụng biến oDoc.Range.InsertBefore "Truy cập https://easyexcel.net/vba-code-library" oDoc.SaveAs FileName: = strPath & strTime, FileFormat: = wdFormatFilteredHTML oDoc.Close wdDoNotSaveChanges 'Close doc End Sub

Save As PDF

Macro này sẽ lưu tài liệu Word dưới dạng PDF:

Macro Sub MacroSaveAsPDF () 'lưu pdf trong cùng một thư mục chứa tài liệu đang hoạt động hoặc trong thư mục tài liệu nếu tệp chưa được lưu' Dim strPath As String Dim strPDFname As String strPDFname = InputBox ("Nhập tên cho PDF", "Tên tệp "," example ") Nếu strPDFname =" "Thì 'người dùng đã xóa văn bản khỏi hộp nhập, thêm tên mặc định strPDFname =" example "End Nếu strPath = ActiveDocument.Path Nếu strPath =" "Thì' doc vẫn chưa được lưu strPath = Tùy chọn. DefaultFilePath (wdDocumentsPath) & Application.PathSeparator Khác 'chỉ thêm \ ở cuối strPath = strPath & Application.PathSeparator End If ActiveDocument.ExportAsFixedFormat OutputFileName: = _ strPath & strPDFname & ".pdf", _ ExportFormat: = wdExport: = False, _ OptimizeFor: = wdExportOptimizeForPrint, _ Range: = wdExportAllDocument, _ includeDocProps: = True, _ CreateBookmarks: = wdExportCreateWordBookmarks, _ BitmapMissingFonts: = True End Sub

Chức năng này cũng sẽ Lưu bất kỳ tài liệu word nào dưới dạng PDF:

Sub MacroSaveAsPDFwParameters (strPath As String tùy chọn, strFilename As String tùy chọn) 'strPath, nếu được truyền, phải bao gồm dấu phân cách đường dẫn ["\"] If strFilename = "" Then strFilename = ActiveDocument.Name End If' chỉ giải nén tên tệp mà không có phần mở rộng If InStr (1, strFilename, ".")> 0 Sau đó strFilename = Left $ (strFilename, InStrRev (strFilename, ".") - 1) End If If strPath = "" Then If ActiveDocument.Path = "" Thì 'doc không được lưu chưa, chúng tôi sẽ sử dụng đường dẫn mặc định strPath = Options.DefaultFilePath (wdDocumentsPath) & Application.PathSeparator Đường dẫn sử dụng khác của doc strPath = Options.DefaultFilePath (wdDocumentsPath) & Application.PathSeparator End If End If On Error GoTo EXITHERE ActiveDocument.ExportAsFixedFixed OutputFileName: = _ strPath & strFilename & ".pdf", _ ExportFormat: = wdExportFormatPDF, _ OpenAfterExport: = False, _ OptimizeFor: = wdExportOptimizeForPrint, _ Range: = wdExportAllDocument, _bao gồmDocPropsport: = True, _ CreateExmarks BitmapMissingFon ts: = True Exit Sub EXITHERE: MsgBox "Lỗi:" & Err.Number & "" & Err.Description End Sub

Bạn có thể nhập đường dẫn tệp và tên tệp để cho biết tệp nào sẽ lưu dưới dạng PDF:

Sub CallSaveAsPDF () Gọi MacroSaveAsPDFwParameters ("c: / Documents", "example.docx") End Sub

Bạn sẽ giúp sự phát triển của trang web, chia sẻ trang web với bạn bè

wave wave wave wave wave