Solved File Emails after sent (Outlook) (1 Viewer)

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
Hello the world - hope all is well (It has been some time since I have been on this forum) -
Straight to it .

I have "pinched/borrowed " some coding to
open outlook and then file the sent email - however its being "awkward" and I am not seeing the issue
http://christiaanheidema.nl/?Tips_and_Tricks___Saving_e-mails_after_sending

is where I ...borrowed the coding
below seems to be the guilty party - I have attached the bd as well (Some assistance would be most welcome and would stop me throwing toys out of my pram )

Private Function FindSentItem(itemID As String, sentFromTime As Date) As Outlook.MailItem
Const MAX_TRY_COUNT = 3
Const SLEEP_TIME = 1000



Dim items As Outlook.items
Dim item As Object
Dim attempt As Integer



attempt = 1

findSentItem_start:
With olkNS.GetDefaultFolder(olFolderSentMail) **************** issue seems to be here (Error 424 ..object required) ****
Set items = .items.Restrict("[SentOn] >= '" & Format(sentFromTime, "ddddd h:nn AMPM") & "'")
For Each item In items
If TypeName(item) = "MailItem" Then
If item.Categories = itemID Then
Set FindSentItem = item
Exit Function
End If
End If
Next item
End With
'
' If not found at this attempt, try again
' after some sleep
'
If attempt < MAX_TRY_COUNT Then
attempt = attempt + 1
' Call Sleep(SLEEP_TIME)
GoTo findSentItem_start
End If
Set FindSentItem = Nothing

End Function
 

Attachments

  • emailtesty.accdb
    608 KB · Views: 16

theDBguy

I’m here to help
Staff member
Local time
Today, 13:14
Joined
Oct 29, 2018
Messages
13,185
Hi. If you're saying you're getting an "object required" error on that line, then it may be because you haven't declared a variable called olkNS and set it to something (at least, I couldn't see it). Just a thought...

PS. I also haven't look at your attachment yet.
 

Isaac

Lifelong Learner
Local time
Today, 13:14
Joined
Mar 14, 2017
Messages
2,701
I didn't download your application, but do you have olkNS actually set anywhere?
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
not in the private Function should I need to ?
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
I will tinker .... IT doing something - but not what I expected...lol
 

Isaac

Lifelong Learner
Local time
Today, 13:14
Joined
Mar 14, 2017
Messages
2,701
not in the private Function should I need to ?
Not if you were passing it INTO the function as a required parameter, already set, then you wouldn't need to, no.

I checked out your db attachment. The failure is inside private function
Code:
mailitemisopen
on:
Code:
For Each olkInsp In olkApp.Inspectors

olkApp is a non-existent thing. Not defined or set within that function.

Me personally, I think in this particular case with all kinds of objects and application instances flying about willy-nilly, and for you, you may be making it harder for yourself than necessary by separating this into so many functions. Just one man's opinion.
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
Ah advise is always welcome - and I am not a programmer - more of know what can be done then go and hack the bits together to get it done .. so advise from "proper" folk is gratefully accepted

This does seem like a frequent or certainly wanted function and I was surprise that their were no samples out there
I have found copy emails (manually selected the emails and push a button and it copies them into a folder )
but this looked interesting as it had elements that seemed interesting -
open email from system - great at this point I can get a file reference (say )12345 and I can state that the folder will be on a folder S:zzz\12345 once email sent then copy email on to folder
 

Isaac

Lifelong Learner
Local time
Today, 13:14
Joined
Mar 14, 2017
Messages
2,701
You want to 1) compose an email, and then 2) save it (as filename.msg) to a local or network folder?
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
yeah
- have I done this the "long way#" (Network)
 

Isaac

Lifelong Learner
Local time
Today, 13:14
Joined
Mar 14, 2017
Messages
2,701
Honestly I'd never tried to use a mail item first as creating a new one, and then as the sent version of that same object.
Here is what I came up with - it works for me.

Note...this does require an Outlook vba reference, and it does depend on the 'last' sent item being the item the code just sent - probably good for 99.99999999% of the time

Code:
Sub SendEmail(strTo As String, strSubject As String, strBody As String, strSaveAsFilename As String)
Dim objOutlook As Outlook.Application, objMI As Object

Dim fldSentItems As Outlook.MAPIFolder, varSentItems As Outlook.Items

Set objOutlook = CreateObject("Outlook.Application")
Set fldSentItems = objOutlook.Session.GetDefaultFolder(olFolderSentMail)

Set objMI = objOutlook.createitem(0)
objMI.to = strTo
objMI.Subject = strSubject
objMI.body = strBody
objMI.display
objMI.send

Set varSentItems = fldSentItems.Items
varSentItems.Sort "[SentOn]", True
Set objMI = Nothing
Set objMI = varSentItems.GetLast

objMI.SaveAs strSaveAsFilename, 3

MsgBox "Completed without errors"

End Sub
Code:
Sub TestIt()
Dim sTo As String, sSubject As String, sBody As String, sSaveAsFilename As String
sTo = "email@domain.com"
sSubject = "test"
sBody = "test body"
sSaveAsFilename = "c:\users\username\desktop\test.msg"

SendEmail sTo, sSubject, sBody, sSaveAsFilename

End Sub

HTH
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
I posted in one of the other forums and your answer seems the cleanest ...(and one I can almost understand)
I will tinker
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 13:14
Joined
Nov 8, 2005
Messages
3,212
Seems to be working thank you everybody who assisted ..
 

Users who are viewing this thread

Top Bottom