Results 1 to 7 of 7

Thread: Need Macro Help to send a worksheet as PDF

  1. #1

    Need Macro Help to send a worksheet as PDF



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi Ken

    I am new to macro concept

    I need help to create a macro - which will enable to send the worksheet as PDF to email

    I have 32 worksheets

    1st sheet contains - mail id and person name
    2nd sheet - contains data (ex: salary details)
    3rd to 30th sheet - having the extracts from data (ie in the form of Payslip) - worksheet name same as per the person name in the 1st sheet

    Now I need the macro as below

    when i run a macro - it's has to create the worksheets(3rd to 30th) as pdf and need to mail to the persons individually as per the list in the first sheet

    example

    3rd sheet for me - it's need to be converted as pdf and has to be mailed to me alone
    4th sheet for my collegue X - it's need to be converted as pdf and has to be mailed to x alone

    Am using Excel 2007 and outlook 2007

  2. #2
    Hi KEN

    I have managed to generate the macro codings to convert the excel sheet range in to PDF and to send e-mail. Can you helpme

    1. How to encrypt the PDF with password protection in the same macro?
    2. How to encrypt the PDF to allow users to print the PDF only (ie no changes/copy of the text in the PDF)

    Am using PDFcreator as a PDF genearator / Outlook 2007 as a mail client

    Please help me

    Thanks in advance

  3. #3
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,231
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Can you post the code that you've come up with so far?

    PS, I split this to a new thread so that we can focus on your issue specifically.
    Ken Puls, FCPA, FCMA, MS MVP (Excel)

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training) or with my book M is for Data Monkey!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  4. #4
    Hi Ken

    Please find the codes as below

    Code:
    Sub PS_1()
    'Dim pdfjob As Object
    'Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
    Dim sPDFName As String
    Dim sPDFPath As String
    Dim bRestart As Boolean
    Dim StrTo As String
    Dim StrSubject As String
    Dim StrBody As String
    Dim Send As Boolean
    Dim OutApp As Object
    Dim OutMail As Object
    '/// Change the output file name here! ///
    sPDFName = "XX.pdf"
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    Dim pdfjob As Object
    Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
    'Check if worksheet is empty and exit if so
    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
    'Activate error handling and turn off screen updates
    On Error GoTo EarlyExit
    Application.ScreenUpdating = False
    'Check if PDFCreator is already running and attempt to kill the process if so
    With pdfjob
    If .cStart("/NoProcessingAtStartup") = False Then
    MsgBox "PDFCreator is Open.", vbCritical + _
    vbOKOnly, "PrtPDFCreator"
    Exit Sub
    End If
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0 ' 0 = PDF
    .cClearCache
    End With
    'Print the document to PDF
    ActiveSheet.PageSetup.PrintArea = "$B$11:$K$27"
    ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
    'Wait until the print job has entered the print queue
    Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
    Loop
    pdfjob.cPrinterStop = False
    'Wait until PDF creator is finished then release the objects
    Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
    Loop
    ' pdfjob.cClose
    ' Set pdfjob = Nothing
    '\\\\\ End Saving PDF
    'Delete the PDF if it already exists
    If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
    'Print the document to PDF
    ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
    'Wait until the print job has entered the print queue
    Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
    Loop
    pdfjob.cPrinterStop = False
    'Wait until the file shows up before closing PDF Creator
    Do
    DoEvents
    Loop Until Dir(sPDFPath & sPDFName) = sPDFName
    '/////// Send PDF Invoice in Email
    Dim toEmail As String
    toEmail = Range("master!c3").Value
    tosub = Range("master!b1").Value
    toname = Range("master!b3").Value
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    .To = toEmail
    .CC = ""
    .BCC = ""
    .Subject = "PayAdvice for the month of " & tosub
    '.Body = "Invoice for blah blah blah"
    .Body = "Dear " & toname & vbNewLine & _
    "" & vbNewLine & _
    "Attached, please find a PayAdvice for the month of " & tosub & vbNewLine & _
    "" & vbNewLine & _
    "Please Contact Y If You Have any Questions." & vbNewLine & _
    "" & vbNewLine & _
    "" & vbNewLine & _
    "Thank You !"
    .attachments.Add ("C:\Documents and Settings\Muthu\Desktop\XX.pdf")
    '.Attachments.Add sPDFPath & "\" & PrevInv
    '.Send 'or use .Display
    .Display
    '.Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    '//////////// Save PDF
    Skipemail:
    '/// Close PDF File
    pdfjob.cClose
    Set pdfjob = Nothing
    Cleanup:
    'Release objects and terminate PDFCreator
    'pdfjob.cClose
    'Set pdfjob = Nothing
    'Shell "taskkill /f /im PDFCreator.exe", vbHide
    On Error GoTo 0
    'ActiveWorkbook.Save
    Application.ScreenUpdating = True
    Dim killfile As String
    killfile = "C:\Documents and Settings\Muthu\Desktop\XX.pdf"
    'Áheck that file exists
    If Len(Dir$(killfile)) > 0 Then
    'first remove readonly attribute, if set
    SetAttr killfile, vbNormal
    'then delete the file
    Kill killfile
    End If
    Exit Sub
    EarlyExit:
    'Inform user of error, and go to cleanup section
    MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
    "has been terminated. Please try again.", _
    vbCritical + vbOKOnly, "Error"
    Resume Cleanup
    End Sub

  5. #5
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,231
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Okay, cool.

    See up near the top where there are all those lines that start with .cOption? Plunk this in above the .cClearCache:

    Code:
            'The following are required to set security of any kind
            .cOption("PDFUseSecurity") = 1
            .cOption("PDFOwnerPass") = 1
            .cOption("PDFOwnerPasswordString") = sMasterPass
            'To set individual security options
            .cOption("PDFDisallowCopy") = 1
            .cOption("PDFDisallowModifyContents") = 1
            .cOption("PDFDisallowPrinting") = 1
            'To force a user to enter a password before opening
            .cOption("PDFUserPass") = 1
            .cOption("PDFUserPasswordString") = sUserPass
            'To change to High encryption
            .cOption("PDFHighEncryption") = 1
    You'll also want to add this around the other DIM statements right near the top of the macro:
    Code:
    Dim sMasterPass as string
    Dim sUserPass as string
    sMasterPass = "Admin"
    sUserPass = "Password"
    Change your sMasterPass to what you want to use, and the next to what you want to give to your users.

    For each of the options above 1 is on, and 0 is off.

    Hope this helps,
    Ken Puls, FCPA, FCMA, MS MVP (Excel)

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training) or with my book M is for Data Monkey!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  6. #6
    Hi Ken

    Cool its working fine.

    Thanks for your great work......

  7. #7

    Help..! Ken

    Quote Originally Posted by rrmuthukumar View Post
    Hi Ken

    Cool its working fine.

    Thanks for your great work......
    Hi Ken...! Need some , I am also using below code to solve my issue... But I am working on bit different lines. I have an excel file which has a database of salary details... like Emp ID, Date of Joining,Emp Name, Payments, eMail ID etc.. On sheet named as "DB", I also have a another sheet named as Payslip . I am looking for a module which could help me creating password protected PDF Files...... The Password would be combination of EMPID & MMYY. I would like to created PDF Files which are password protected using this dynamic code created with combination... All this has to be done for all entries in the sheet DB and should prepare these files in LOOP.. I guess I would be able to add code for emailing these created PDF Files via email ... Do you think you would be able to help.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •