Page 1 of 1

Excel VBA Export to PDF with password

Posted: Thu Jul 27, 2017 4:42 pm
by janry
สวัสดีค่ะ
ตอนนี้เขียน code vba เป็นงานจาก Excel แล้วทำการ Export เป็น PDF โดยที่ก่อนจะเปิด PDF ต้องใส่ User and password ด้วยค่ะ แต่ตอนนี้ทำได้แค่ Export to PDF เท่านั้น ไม่สามารถใส่ Password ได้ รบกวนดูCode ด้วยนะคะ

Code: Select all

 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

Re: Excel VBA Export to PDF with password

Posted: Thu Jul 27, 2017 9:21 pm
by snasui
:D ไม่สามารถ Save PDF แบบมี Password ได้ด้วย VBA ครับ

แม้แต่ Save ด้วย Manaual ก็ไม่มี Option ให้ Save แบบมี Password ได้ครับ

Re: Excel VBA Export to PDF with password

Posted: Mon Jul 31, 2017 9:16 am
by janry
snasui wrote::D ไม่สามารถ Save PDF แบบมี Password ได้ด้วย VBA ครับ

แม้แต่ Save ด้วย Manaual ก็ไม่มี Option ให้ Save แบบมี Password ได้ครับ
สอบถามเพิ่มเติมค่ะ
แล้วถ้า จากข้อมูลเป็น Excel ใน 1 file หลายๆ recode สามารถ แยกออกมาแบ่งเป็น file ตาม folder และใส่ Password (Excel) สามารถทำไดไหมค่ะ และ code ในตัวอย่าง ต้องเปลี่ยนตรงไหนบ้างจากเดิมที่ Export to PDF ให้มาเป็น Excel

Re: Excel VBA Export to PDF with password

Posted: Mon Jul 31, 2017 9:12 pm
by snasui
:D การแยกข้อมูลเป็นหลายไฟล์พร้อมใส่ Password สามารถทำได้ครับ

ส่วนการเปลี่ยน Code ให้เปลี่ยนมาเองก่อน คำถามควรจะเกิดจากการทำตามแนวทางที่ต้องการแล้วติดปัญหาครับ