+ Reply to Thread
Results 1 to 3 of 3

Return FolderPath to add attachment to email

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Return FolderPath to add attachment to email

    Hi All,

    I have code which creates a folder and saves a sheet within the folder as PDF. This works fine but when I try to generate an email with PDF attachment I cant seem to get the folder path to return in my code.

    Any help appreciated.

    Sub Macro1()
    Dim PDF_Name As String
    Dim Create_PDF As String
    Dim FolderPath As String
    Dim NewFolderName As String
    
    Dim OutApp As Object
    Dim OutMail As Object
    
    NewFolderName = ActiveSheet.Range("A1").Value
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If ActiveSheet.Range("A2").Value = "A" Then
    
        FolderPath = "C:\" & NewFolderName
    
        MkDir (FolderPath)
    
    End If
    
        PDF_Name = (FolderPath & "\" & ActiveSheet.Range("A3").Value)
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_Name, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
            
            If Dir(PDF_Name) <> "" Then
            
            Create_PDF = PDF_Name
            
        End If
             
         Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = (ActiveSheet.Range("A4").Value)
            .CC = ""
            .BCC = ""
            .Subject = (ActiveSheet.Range("A5").Value)
            .Body = "Text Here"
            .Attachments.Add Create_PDF 'This is where I have the problem
            .Display   'or use .Send
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Return FolderPath to add attachment to email

    I guess your A3 in reality does not include the ".pdf" extension which gets added when the export to pdf is done. hence your Dir fails and Create_PDF never gets a value. either specify the .pdf in the cell or add it in the code.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    11-15-2011
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    163

    Re: Return FolderPath to add attachment to email

    Quote Originally Posted by JosephP View Post
    I guess your A3 in reality does not include the ".pdf" extension which gets added when the export to pdf is done. hence your Dir fails and Create_PDF never gets a value. either specify the .pdf in the cell or add it in the code.
    Doh!

    Thanks JP

+ 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