Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Mar 2007
    Location
    Wikltshire UK
    Posts
    152
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Kill Command Nor Working (Outlook 2007)

    Hi,

    I've a Sub Routine I'm passing a Mail Object (itm) to with Attachments (.att). It prints the Object and the Attachement Saves it to a Temporary Directory, but when I Include the Kill Command it Dosn't seem to work at all?? Any Ideas??
    As I say I only need to Delete the Temporary File at this point, and it Dosn't seem to work??

    Option Explicit

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

    Private Const SW_SHOWMAXIMIZED As Long = 3

    Sub PrintAttachments(itm As MailItem)

    itm.PrintOut
    Const strLog = "C:Log.txt"
    Const strAvoid = "deutsch"
    Dim att As Attachment
    Dim strTempPath As String
    Dim strFile As String
    Dim f As Integer
    Dim lngResult As Long
    Dim strFrom As String

    On Error GoTo ErrHandler

    strTempPath = "C:Temp"
    'If Not Right(strTempPath, 1) = "/" Then
    ' strTempPath = strTempPath & "/"
    'End If
    ' Log file
    f = FreeFile
    Open strLog For Append As #f
    ' Name of sender
    strFrom = itm.SenderName
    ' Loop through attachments
    For Each att In itm.Attachments
    ' Name of attachment
    strFile = att.FileName
    ' Test that "deutsch" does not occur in filename
    If InStr(1, strFile, strAvoid, vbTextCompare) = 0 Then
    ' Save attachment to temporary file
    att.SaveAsFile strTempPath & strFile
    ' Print it
    lngResult = ShellExecute(0&, "Print", strTempPath & strFile, 0&, 0&, SW_SHOWMAXIMIZED)
    If lngResult <= 32 Then
    MsgBox "Couldn’t print " & strFile, vbExclamation
    End If
    ' Delete temp file
    Kill strTempPath & strFile
    ' Write to log
    Write #f, strFrom, strFile
    End If
    Next att
    ExitHandler:
    On Error Resume Next
    Close #f
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Kill Command Nor Working (Outlook 2007)

    Do you get an error message? If so, you'll probably have to build in a delay after the call to ShellExecute. The printing of the document may not be finished yet, so the document may still be in use.

    Put the following declaration at the top of the module, before Subs and Functions:

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Insert the following line above the Kill line:

    Sleep 5000

    This will pause 5 seconds (5,000 milliseconds) before trying to delete the file.

  3. #3
    2 Star Lounger
    Join Date
    Mar 2007
    Location
    Wikltshire UK
    Posts
    152
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Kill Command Nor Working (Outlook 2007)

    Yep Thats Solved it.

    Thanks HansV.
    Thanks for the Original Code aswell btw.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Kill Command Nor Working (Outlook 2007)

    You can experiment with the value for Sleep - 1000 is probably too short, but you may be able to get away with 2000 or 3000, depending on the documents.

Posting Permissions

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