macro to save a worksheet by the tab name as a pdf then open in outlook

SKITZMAN

New member
Joined
Nov 22, 2016
Messages
5
Reaction score
0
Points
0
Location
TEXAS
This is my third attempt at asking for help, no luck on the other 2 forums so I found this one (fingers crossed). I have created a macro button on a worksheet and if you hit the button its supposed to save the current worksheet as a PDF with the name of the tab, then open up outlook and attach it. I have it close it does save the worksheet as a pdf and open my outlook with the 2 contacts I need to email it to then I just hit send. Although my issue is I need it to save the pdf as the name on the current tab not the workbook name.
I have attached my code that works but doesn't save the pdf as the TAB name. Any help in this issue I would be forever grateful.

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 = ActiveSheet.Name

' 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 = ("mypurchasingagent")' fill in who is getting the email
.CC = ("myshippingdept") 'fill in who is getting the email
.Body = "Hello," & vbLf & vbLf _
& "This material has been ordered and set to arrive." & vbLf & vbLf _
& "Regards," & vbLf _
& Application.UserName & vbLf & vbLf
.Attachments.Add PdfFile

' Try to send
On Error Resume Next
.Display
Application.Visible = True
If Err Then
MsgBox "Unable to prepare E-mail", vbExclamation
Else
MsgBox "E-mail successfully prepared", 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
 
The problem seems to be a simple mistake you are overlooking.
you have PdfFile = ActiveWorkbook.FullName and at the same time you say this is not what you want.
Following your code logic put Pdffile = Title.
Of course this will break your code few lines down but I am assuming you can fix this without further help.
 
here is my spreadsheet

I have tried to go in and fix mine to do like it the examples and I can not get it. Could you look at it and let me know what I have wrong?
 

Attachments

  • START WORKORDER ITEMIZED LIST REV 4.5 MIKE CLOSE.xlsm
    178.3 KB · Views: 16
The file you posted, three weeks ago, in #5 at EF, saves the sheet as a pdf the way you want.
Why have you changed PdfFile back to =ActiveWorkbook.FullName ?

When no folder is specified for saving a file, Excel saves to the default location set in Excel Options.

I don't use Outlook on this computer, so this is not tested.
Try the Outlook attachments line like this
Code:
.Attachments.Add Application.DefaultFilePath & PdfFile
 
I understand it saves the worksheet as a pdf I want it to save it as the name on the tab not the workbook name. I somehow got it to work 2 times but when I closed it and opened it back up it didn't work correct.
 
I want it to save it as the name on the tab not the workbook name
Then change the code back to what it was 3 weeks ago.

Download the file you posted at EF and try it.

I've now tested with Outlook and the attachment line needs to be
Code:
.Attachments.Add Application.DefaultFilePath & "\" &  PdfFile
 
Thank you so very much for all your help. it works awesome now.
 
Cross post without links http://www.excelforum.com/showthread.php?t=1160782&p=4517092&highlight=#post4517092

Please read this to understand why and add links to ALL your cross posts
 
will do thank you again for all your help. I am going to go to the other forums and post that this has been resolved so I don't waste anyones time.
 
Back
Top