Export as PDF file and Auto Save
Sub ExportAsPDFAutoSave()
Dim FileName As String
' Auto-generate PDF file name with timestamp
FileName = ThisWorkbook.Path & "\" & _
"Report_" & Format(Now, "yyyymmdd_hhmmss") & ".pdf"
' Export the active sheet directly as PDF (auto-saved)
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=FileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF saved as:" & vbCrLf & newFileName, vbInformation, "Export Complete"
End Sub
यह VBA code automatic रूप से active Excel sheet को PDF file के रूप में export करता है और उसे उसी folder में save करता है जहां workbook located है।
यह step by step इस प्रकार काम करता है:
- FileName variable - यह PDF के लिए automatic से एक file name बनाता है। Name "Report_" से शुरू होता है और उसके बाद current date और time (yyyymmdd_hhmmss format में) लिखा होता है। यह सुनिश्चित करता है कि प्रत्येक PDF का एक unique name हो और पुराने names को overwrite न किया जाए।
Example: Report_20251108_184500.pdf. - ExportAsFixedFormat method - यह command Excel को active worksheet को PDF format में export करने के लिए कहता है।
- Type:=xlTypePDF का अर्थ है कि file PDF होगी।
- FileName:=FileName, save करने के लिए पूरा path और name देता है।
- Quality:=xlQualityStandard normal print quality बनाए रखता है।
- IncludeDocProperties:=True में title या author जैसी workbook जानकारी शामिल होती है।
- IgnorePrintAreas:=False का अर्थ है कि यह defined print area का उपयोग करता है।
- OpenAfterPublish:=False का अर्थ है कि यह save करने के बाद PDF नहीं खोलेगा।
- Export के बाद, एक message box दिखाई देता है जो दर्शाता है कि PDF को उसके name और location के साथ सफलतापूर्वक save कर दिया गया है।
संक्षेप में, यह macro manual input की आवश्यकता के बिना आपकी Excel sheet को automatic रूप से PDF के रूप में बनाकर, name देकर और save कर समय बचाता है।
Export Excel Sheet to PDF
Sub ExportSheetToPDF()
Dim FilePath As String
FilePath = ThisWorkbook.Path & "\MySheet.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
इस VBA code का उपयोग Excel में current में active worksheet को PDF file के रूप में export करने और save करने के बाद उसे automatic रूप से खोलने के लिए किया जाता है।
प्रत्येक भाग step by step क्या करता है, यहाँ बताया गया है:
- FilePath variable declare करें:
Dim FilePath As String एक variable बनाता है जो पूरी file location को store करेगा जहाँ PDF save होगी। - File path set करें:
FilePath = ThisWorkbook.Path & "\MySheet.pdf" Excel को बताता है कि PDF को उसी folder में save करें जहाँ Excel workbook store है। PDF file का नाम “MySheet.pdf” होगा। - Sheet को PDF के रूप में export करें:
ActiveSheet.ExportAsFixedFormat command अभी active sheet को PDF file में बदल देता है। - Type:=xlTypePDF यह बताता है कि output एक PDF file होनी चाहिए।
- Filename:=FilePath save करने की जगह और file का नाम set करता है।
- Quality:=xlQualityStandard standard print quality ensure करता है।
- IncludeDocProperties:=True author या tile जैसी properties को शामिल करता है।
- IgnorePrintAreas:=False सिर्फ़ defined print area (अगर कोई है) का इस्तेमाल करता है।
- OpenAfterPublish:=True save करने के बाद नई बनी PDF file को अपने आप खोलता है।
संक्षेप में, यह macro मौजूदा Excel sheet को MySheet.pdf नाम की PDF file में बदल देता है, उसे आपकी workbook के पास save करता है, और तुरंत देखने के लिए उसे खोलता है।
Save Specific Range as PDF
Sub ExportRangeToPDF()
Dim rng As Range
Set rng = Range("A1:D20")
rng.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\RangeExport.pdf"
End Sub
यह VBA code active worksheet से एक specific cell range (A1:D20) को PDF file के रूप में export करता है।
- यह सबसे पहले एक range variable rng define करता है और उसे A1 से D20 cell पर set करता है।
- rng.ExportAsFixedFormat command सिर्फ़ उस selected range को PDF file में convert करता है।
- PDF उसी folder में save होती है जहाँ Excel workbook है, fileका नाम “RangeExport.pdf” होता है।
संक्षेप में, यह macro आपको अपनी sheet का सिर्फ़ एक खास हिस्सा (पूरी worksheet नहीं) एक अच्छे format वाले PDF document के रूप में automatically save करने में मदद करता है।
Export Multiple Sheets as One PDF
Sub ExportMultipleSheetsToPDF()
Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\MultiSheets.pdf"
End Sub
यह VBA code एक Excel workbook से कई sheets को एक combined PDF file में export करता है।
- Sheets(Array("Sheet1", "Sheet2")).Select line Sheet1 और Sheet2 नाम की दो sheets को एक साथ select करती है।
- फिर ActiveSheet.ExportAsFixedFormat command इन selected sheets को एक single PDF document में convert करता है।
- PDF file workbook वाले ही folder में “MultiSheets.pdf” नाम से save हो जाती है।
संक्षेप में, यह macro आपको हर sheet को अलग-अलग export करने के बजाय, कई worksheet को एक साथ मिलाकर एक PDF file में जल्दी से save करने में मदद करता है।
Convert Word Document to PDF (From Word)
Sub SaveAsPDF()
Dim FilePath As String
FilePath = ActiveDocument.Path & "\" & _
Replace(ActiveDocument.Name, ".docx", ".pdf")
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=FilePath, _
ExportFormat:=wdExportFormatPDF
End Sub
यह VBA code Microsoft Word में मौजूदा document को PDF file के रूप में save करने के लिए इस्तेमाल होता है।
- यह एक variable FilePath बनाता है जो PDF file की पूरी location और name store करता है।
- यह code मौजूदा Word file name में .docx extension को .pdf से बदल देता है, और उसे उसी folder में रखता है।
- ActiveDocument.ExportAsFixedFormat command active Word document को PDF में बदल देता है।
- OutputFileName यह set करता है कि इसे कहाँ save करना है, और ExportFormat:=wdExportFormatPDF Word को बताता है कि इसे PDF format में export करना है।
संक्षेप में, यह आपकी खुली हुई Word file को automatically PDF version के रूप में save कर देता है।
Attach Generated PDF to Outlook Email
Sub ExportToPDF_AttachEmail()
Dim FilePath As String
FilePath = ThisWorkbook.Path & "\Report.pdf"
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, Filename:=FilePath
Dim OutApp As Object, OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "example@mail.com"
.Subject = "PDF Report"
.Body = "Please find attached the report."
.Attachments.Add FilePath
.Display 'use .Send to auto-send
End With
End Sub
यह VBA code active Excel sheet को PDF के रूप में export करता है और फिर उस PDF को attach करके Outlook में एक email बनाता है।
- यह सबसे पहले FilePath को set करता है ताकि PDF को Excel file वाले ही folder में “Report.pdf” के रूप में save किया जा सके।
- ExportAsFixedFormat command active sheet को PDF file में बदल देता है।
- इसके बाद यह Microsoft Outlook खोलता है और एक नया email message बनाता है।
- Email में पाने वाले का पता, subject और body text भरा जाता है, और save की गई PDF अपने आप attach हो जाती है।
- आखिर में, email review के लिए display किया जाता है (या अगर .Send का इस्तेमाल किया जाता है तो अपने आप भेज दिया जाता है)।
Print PDF Automatically (Using Shell)
Sub PrintPDF()
Dim FilePath As String
FilePath = "C:\Users\Public\Document.pdf"
Shell "cmd /c start acrord32.exe /p /h """ & FilePath & """", vbHide
End Sub
यह VBA code Adobe Acrobat Reader का इस्तेमाल करके PDF file को automatically print करने के लिए इस्तेमाल किया जाता है।
- यह एक variable FilePath define करता है जो print होने वाली PDF file की location store करता है - यहाँ यह "C:\Users\Public\Document.pdf" है।
- Shell command एक Windows command चलाता है जो background में Acrobat Reader (acrord32.exe) खोलता है और PDF को default printer पर भेजता है।
- /p और /h option Acrobat को चुपचाप print करने और print करते समय window को hide के लिए कहते हैं।
संक्षेप में, यह macro Acrobat window दिखाए बिना, एक खास PDF file को तेज़ी से automatically print करता है।
