Hello all,
I adopted some code for having a drag and drop from outlook capability in my database.
I have it working, basically you drop the email into a memo field and the action is caught which triggers the code to save a copy of the email as a msg to a destination folder.
I'd like to make a button "Delete Email" that deletes the record and also deletes that msg file that was created upon dropping the email in the database.
The file location is stored in the table after dropping the email into the database, so I'm sure this is able to be done, I'm just not sure how to go about it.
And as I said, I got all of this code from another forum, I'm very inexperienced with coding
Here's my code for the actual drag and drop, It may be helpful:
Code:
'I got the guts of this sub from Remou on tek-tips.com. S/he told me I can drag and drop an 'email to a memo field, then gave me the object control code to save the file.
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
'My network consultant advises not putting too many files in a folder - like our Permanent Images.
'Therefore, I will separate emails into a new folder each year. This code allows me
'to never check on it, by creating the folder automatically when the year changes.
Set fsFolder = CreateObject("Scripting.FileSystemObject")
strFolderPath = "R:\Emails " & Year(Date)
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 = [Forms]![EmailAttachmentForm]![ParticipantName] & "_" & Me![NoteID] & ".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
olSel.Item(1).SaveAs strPathAndFile, olMSG
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![EmailLocation] = strPathAndFile
Me.EmailMemo = "EMAIL ATTACHED"
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
DoCmd.Close