+ Reply to Thread
Results 1 to 5 of 5

Active Worksheet to PDF to Email VBA code

  1. #1
    Registered User
    Join Date
    09-12-2018
    Location
    Port of Spain, Trinidad
    MS-Off Ver
    Excel 16
    Posts
    53

    Active Worksheet to PDF to Email VBA code

    Hi all, I've spent the good part of today trying to solve this VBA code but with no tangible success. I am very inexperienced in Macros and VBA but at least I tried.

    I want to be able to turn a worksheet into a PDF that is then emailed.

    I found a code online but for some reason it doesn't seem to work. vba error msg.PNGvba error line.PNG

    If I understand parts of the code correctly
    I want the "Title" to be cell C6 and also to be part of the PDF file name, if this file is being saved. If the file is not being saved then it doesn't need to have Title in it I guess.
    I want a string called "Name" to be cell G6 wherein this is the "To:" field of the email being sent.

    I have attached the workbook as well.

    VBA code
    Sub AttachActiveSheetPDF()
    Dim IsCreated As Boolean
    Dim i As Long
    Dim PdfFile As String, Title As String
    Dim OutlApp As Object

    ' Not sure for what the Title is
    Title = Range("A1")

    ' Define PDF filename
    PdfFile = ActiveWorkbook.FullName
    i = InStrRev(PdfFile, ".")
    If i > 1 Then PdfFile = Left(PdfFile, i - 1)
    PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"

    ' Export activesheet as PDF
    With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With

    ' Use already open Outlook if possible
    On Error Resume Next
    Set OutlApp = GetObject(, "Outlook.Application")
    If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
    End If
    OutlApp.Visible = True
    On Error GoTo 0

    ' Prepare e-mail with PDF attachment
    With OutlApp.CreateItem(0)

    ' Prepare e-mail
    .Subject = Title
    .To = "..." ' <-- Put email of the recipient here
    .CC = "..." ' <-- Put email of 'copy to' recipient here
    .Body = "Hi," & vbLf & vbLf _
    & "The report is attached in PDF format." & vbLf & vbLf _
    & "Regards," & vbLf _
    & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile

    ' Try to send
    On Error Resume Next
    .Send
    Application.Visible = True
    If Err Then
    MsgBox "E-mail was not sent", vbExclamation
    Else
    MsgBox "E-mail successfully sent", vbInformation
    End If
    On Error GoTo 0

    End With

    ' Delete PDF file
    Kill PdfFile

    ' Quit Outlook if it was created by this code
    If IsCreated Then OutlApp.Quit

    ' Release the memory of object variable
    Set OutlApp = Nothing
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: Active Worksheet to PDF to Email VBA code

    .
    This will provide the TO in cell G6 :

    Please Login or Register  to view this content.
    You can utilize the same format for the CC as well.


    Not certain what you mean by TITLE when saved or not saved ???

  3. #3
    Registered User
    Join Date
    09-12-2018
    Location
    Port of Spain, Trinidad
    MS-Off Ver
    Excel 16
    Posts
    53

    Re: Active Worksheet to PDF to Email VBA code

    Thank you for the assistance on the To part in cell g6. My comment about the file being saved referred to the Kill PDF file command near the end. No need to stress over this as it is also insignificant really.

    Were u able to get the overall code to work though?

    My main dilemma is that code doesnt run fully. There is a "Run time error 91" that points to the line with OutlApp.CreateItem(0) still exists.

    Also, I dont know if there are problems with other parts of the code. Ideally I wanted someone to either use the code given as a template to fix whatever errors there might be or help me to come up with another code that works.

    I hope this clarifies.

  4. #4
    Registered User
    Join Date
    09-12-2018
    Location
    Port of Spain, Trinidad
    MS-Off Ver
    Excel 16
    Posts
    53

    Re: Active Worksheet to PDF to Email VBA code

    Ok this morning somehow the entire code is working. So I am just fiddling with a few lines here and there to customize it. Thank you very much for your assistance.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: Active Worksheet to PDF to Email VBA code

    .
    You are welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Email the active worksheet via Gmail (instead of outlook)
    By JamesReyes in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2018, 08:14 PM
  2. Email Active Worksheet via Gmail
    By cturpin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2016, 11:53 AM
  3. Email Select Pages of Active Worksheet as PDF
    By Beachmonkey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2016, 02:48 PM
  4. [SOLVED] Email Macro to attach a non active worksheet to outlook email
    By mickgibbons1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-21-2013, 08:38 PM
  5. [SOLVED] Save active worksheet to pdf and email
    By wolfm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 02:19 AM
  6. VBA - save active worksheet as HTML & Email
    By gpjb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-05-2005, 10:12 PM
  7. [SOLVED] email active worksheet
    By es in forum Excel General
    Replies: 2
    Last Post: 01-31-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1