none
Automated Attachment Printing Outlook 2010 Error RRS feed

  • Question

  • Hello all,

    I am trying to set up automatic printing of attachments for my email and am running into an error. I have the following code and have set my rule and also made sure to have the reference set as well but keep getting an error code "91-Object variable or With block variable not set" where did I go wrong???

    Sub LSPrint(Item As Outlook.MailItem)
        On Error GoTo OError
          
        'detect Temp
        Dim oFS As FileSystemObject
        Dim sTempFolder As String
        Dim sFileType As String
        Set oFS = New FileSystemObject
        'Temporary Folder Path
        sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
        
        'creates a special temp folder
        cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
        MkDir (cTmpFld)
        
        'save & print
        Dim oAtt As Attachment
        For Each oAtt In Item.Attachments
          FileName = oAtt.FileName
          Fullfile = cTmpFld & "\" & FileName
          
          'save attachment
          
        sFileType = LCase$(Right$(oAtt.FileName, 4))
     
        Select Case sFileType
     
    ' Add additional file types below
     ' use shell to print using default app
           Case ".xls", "xlsx", ".doc", "docx", ".jpg", ".png", ".pdf"
          
          'prints attachment
          Set objShell = CreateObject("Shell.Application")
          Set objFolder = objShell.NameSpace(0)
          Set objFolderItem = objFolder.ParseName(Fullfile)
          objFolderItem.InvokeVerbEx ("print")

      
           End Select
        Next oAtt
        
        'Cleanup
        If Not oFS Is Nothing Then Set oFS = Nothing
        If Not objFolder Is Nothing Then Set objFolder = Nothing
        If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
        If Not objShell Is Nothing Then Set objShell = Nothing
        
    OError:
        If Err <> 0 Then
          MsgBox Err.Number & " - " & Err.Description
          Err.Clear
        End If
        Exit Sub
    End Sub

    • Moved by Steve Fan Tuesday, November 17, 2015 6:49 AM coding related
    Monday, November 16, 2015 10:59 PM

Answers

  • Hi,

    This forum is for general questions and feedback related to Outlook. Since your question is more related to coding, I'll move it to the Outlook for Developers forum:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Steve Fan
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    • Marked as answer by HeidiMK Wednesday, November 18, 2015 3:31 PM
    Tuesday, November 17, 2015 6:49 AM

All replies

  • Hi,

    This forum is for general questions and feedback related to Outlook. Since your question is more related to coding, I'll move it to the Outlook for Developers forum:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Steve Fan
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    • Marked as answer by HeidiMK Wednesday, November 18, 2015 3:31 PM
    Tuesday, November 17, 2015 6:49 AM
  • Hello Heidi,

    Did you try to run the code under the debugger? 

    What value does the sFileType object get?

    sFileType = LCase$(Right$(oAtt.FileName, 4))

    You may find the Using VBA to Manage Your Outlook Email Attachments page helpful. 

    Tuesday, November 17, 2015 9:36 AM
  • Eugene,

    Here is what it is giving me for the value of that line.

    <Out of context>

    Tuesday, November 17, 2015 3:47 PM
  • The single line of code contains multiple method calls, so I'd suggest breaking the chain of method calls and declaring them on separate lines of code. Thus, you will be able to find the exact property or method call which generates an error message. 
    Tuesday, November 17, 2015 7:38 PM
  • Eugene,

    Thank you, and in English that means? Lol. I'm sorry for my lack of knowledge, I found this code in a previous thread and just copy and pasted it, I have no idea what you are suggesting that I do. Could you show me an example please?

    Tuesday, November 17, 2015 7:55 PM
  • Steve,

    Thank you, I was not sure where to post as I had gotten the code from another thread and asked the question in there and it was not answered so I figured I would just start a new thread. I hope someone will be able to assist me as I get a large number of emails with attachments for my job and this code would save me a lot of time and energy opening all those up.

    Wednesday, November 18, 2015 3:31 PM
  • Hi HeidiMK,

    Please remove the code "On Error GoTo OError" or try to debug the code to see which line of code cause this issue. We can debug the code via set breakpoint via "F9" and debug the code line by line "F8".

    It is hard to provide accurate suggestion to fix this issue without detail information about the error. I suggest that you learn some basic knowledge to help us narrow down this issue. Here are some helpful links for get started for VBA developing:
    Getting Started with VBA in Outlook 2010

    Error Handling and Debugging Tips for Access 2007, VB, and VBA

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 19, 2015 2:10 AM
    Moderator