Pookiemeister
Well-known Member
- Joined
- Jan 6, 2012
- Messages
- 626
- Office Version
- 365
- 2010
- Platform
- Windows
I am trying to attach a converted pdf to an email but I get the error in the title. Here's my code:
I was wondering what I might of missed? The error occurs on this line:
I also tried:
When I change the current declaration to "As Object" I get a runtime error '13 Type mismatch. Thank you all who contribute to the solution.
Code:
Private Sub cmdbtnPDF_Save_Click()
Dim sPath As String
Dim sFilename As String
Dim wbAnswer, EmailAnswer As Integer
sPath = "C:\Users\username\Desktop\Saved PDF Test\"
sFilename = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFilename
ActiveWorkbook.Save
EmailAnswer = MsgBox("Do you want to e-mail this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFilename)
If EmailAnswer = vbYes Then
Dim olApp As Outlook.Application
Dim olMailItem As Outlook.MailItem
Dim olAttachment As Outlook.Attachment
Set olApp = CreateObject("Outlook.Application")
Set olMailItem = olApp.CreateItem(olMailItem)
Set olAttachment = olMailItem.Attachments
With olMailItem
.To = ""
.CC = ""
.Subject = ""
.Body = ""
olAttachment.Add sPath & sFilename
.Display
End With
Set olMailItem = Nothing
Set olApp = Nothing
Else
Exit Sub
End If
wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
If wbAnswer = vbYes Then
ActiveWorkbook.Save
Application.Quit
Else
Exit Sub
End If
End Sub
I was wondering what I might of missed? The error occurs on this line:
VBA Code:
Set olMailItem = olApp.CreateItem(olMailItem)
Code:
Dim olApp As Object
Dim olMailItem As Object
Dim olAttachment As Object
When I change the current declaration to "As Object" I get a runtime error '13 Type mismatch. Thank you all who contribute to the solution.
Last edited: