Cách xuất pdf tự động lưu

quốc bình

Yêu THVBA
A/C cho em hỏi, em có đoạn code bên dưới để xuất một vùng chọn sang PDF nhưng vòng lặp i chạy với số lớn mà mỗi lần chạy lại phải ngồi click save để lưu file, giờ em làm sao để nó tự lưu lại không cần ngồi click save nữa chứ làm tự động mà cứ ngồi click như thế cũng hơi mất thời gian. xin cảm ơn A/C nhiều!
Mã:
Sub PrintSelectionToPDF()
    Dim invoiceRng As Range
    Dim strfile As String
    Dim myfile As Variant
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("BVLN")
    Set invoiceRng = Range("A1:M31")
    Application.EnableEvents = False
    For i = 1 To 100
        ws.Range("N1") = i
        strfile = "BienBan" & "_" & i & ".pdf"
        myfile = Application.GetSaveAsFilename(InitialFileName:=strfile, FileFilter:="PDF Files (*.pdf), *.pdf", Title:="Lua chon thu muc và tên file luu thành PDF")
        invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
    Application.EnableEvents = True
End Sub
 
A/C cho em hỏi, em có đoạn code bên dưới để xuất một vùng chọn sang PDF nhưng vòng lặp i chạy với số lớn mà mỗi lần chạy lại phải ngồi click save để lưu file, giờ em làm sao để nó tự lưu lại không cần ngồi click save nữa chứ làm tự động mà cứ ngồi click như thế cũng hơi mất thời gian. xin cảm ơn A/C nhiều!
Anh muốn lưu vào đâu? Vào đường dẫn chứa workbook đang kích hoạt ạ? Có thể đính kèm file xem hình thù nó thế nào không ạ?
Hên xui
Mã:
Option Explicit
Sub PrintSelectionToPDF()
    Dim invoiceRng As Range, Link$, i&
    Dim strfile As String
    Dim myfile As Variant
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("BVLN")
    Link = ThisWorkbook.Path & "\"
    Set invoiceRng = ws.Range("A1:M31")
    Application.EnableEvents = False
    For i = 1 To 100
        ws.Range("N1") = i
        strfile = "BienBan" & "_" & i & ".pdf"
        invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=Link & strfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
End Sub
 
Sửa lần cuối:

quốc bình

Yêu THVBA
Anh muốn lưu vào đâu? Vào đường dẫn chứa workbook đang kích hoạt ạ? Có thể đính kèm file xem hình thù nó thế nào không ạ?
Hên xui
Mã:
Option Explicit
Sub PrintSelectionToPDF()
    Dim invoiceRng As Range, Link$, i&
    Dim strfile As String
    Dim myfile As Variant
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("BVLN")
    Link = ThisWorkbook.Path & "\"
    Set invoiceRng = ws.Range("A1:M31")
    Application.EnableEvents = False
    For i = 1 To 100
        ws.Range("N1") = i
        strfile = "BienBan" & "_" & i & ".pdf"
        invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=Link & strfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
End Sub
Mình muốn chọn đường dẫn lưu file thì làm sao? giúp mình với. mình không biết cách đính kèm file nên để link:
 
Bạn thử:
Mã:
Sub PrintSelectionToPDF()
    Dim invoiceRng As Range, Link$, i&
    Dim strfile As String
    Dim myfile As Variant
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("BVLN")
    '===============================FORUM Tuthocvba.net========================
    With Application.FileDialog(msoFileDialogFolderPicker)
        
        .AllowMultiSelect = False

        .Title = "Select folder"
        If .Show = True Then
            Link = .SelectedItems(1)
        End If
    End With
    If Trim(CStr(Link)) = "" Then Exit Sub
    Link = Link & Application.PathSeparator
    '===============================END========================================
    'Link = ThisWorkbook.Path & "\"
    Set invoiceRng = ws.Range("A1:M31")
    Application.EnableEvents = False
    For i = 1 To 100
        ws.Range("N1") = i
        strfile = "BienBan" & "_" & i & ".pdf"
        invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=Link & strfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
End Sub
Dòng code từ 7-19 là để select folder.
Bạn chỉ phải chọn folder lưu trữ 1 lần.
 
Top