View Single Post
 
Old 12-06-2017, 05:38 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Without access to the workbook, I can only point you in the right direction. The process itself is simple enough. You will need a function in your workbook that takes the ID from the e-mail, creates the PDF and returns the PDF path and name. The basics are

Code:
Function MacroName(strID As String) As String
    'Run your workbook code here
    MacroName = "C:\path\" & strID & ".pdf" ' return the PDF path and name
End Function
In Outlook, you need a rule to identify the messages that require the reply and then run the script xlGetData to process those messages as they arrive e.g. as follows.

Change the various parts to reflect the true names of your workbook and the function. If you are comfortable with VBA, you should be able to make this work with your workbook. It tests OK with the examples used.

Code:
Option Explicit

Public Sub xlGetData(olItem As MailItem)
Const strWorkBook As String = "C:\Path\Workbookname.xlsm" 'The name of the workbook
Dim xlApp As Object
Dim xlWB As Object
Dim strID As String
Dim strPDF As String
Dim bStarted As Boolean
Dim olReply As MailItem
Dim fso As Object
    strID = olItem.Subject
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err <> 0 Then
        Set xlApp = CreateObject("Excel.Application")
        bStarted = True
    End If
    On Error GoTo 0
    Set xlWB = xlApp.workbooks.Open(strWorkBook)
    strPDF = xlApp.Run("'" & xlWB.Name & "'!MacroName", strID)

    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(strPDF) Then
        Set olReply = olItem.Reply
        With olReply
            .Attachments.Add strPDF
            .Display
            '.Send 'Restore this line after testing
        End With
Debug.Print strID & vbTab & strPDF
    Else
        MsgBox "The file" & vbCr & strPDF & vbCr & "does not exist."
    End With
lbl_Exit:
    xlWB.Close 0
    If bStarted Then
        xlApp.Quit
    End If
        Set xlApp = Nothing
        Set xlWB = Nothing
    Exit Sub
End Sub
You can run the following macro with a selected message to test the process
Code:
Sub TestMacro()
Dim olMsg As MailItem
    On Error Resume Next
    Set olMsg = ActiveExplorer.Selection.Item(1)
    xlGetData olMsg
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 12-07-2017 at 01:49 AM.
Reply With Quote