VBA code to convert excel to pdf and email it as attachment

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,882
Office Version
2013
Platform
Windows
Change the line of code that is
Code:
.Send

AND REPLACE it with 


.Display
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,882
Office Version
2013
Platform
Windows
Hi and welcome to the Forum, but in future please start a new thread, if you have a question.
To naswer your current question, the code will work in 2003
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,659
Dear ZVI,

Please help me to convert excel chart into pdf file by using macro .
Hi,
Create chart on a sheet, save workbook in any folder and use the below code to export active sheet to the PDF file.
Rich (BB code):
Sub SheetToPdf()
  Dim PdfFile As String
  
  ' Check compatibility of Excel version
  If Val(Application.Version) < 12 Then
    MsgBox "Export to PDF requires Excel 2007+", vbExclamation, "SheetToPdf"
    Exit Sub
  End If
  
  ' Define PDF filename
  PdfFile = ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name, ".xlsm", "") & "_" & ActiveSheet.Name & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Report
   MsgBox "PDF file:" & vbLf & PdfFile, vbInformation, "SheetToPdf"
 
End Sub
Regards,
 

gopipallan

New Member
Joined
Oct 22, 2013
Messages
3
Thank You for your prompt response.

but still no luck i'm getting an error of invalid argument on below line

.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Thanks & regards

Raja pallan

Hi,
Create chart on a sheet, save workbook in any folder and use the below code to export active sheet to the PDF file.
Rich (BB code):
Sub SheetToPdf()
  Dim PdfFile As String
  
  ' Check compatibility of Excel version
  If Val(Application.Version) < 12 Then
    MsgBox "Export to PDF requires Excel 2007+", vbExclamation, "SheetToPdf"
    Exit Sub
  End If
  
  ' Define PDF filename
  PdfFile = ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name, ".xlsm", "") & "_" & ActiveSheet.Name & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Report
   MsgBox "PDF file:" & vbLf & PdfFile, vbInformation, "SheetToPdf"
 
End Sub
Regards,
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,882
Office Version
2013
Platform
Windows
You are using Excel 2007 or higher ???
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,882
Office Version
2013
Platform
Windows
Try this one, using the old .xls file extension
Code:
Sub SheetToPdf()
  Dim PdfFile As String
  
  ' Check compatibility of Excel version
  If Val(Application.Version) < 12 Then
    MsgBox "Export to PDF requires Excel 2007+", vbExclamation, "SheetToPdf"
    Exit Sub
  End If
  
  ' Define PDF filename
  PdfFile = ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name, ".xls", "") & "_" & ActiveSheet.Name & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Report
   MsgBox "PDF file:" & vbLf & PdfFile, vbInformation, "SheetToPdf"
 
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,099,508
Messages
5,469,030
Members
406,628
Latest member
jared92

This Week's Hot Topics

Top