We help IT Professionals succeed at work.

Outlook Attachment file name keeps previous value

esps
esps asked
on
458 Views
Last Modified: 2013-11-26
Hi Experts,
I have a situation that is driving me nuts.
My outlook add-in is supposed to get the filename of the attachment of the open email before it is sent out (I trap this in the itemsend event)
Now, if I take any existing email in the inbox (with an attachment) and forward this email to xyz then the code (see code) works fine. However, when I create a new email, the value of attachment.filename (or .displayname) is the same as the previous email forwarded. The value not refreshed or reset.
Please help. See code
Sub ReadActiveAttachments()
        'Declaration
        Dim myItem As Object = Nothing
        Dim myAttachments As Object = Nothing
        Dim myOrt As String = Nothing
        Dim myOlApp As New Outlook.Application
        Dim myOlExp As Outlook.Explorer = Nothing
        Dim myOlSel As Outlook.Selection = Nothing
        Dim explorer As Outlook.Explorer = Me.OutlookApp.ActiveExplorer
        CancelSendMail = False
        myOrt = "C:\temp\"
        Dim currUser As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
        Try
            myOlSel = explorer.Selection
            For Each myItem In myOlSel
                myAttachments = myItem.Attachments
                If myAttachments.Count > 0 Then
 
                   'for all attachments do...
                    For i = 1 To myAttachments.Count
 
                        'save them to destination
                        myAttachments(i).SaveAsFile(myOrt & _
                            myAttachments(i).Filename) 
                        Dim FileID As String = myOrt & _
                        myAttachments(i).Filename()
                        'Dim FileID As String = myAttachments(i).DisplayName

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2011

Commented:
Hello esps,

You have the line:
Dim explorer As Outlook.Explorer = Me.OutlookApp.ActiveExplorer

Wne you forward an email that presumably points to the 'old' email.  WHen you create a new email it is in the explorer window initially therefore the assignment still points to that earlier mail.  In this type of case you could consider a mechanism such as

    If TypeName(Application.ActiveWindow) = "Inspector" Then
            Set mai = Application.ActiveInspector.CurrentItem
    ElseIf TypeName(Application.ActiveWindow) = "Explorer" Then
        Set mai = Application.ActiveExplorer.Selection.Item(1)
    Else
        Exit Sub
    End If

In teh context mai is a mailitem and should still point to the for=warded item and will now additionally point to the new item.

Regards,

chris_bottomley

Author

Commented:
Hi Chris,
I am doing something stupid or not understanding something here??

I have changed the code to:

        Dim myOlApp As New Outlook.Application
        Dim myOlExp As Outlook.Explorer = Nothing
        Dim myOlIns As Outlook.Inspector = Nothing
        Dim myOlSel As Outlook.Selection = Nothing
        Try
            If TypeName(myOlApp.ActiveWindow) = "Inspector" Then
                myOlIns = myOlApp.ActiveInspector.CurrentItem
                myOlSel = myOlIns.Selection
            ElseIf TypeName(myOlApp.ActiveWindow) = "Explorer" Then
                myOlExp = myOlApp.ActiveExplorer.Selection.Item(1)
                myOlSel = myOlExp.Selection
            Else
                Exit Sub
            End If

On the line:    myOlIns = myOlApp.ActiveInspector.CurrentItem
 I get the error below;
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.Inspector'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063005-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
CERTIFIED EXPERT
Top Expert 2011

Commented:
Try the following and then running
            For Each myItem In myOlSel
etc.

Chris
        Dim myOlApp As New Outlook.Application
        Dim myOlExp As Outlook.Explorer = Nothing
        Dim myOlIns As Outlook.Inspector = Nothing
        Dim myOlSel As Outlook.Selection = Nothing
        Try
            If TypeName(myOlApp.ActiveWindow) = "Inspector" Then
'                myOlIns = myOlApp.ActiveInspector.CurrentItem
'                myOlSel = myOlIns.Selection
                myOlSel = myOlApp.ActiveInspector.CurrentItem
            ElseIf TypeName(myOlApp.ActiveWindow) = "Explorer" Then
'                myOlExp = myOlApp.ActiveExplorer.Selection.Item(1)
'                myOlSel = myOlExp.Selection
                myOlSel = myOlApp.ActiveExplorer.Selection.Item(1)
            Else
                Exit Sub
            End If

Open in new window

Author

Commented:
Hi Chris,

It now gives this error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.Selection'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063087-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I'll try a few other options in the interim.
Sincerely hope you have more suggestions
regards
CERTIFIED EXPERT
Top Expert 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Chris,
You have certainly put me on the right track.
Thanks a span.

For completeness for others, I am pasting the code that works.
best regards

 Dim myOlApp As New Outlook.Application
        Dim attCounter As Integer = 0
        Dim loopCounter As Integer = 0
        Dim attFileName As String = ""
     
        Dim CurrentMailItem As Outlook.MailItem = Nothing
        On Error Resume Next
        If TypeName(myOlApp.ActiveWindow) = "Inspector" Then
            CurrentMailItem = myOlApp.ActiveInspector.CurrentItem
        ElseIf TypeName(myOlApp.ActiveWindow) = "Explorer" Then
            CurrentMailItem = myOlApp.ActiveExplorer.Selection.Item(1)
        Else
            Exit Sub
        End If
        attCounter = CurrentMailItem.Attachments.Count
        For loopCounter = 1 To attCounter

            attFileName = CurrentMailItem.Attachments.Item(loopCounter).FileName
            If attCounter > 0 Then

                           CurrentMailItem.Attachments.Item(loopCounter).SaveAsFile( _
                Environ$("TEMP") & "\" & attFileName)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.