समझिए
1. एक लाइन: शीट से PDF
Worksheets("Invoice").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & Application.PathSeparator & "Invoice.pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
PDF शीट की Page Layout सेटिंग मानती है: प्रिंट एरिया, ओरिएंटेशन, मार्जिन, Fit to 1 page wide। टेम्पलेट पर इन्हें एक बार सेट करें, और हर PDF सही दिखेगी।
2. आसान लूप: हर शीट अपनी अलग PDF
Sub ExportEverySheet()
Dim sh As Worksheet, folder As String
folder = ThisWorkbook.Path & Application.PathSeparator
For Each sh In ThisWorkbook.Worksheets
If sh.Visible = xlSheetVisible Then
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=folder & sh.Name & ".pdf"
End If
Next sh
End Sub
3. असली काम: टेम्पलेट से हर ग्राहक की एक PDF
शीट "Customers" (लिस्ट):
| A | B | C | |
|---|---|---|---|
| 1 | CustomerID | Name | |
| 2 | C001 | Rahul Sharma | rahul@example.com |
| 3 | C002 | Neha Gupta | neha@example.com |
शीट "Statement" (टेम्पलेट): सेल B3 में CustomerID है। बाकी सब — नाम, पता, इनवॉइस की लिस्ट, टोटल — B3 के आधार पर XLOOKUP/SUMIFS/FILTER फ़ॉर्मूलों से आता है। B3 बदलें, पूरा स्टेटमेंट बदल जाता है।
मैक्रो बस B3 बदलता है, गणना करवाता है, और एक्सपोर्ट करता है — बार-बार।
Option Explicit
Sub ExportStatements()
Dim wsList As Worksheet, wsTpl As Worksheet
Dim lastRow As Long, i As Long, count As Long
Dim folder As String, custID As String, custName As String
Dim calcMode As XlCalculation
On Error GoTo ErrHandler
Set wsList = ThisWorkbook.Worksheets("Customers")
Set wsTpl = ThisWorkbook.Worksheets("Statement")
lastRow = wsList.Cells(wsList.Rows.Count, "A").End(xlUp).Row
folder = ThisWorkbook.Path & Application.PathSeparator & "PDF_" & Format(Date, "yyyy-mm")
If Dir(folder, vbDirectory) = "" Then MkDir folder
calcMode = Application.Calculation
Application.ScreenUpdating = False
For i = 2 To lastRow
custID = Trim(CStr(wsList.Cells(i, "A").Value))
custName = Trim(CStr(wsList.Cells(i, "B").Value))
If Len(custID) > 0 Then
wsTpl.Range("B3").Value = custID
Application.Calculate ' make sure formulas update
wsTpl.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=folder & Application.PathSeparator & custID & "_" & custName & ".pdf", _
Quality:=xlQualityStandard, IgnorePrintAreas:=False
count = count + 1
End If
Next i
MsgBox count & " PDFs saved in:" & vbCrLf & folder, vbInformation
CleanExit:
Application.Calculation = calcMode
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox "Stopped at row " & i & ": " & Err.Description, vbExclamation
Resume CleanExit
End Sub
C001_Rahul Sharma.pdf जैसे नाम फ़ाइलें ढूँढना और अगले लेसन में अटैच करना आसान बनाते हैं।
4. जिनको भेजने लायक कुछ नहीं, उन्हें छोड़ें
अगर टेम्पलेट, मान लीजिए F30 में टोटल दिखाता है, तो ज़ीरो बैलेंस वाले ग्राहक छोड़ें:
If wsTpl.Range("F30").Value <> 0 Then
' export
End If
5. कई शीट एक PDF में
उन्हें ग्रुप के रूप में चुनें और एक्टिव चुनाव एक्सपोर्ट करें:
ThisWorkbook.Worksheets(Array("Summary", "Details")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=folder & "\Report.pdf"
Worksheets("Summary").Select ' ungroup afterwards
यह उन गिनी-चुनी जगहों में से है जहाँ .Select सच में ज़रूरी है।
6. अच्छी PDF के लिए पेज सेटअप चेकलिस्ट
टेम्पलेट पर: Page Layout → Print Area सेट; Orientation चुना हुआ; Width: 1 page; टेबल कई पेज में जाए तो Print Titles; पेज नंबर वाला हेडर/फ़ुटर। लूप चलाने से पहले एक बार File → Print प्रीव्यू देखें।
आम गलतियाँ
प्रिंट एरिया न होना, जिससे PDF में खाली पेज या फ़ालतू कॉलम आते हैं। कैलकुलेशन मैनुअल होने पर Application.Calculate भूलना (हर PDF में पहला ग्राहक दिखता है)। नामों में अमान्य अक्षर (लेसन 1 का SafeName दोबारा इस्तेमाल करें)। मल्टी-शीट एक्सपोर्ट के बाद शीट ग्रुप में छोड़ देना।