+ Reply to Thread
Results 1 to 4 of 4

Script to save as PDF and Email through Outlook

  1. #1
    Registered User
    Join Date
    04-19-2016
    Location
    US
    MS-Off Ver
    2013
    Posts
    2

    Script to save as PDF and Email through Outlook

    I have the following script that I want to use to save an excel workbook as a pdf and email it through Outlook. I have this file on my computer and the script works perfectly. As soon as I send the file to someone else, it doesn't work. Throws a Runtime 1004 error. I will color the area of the script that it shows wrong when hitting the debug button red.

    Script:


    Sub EmailPDF()
    fdir = "c:\\temp\\"
    fname = InputBox("Please Enter Filename")
    fpath = fdir & fname & ".pdf"
    With Application
    .EnableEvents = True
    .ScreenUpdating = False
    End With

    ToAddress = "email@domain.com"
    CCAddress = Range("C1")
    CCAddress2 = Range("i10")
    CCAddress3 = CCAddress & "; " & CCAddress2
    MailSub = "Order " & fname

    'Generate PDF document to c:\. Substitute ActiveSheet for ActiveWorkbook to PDF the entire document
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    fpath, Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False



    'Create Mail & attach PDF
    Set oOutlookApp = CreateObject("Outlook.Application")

    'Create a new message
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
    .To = ToAddress
    .CC = CCAddress3
    .Subject = MailSub

    'Bring up new mail window
    oItem.Display

    'Add Attachment
    oItem.Attachments.Add fpath

    'Cleanup , baby
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set fs = CreateObject("Scripting.FileSystemObject")

    fs.deletefile fpath

    End With
    End Sub


    Any help would be greatly appreciated. I am still learning when it comes to using these scripts so be easy on me please! Thanks in advance.

  2. #2
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Script to save as PDF and Email through Outlook

    It looks like you have too many forward slashes in your directory. Try changing it to:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-19-2016
    Location
    US
    MS-Off Ver
    2013
    Posts
    2

    Re: Script to save as PDF and Email through Outlook

    Hi spitfireblue,

    Thanks for your reply.

    That doesn't seem to be the problem. When debugging it shows a yellow arrow next to the last line of this section:

    'Generate PDF document to c:\. Substitute ActiveSheet for ActiveWorkbook to PDF the entire document
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    fpath, Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False

    I made the change you suggested and it still works fine on my computer and it gets passed the first 2 sections of the code and then fails at the above stated line on any one else computer.

  4. #4
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Script to save as PDF and Email through Outlook

    Hmm, not sure then. Maybe an issue with Print Area's. Try running that section on 3 different lines and see where the error comes up.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Email rule in outlook with VBA script on shared mailbox
    By CR7 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2015, 09:01 AM
  2. [SOLVED] VBA - Save, and Embed in Email (Outlook)
    By jlo33 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 07-19-2013, 10:10 AM
  3. Save range and send as outlook email
    By hejbeiter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2012, 12:50 PM
  4. VB to save word doc with a filename just copied from an Outlook email
    By chrisvelnet in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2011, 09:28 AM
  5. save email in Outlook
    By Jessebraswell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2009, 10:27 AM
  6. [SOLVED] Reply to outlook email from excel script
    By theerdman@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-21-2006, 12:00 PM

Tags for this Thread

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