snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
สวัสดีค่ะ
ตอนนี้เขียน code vba เป็นงานจาก Excel แล้วทำการ Export เป็น PDF โดยที่ก่อนจะเปิด PDF ต้องใส่ User and password ด้วยค่ะ แต่ตอนนี้ทำได้แค่ Export to PDF เท่านั้น ไม่สามารถใส่ Password ได้ รบกวนดูCode ด้วยนะคะ
Sub SlipCreate()
Worksheets("data").Range("A:A").Clear
Worksheets("data").Range("A2").Value = "O"
Sheets("data").Select
Dim i As Integer
Dim fName As String
Dim folderName As String
Dim path As String
path = ThisWorkbook.path & "/Slip/"
i = 2
Do
If Not IsEmpty(Sheets("data").Range("B" & i).Value) Then
fName = "J" & Format(Sheets("data").Range("B" & i).Value, "000000") & "_" & Format(Date, "yyyy") & Format(Date, "mm")
folderName = "J" & Format(Sheets("data").Range("B" & i).Value, "000000")
Sheets("slip").Range("A1").Value = Format(Sheets("data").Range("B" & i).Value, "000000")
Sheets("data").Range("A" & i - 1).ClearContents
Sheets("data").Range("A" & i).Value = "O"
chkFolder path & folderName
ExportPDF path & folderName, fName
Else
Exit Do
End If
i = i + 1
Loop
End Sub
Sub ExportPDF(path As String, fName As String)
Worksheets("slip").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=path & "/" & fName & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
Sub chkFolder(path As String)
If Len(Dir(path, vbDirectory)) = 0 Then
MkDir path
End If
End Sub