Email PDF

Jenna_B

New Member
Joined
Sep 22, 2016
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I am having a slight issue with my code, and perhaps I have been looking at it to long BUT, I am looking to have the ability to choose a new file name instead of just overwrite the existing file.
My current Filename string is : DestFolder & "_" & Format(Now(), "yyyymmdd") & "_" & (Environ$("Username")) & ".pdf" and I think I would like the file to save as -1, or -2, or -3 depending on how many filenames have already been found. My full code here:


VBA Code:
Sub create_and_email_pdf()



Dim EmailSubject As String, EmailSignature As String

Dim CurrentMonth As String, DestFolder As String, PDFFile As String

Dim Email_To As String, Email_CC As String, Email_BCC As String

Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean

Dim OverwritePDF As VbMsgBoxResult

Dim OutlookApp As Object, OutlookMail As Object

CurrentMonth = ""

PDFFile = "Valve Conversion Request" & "_" & Format(Now(), "yyyymmdd") & "_" & (Environ$("Username")) & ".pdf"

DestFolder = "I:\Valve Conversion Requests"





    EmailSubject = "Valve Conversion Request"

    OpenPDFAfterCreating = False   

    AlwaysOverwritePDF = False 

    DisplayEmail = True

    Email_To = ActiveSheet.Range("N1")

    Email_CC = ""

    Email_BCC = ""

    



    CurrentMonth = Mid(ActiveSheet.Range("N2").Value, InStr(1, ActiveSheet.Range("N2").Value, " ") + 1)

  



    PDFFile = DestFolder & "_" & Format(Now(), "yyyymmdd") & "_" & (Environ$("Username")) & ".pdf"





    If Len(Dir(PDFFile)) > 0 Then

    

        If AlwaysOverwritePDF = False Then

        

            OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")

        

            On Error Resume Next



            If OverwritePDF = vbYes Then

    

                Kill PDFFile

        

            Else

    

                MsgBox "Unable to continue." _

                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"

                

                Exit Sub

        

            End If



        Else

        

            On Error Resume Next

            Kill PDFFile

          

        End If

      

        If Err.Number <> 0 Then

      

            MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _

                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"

              

            Exit Sub

      

        End If

          

    End If

 

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _

        :=False, OpenAfterPublish:=OpenPDFAfterCreating



    Set OutlookApp = CreateObject("Outlook.Application")

    Set OutlookMail = OutlookApp.CreateItem(0)

      

    With OutlookMail

      

        .Display

        .To = Email_To

        .CC = Email_CC

        .BCC = Email_BCC

        .Subject = EmailSubject

        .Attachments.Add PDFFile

              

        If DisplayEmail = False Then

          

            .Send

          

        End If

      

    End With

  



End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I fixed this... I figured what I filled out actually contains a field I could reference that has a unique identifier in it.
However, my issue is now that the PDF file isn't saving to the destination folder....
 
Upvote 0
Solution

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top