Page 1 of 1

ขอคำแนะนำ CodeVB ส่งออกไฟล์เป็น Pdf ไปโฟลเดอร์ที่สร้างไว้

Posted: Fri Jul 29, 2022 8:59 am
by tigerwit
จากไฟล์ที่แนบมา
ต้องการส่งออกรายงานเป็นไฟล์ .PDF
โดยมีเงื่อนไขว่าต้องบันทึกไฟฟล์ .PDF ไว้ในโฟลเดอร์ที่สร้างขึ้น

Code: Select all

Sub MakeFolder()

    Dim sFolderPath As String
    Dim Path As String
    Dim FName As String
    On Error Resume Next
    Application.ScreenUpdating = False
      
    sFolderPath = "C:\" & Range("R2").Value
    If Dir(sFolderPath, vbDirectory) = "" Then
        MkDir sFolderPath
    End If
    
    sFolderPath = "C:\" & Range("R2").Value & "\" & Range("R3").Value
    If Dir(sFolderPath, vbDirectory) = "" Then
        MkDir sFolderPath
    End If
    
End Sub

และ

Code: Select all

Sub PrintPDF()
Dim FPath As String
Dim i As Long
Call MakeFolder
FPath = "C:\" & Range("R2").Value & "\" & Range("R3").Value
        If MsgBox("คุณต้องการพิมพ์เอกสารทั้งหมด ใช่หรือไม่?", 36, "ยืนยันการสั่งพิมพ์") = 6 Then
            Sheet1.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FPath & Sheet1.Range("H4").Value & ".pdf"

            MsgBox " พิมพ์แบบรายงานผลการเรียนรายบุคคลเสร็จเรียบร้อยแล้ว! ! !"
        Else
                    MsgBox "คุณได้ยกเลิกการสั่งพิมพ์", vbInformation, "Cencel Printing"

        End If
End Sub
จากการทดลองรัน Code ดู ไฟล์จะถูกบันทึกในโฟลเดอร์แรกที่สร้าง
ที่ต้องการคือให้บันทึกเข้าไปในโฟลเดอร์ชั้นในสุด (C:\ครูสุดสวย\รายงานผู้ปกครอง
ต้องปรับ Code อย่างไรครับ

Re: ขอคำแนะนำ CodeVB ส่งออกไฟล์เป็น Pdf ไปโฟลเดอร์ที่สร้างไว้

Posted: Fri Jul 29, 2022 9:12 am
by snasui
:D ปรับตรง Statement นี้ครับ

จาก
sFolderPath = "C:\" & Range("R2").Value & "\" & Range("R3").Value

ปรับเป็น
sFolderPath = "C:\" & Range("R2").Value & "\" & Range("R3").Value & "\"

Re: ขอคำแนะนำ CodeVB ส่งออกไฟล์เป็น Pdf ไปโฟลเดอร์ที่สร้างไว้

Posted: Wed Aug 03, 2022 9:33 am
by tigerwit
ได้แล้วครับ
ขอบคุณมากครับ