+ Reply to Thread
Results 1 to 18 of 18

Send via email

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Send via email

    Hi Guys,

    I really hope you can help. I am looking to use the below code. However I do not wish to send it as an excel format I wish to send it as a pdf. Any Ideas?

    Option Explicit



    Sub Mail_Every_Workbook()

    'Working in 97-2010

    Dim sh As Worksheet

    Dim wb As Workbook

    Dim FileExtStr As String

    Dim FileFormatNum As Long

    Dim TempFilePath As String

    Dim TempFileName As String

    Dim I As Long



    TempFilePath = Environ$("temp") & "\"



    If Val(Application.Version) < 12 Then

    'You use Excel 97-2003

    FileExtStr = ".xls": FileFormatNum = -4143

    Else

    'You use Excel 2007-2010

    FileExtStr = ".xlsm": FileFormatNum = 52

    End If



    With Application

    .ScreenUpdating = False

    .EnableEvents = False

    End With



    For Each sh In ThisWorkbook.Worksheets

    If sh.Range("A1").Value Like "?*@?*.?*" Then



    sh.Copy

    Set wb = ActiveWorkbook



    TempFileName = "Sheet " & sh.Name & " of " _
    & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")



    With wb

    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

    On Error Resume Next

    For I = 1 To 3
    .SendMail sh.Range("A1").Value, _
    "Huffkins Rota"
    If err.Number = 0 Then Exit For
    Next I

    On Error GoTo 0

    .Close SaveChanges:=False

    End With



    Kill TempFilePath & TempFileName & FileExtStr



    End If

    Next sh



    With Application

    .ScreenUpdating = True

    .EnableEvents = True

    End With

    End Sub
    Last edited by amace87; 03-27-2013 at 09:22 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Send via email

    Hi qmqce87

    Welcome to the Forum!!

    Please note, you must use Code Tags around any Code you post. See Rule #3. Please amend your original post to do so.

    This link will demonstrate most of what you wish to know about converting workbooks or worksheets to PDF and then email as attachments in Outlook.
    If you need help adapting let us know.

    http://www.rondebruin.nl/pdf.htm
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    OK so is this what you were after with rule #3. I do apologise I am new to this. So the below code is what I need adjusting to be able to send PDF instead of xml format. I aleady have a pdf code I just cant figure out how to combine them in order to keep the original code but have it send as pdf instead. please advise.

    Option Explicit
    
    
    
     Sub Mail_Every_Workbook()
    
     'Working in 97-2010
    
     Dim sh As Worksheet
    
     Dim wb As Workbook
    
     Dim FileExtStr As String
    
     Dim FileFormatNum As Long
    
     Dim TempFilePath As String
    
     Dim TempFileName As String
    
     Dim I As Long
    
    
    
     TempFilePath = Environ$("temp") & "\"
    
    
    
     If Val(Application.Version) < 12 Then
    
     'You use Excel 97-2003
    
     FileExtStr = ".xls": FileFormatNum = -4143
    
     Else
    
     'You use Excel 2007-2010
    
     FileExtStr = ".xlsm": FileFormatNum = 52
    
     End If
    
    
    
     With Application
    
     .ScreenUpdating = False
    
     .EnableEvents = False
    
     End With
    
    
    
     For Each sh In ThisWorkbook.Worksheets
    
     If sh.Range("A1").Value Like "?*@?*.?*" Then
    
    
    
     sh.Copy
    
     Set wb = ActiveWorkbook
    
    
    
     TempFileName = "Sheet " & sh.Name & " of " _
     & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
    
    
    
     With wb
    
     .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    
     On Error Resume Next
    
     For I = 1 To 3
     .SendMail sh.Range("A1").Value, _
     "Huffkins Rota"
     If err.Number = 0 Then Exit For
     Next I
    
     On Error GoTo 0
    
     .Close SaveChanges:=False
    
     End With
    
    
    
     Kill TempFilePath & TempFileName & FileExtStr
    
    
    
     End If
    
     Next sh
    
    
    
     With Application
    
     .ScreenUpdating = True
    
     .EnableEvents = True
    
     End With
    
     End Sub
    That was the code to keep and I sort of need the below code to make it pdf.

    Sub Email_ActiveSheet_As_PDF()
    
    'Do not forget to change the email ID
    'before running this code
    
        Dim OlApp As Object
        Dim NewMail As Object
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileFullPath As String
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
    ' Temporary file path where pdf
    ' file will be saved before
    ' sending it in email by attaching it.
    
        TempFilePath = Environ$("temp") & "\"
    
    ' Now append a date and time stamp
    ' in your pdf file name. Naming convention
    ' can be changed based on your requirement.
    
        TempFileName = ActiveSheet.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
    
    'Complete path of the file where it is saved
        FileFullPath = TempFilePath & TempFileName
    
    'Now Export the Activesshet as PDF with the given File Name and path
    
        On Error GoTo err
        With ActiveSheet
            .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=FileFullPath, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
        End With
    
    'Now open a new mail
    
        Set OlApp = CreateObject("Outlook.Application")
        Set NewMail = OlApp.CreateItem(0)
    
        On Error Resume Next
        With NewMail
            .To = "[email protected]"
            .CC = "[email protected]"
            .BCC = "[email protected]"
            .Subject = "Type your Subject here"
            .Body = "Type the Body of your mail"
            .Attachments.Add FileFullPath '--- full path of the pdf where it is saved
            .Send   'or use .Display to show you the email before sending it.
        End With
        On Error GoTo 0
    
    'Since mail has been sent with the attachment
    'Now delete the pdf file from the temp folder
    
        Kill FileFullPath
    
    'set nothing to the objects created
        Set NewMail = Nothing
        Set OlApp = Nothing
    
    'Now set the application properties back to true
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        MsgBox ("Email has been Sent Successfully")
        Exit Sub
    err:
            MsgBox err.Description
    
    End Sub
    Please help.

  4. #4
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    I really need help with the above guys. If someone else could take a look.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Send via email

    Hi amace87

    Are you using Outlook as your Email client?

  6. #6
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    I am yes and am using office 2013.

  7. #7
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    yes I am and office 2013.

  8. #8
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    that's brilliant. Thanks. one last thing. Is there anyway I could add a message box like my first code when the operation is complete. I try to add it at the end but it comes up after every sheet. Is there somewhere specific in the code I could put it to prevent it from doing this?

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Send via email

    Hi amace87

    Place your message box code as indicated
    Sub Mail_Every_Worksheet_With_Address_In_A1_PDF()
    'Working only in 2007 and up
        Dim sh As Worksheet
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileName As String
    
        'Temporary path to save the PDF files
        'You can also use another folder like
        'TempFilePath = "C:\Users\Ron\MyFolder\"
        TempFilePath = Environ$("temp") & "\"
    
        'Loop through every worksheet
        For Each sh In ThisWorkbook.Worksheets
            FileName = ""
    
            'Test A1 for a mail address
            If sh.Range("A1").Value Like "?*@?*.?*" Then
    
                'If there is a mail address in A1 create the file name and the PDF
                TempFileName = TempFilePath & "Sheet " & sh.Name & " of " _
                             & ThisWorkbook.Name & " " _
                             & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
    
                FileName = RDB_Create_PDF(sh, TempFileName, True, False)
    
    
                'If publishing is OK create the mail
                If FileName <> "" Then
                    RDB_Mail_PDF_Outlook FileName, sh.Range("A1").Value, "This is the subject", _
                                         "See the attached PDF file with the last figures" _
                                       & vbNewLine & vbNewLine & "Regards Ron de bruin", False
    
                    'After the mail is created delete the PDF file in TempFilePath
                    If Dir(TempFileName) <> "" Then Kill TempFileName
    
                Else
                    MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
                           "Microsoft Add-in is not installed" & vbNewLine & _
                           "The path to Save the file in arg 2 is not correct" & vbNewLine & _
                           "You didn't want to overwrite the existing PDF if it exist"
                End If
    
            End If
        Next sh
        
        'Place your message box code here <-------------
        
    End Sub

  10. #10
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Send via email

    Absolutely Superb. Thanks for all your help. Exactly what I need. Thanks again.

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Send via email

    You're welcome...glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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