Drag & Drop Outlook .msg to Form (1 Viewer)

Leyton01

Registered User.
Local time
Tomorrow, 06:00
Joined
Jan 10, 2018
Messages
80
I am trying to allow users to drag and drop an Outlook email on to a form and have it save the .msg to a destination folder and store the information in a table.

I have read the tutorials and achieved this for regular files (drag and drop via explorer) using a ListView control but Outlook emails are handled differently. When I tried to adapt code (from tek tips threads) I get a runtime error 287 "Application-defined or object-defined error" in the line in blue below.

Using both Outlook and Access 2016 - need it to work with Outlook 2010 as well.

Any suggestions?

Code:
Private Sub ListView2_OLEDragDrop(Data As Object, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)

'Code originally from tek tips user jhaganjr.
    Dim olApp As Outlook.Application
    Dim olExp As Outlook.Explorer
    Dim olSel As Outlook.Selection
    Dim i, intCounter, intResponse As Integer
    Dim strFilename, strSQL, strFolderPath, strPathAndFile, strMsg As String
    Dim fs As Object
    Dim fsFolder As Object
    Dim blnFolderExists, blnFileExists As Boolean
    Dim Cancel As Integer
    
    
    'Set folder path
    Set fsFolder = CreateObject("Scripting.FileSystemObject")
    strFolderPath = "C:\temp"
    If fsFolder.FolderExists(strFolderPath) = False Then
        fsFolder.CreateFolder (strFolderPath)
    End If

    'Create the filename as a message file from the ClientID and the NoteID - which will be unique
    strFilename = Me.DocID & "_" & "temp" & ".msg"
    
    'Combine for full path and file name
    strPathAndFile = strFolderPath & "\" & strFilename
    
    'Make sure this file does not already exist to avoid overwriting email files when there is a
    'system glitch.
    Set fs = CreateObject("Scripting.FileSystemObject")
    blnFileExists = fs.FileExists(strPathAndFile)
    If blnFileExists = False Then
        'There's not already a file for this client and noteID. This is the way it always
        'should be. But stuff happens. So, I'm checking.
        'Save the email to the filename just created as a message file
        Set olApp = GetObject(, "Outlook.Application")  'First argument is blank to return the currently
                                                        'active Outlook object, otherwise runtime fails
        Set olExp = olApp.ActiveExplorer
        Set olSel = olExp.Selection
        For i = 1 To olSel.Count
[COLOR=Blue]            olSel.Item(i).SaveAs strPathAndFile, olMSG[/COLOR]
        Next
    Else
        'There's already a file for this client and noteID. This should be impossible,
        'but stuff happens. In this case we notify the user and then re-establish the links
        'so the user can handle it.
        strMsg = "ATTENTION: The system detected an e-mail file already created for this note. "
        strMsg = strMsg & "That e-mail is now linked to this note ID. Please do the following:" & vbCr & vbCr
        strMsg = strMsg & "1. View the e-mail normally." & vbCr
        strMsg = strMsg & "2. If it is the correct e-mail, you don't need to do anything else." & vbCr
        strMsg = strMsg & "3. If it is the wrong e-mail, use the Un-Attach E-mail button to get rid of it. "
        strMsg = strMsg & "Then attach the correct e-mail."
        MsgBox strMsg
    End If
    
    'Update the location field with the location.
    Cancel = True   'To roll back changes caused by the drop.
    Me![DocLocation] = strPathAndFile
    Me.EmailMemo = "EMAIL ATTACHED: Click Here To View"
    Me.EmailMemo.Locked = True
    Me.Dirty = False    'To save the changes.
    
    Set fsFolder = Nothing
    Set fs = Nothing
    Set olSel = Nothing
    Set olExp = Nothing
    Set olApp = Nothing
    
End Sub
 

Leyton01

Registered User.
Local time
Tomorrow, 06:00
Joined
Jan 10, 2018
Messages
80
Ok it looks to be a computer specific, maybe rights based issue.

I have tried the DB on a machine with Outlook 2010 (after changing it to late binding) and it works as intended.

I tried my personal machine with Outlook 2016 and Access 2016 and it also worked fine.

The machine that it errors on is a work machine that is locked down by IT and has Outlook 2016 and Access 2016. I am wondering if this is a security thing but the odd part is that the saving is the only issue, I can see all the objects and even use the .Display action which works, just not the .SaveAs
 

Orthodox Dave

Home Developer
Local time
Today, 20:00
Joined
Apr 13, 2017
Messages
218
Forgive me if this is stating the obvious, but do you have read/write permission to the folder you are trying to save in? Probably best to check with your IT.

.Display would not be affected by this of course.
 

Leyton01

Registered User.
Local time
Tomorrow, 06:00
Joined
Jan 10, 2018
Messages
80
Definitely have access on that folder - in fact the routine actually made the folder the first time when I had a typo in the name (so has folder creation permission, not just file creation). It works with same log in with an Outlook 2010/Access 2016 combo but not Outlook 2016/Access 2016. The folder permission would be linked to the user account I would have thought.
 

Orthodox Dave

Home Developer
Local time
Today, 20:00
Joined
Apr 13, 2017
Messages
218
So using the Outlook 2016/Access 2016 combination, the SaveAs part of your sub works on your personal machine but not on the network.

And it's not a folder permission problem on the network because your sub can even create a new network folder.

Assuming the Access 2016 database is identical on both the network and your personal machine, it would seem the only thing left is that the Outlook settings on the network somehow protect emails from being saved elsewhere. You may be able to at least view the network Outlook 2016 settings (although probably greyed out) and compare them to the settings on your personal Outlook 2016.

I assume also you can manually save network emails to the folder? For example if you change
olSel.Item(i).SaveAs strPathAndFile, olMSG to
olSel.Item(i).Display strPathAndFile, olMSG
does the displayed email allow you to manually save it?

Running out of ideas here.
 

jojergraphics

New member
Local time
Tomorrow, 05:00
Joined
Jan 13, 2019
Messages
1
I recognize this is an old thread.. hoping someone is still monitoring
I am trying do the exact same thing, and encountered the same error. I know the reason for the error is that the sysadmin has disabled the permissions inside outlook which allows access to execute the save as function. I can use the display instead and force the user manually SaveAs- but the problem with that is that I would have to give direction to the user where to save the email because I have created a hierarchy of folders much deeper than the original script used.

So- my question- is there any other workaround that would accomplish the same thing still under as much automation as possible... I'm Ok with the SaveAs dialog opening, but I don't want the user to have to navigate to the right location- I'd rather that be all filled in for them- and all they have to do is click OK..

Maybe, Is there a way to export the message to some temp location, and then let access handle moving it to the right location- so that access is doing all the functions and I can avoid passing any commands into outlook from access?

I'm not VB illiterate, but this is out of my level of expertise for sure.
 

Leyton01

Registered User.
Local time
Tomorrow, 06:00
Joined
Jan 10, 2018
Messages
80
I did not find a way around it but had to get our IT to update the permissions in Outlook.


As you have found it is related to Outlook's 'Trust Center" settings --> Programmatic access. For some reason our organisation left this option blank (which is effectively "off") when they moved from 2010-->2016. It was not too hard to convince them to revert to the previous option, which is still protected by checking for active antivirus.
 

Users who are viewing this thread

Top Bottom