Minor meltdown .. (1 Viewer)

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
Access 2010 ..
ok guys when I type the following
4it spanners out on top line on
Application.sessions.accounts
(its the sessions - its not seeing - )
what/where have I goofed ?

(the intention is to set the getdefaultfolder to the account I need as I have two accounts and this looks like it will "default " to what ever account I want - )

I've added the code tags for you! Don't you think it looks better!

Code:
For Each oAccount In Application.Session.Accounts
  If oaccount ="1@email.com" then
    Set store = oaccount.DeliveryStore
    Set folder = store.GetDefaultFolder(olFolderInbox) 'here it selects the inbox folder of account.
    For each item in folder.items
      ' Code goes here
    next
  end if
next
pinched from



picked
 
Last edited by a moderator:

bastanu

AWF VIP
Local time
Today, 12:24
Joined
Apr 13, 2010
Messages
1,402
You have to replace Application with a variable declared as Outlook.Application as Application.Sessions is trying to work with your Access Application (which doesn't have a session):

Code:
Dim outApplication as Outlook.Application

For Each oAccount In outApplication.Session.Accounts
Cheers,
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
digesting .. (thanks) - blinking interents gone wonky...
 

Isaac

Lifelong Learner
Local time
Today, 12:24
Joined
Mar 14, 2017
Messages
8,777
Can you post your full code?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:24
Joined
Jul 9, 2003
Messages
16,280
I have a "minor meltdown" when I see people post code, and forget to enclose it in code tags!
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
what i am trying to do is get the account that my sent email is in ( great when its the "default" ) but when its another account the following doesn't work so I looked at various options to call the alterntive account to be the default "sent folder"

what i had in mind was if X = true (x being a field on my form) then use the alternative

(sending from alternative ok the email sits in the alternative sent box - but the following code only checks the "default " one and I want it to call to the the alternative account - heres the code that does the "find" element

so if I put the code in here somewhere to switch account to change default then it should be ok (well that's my thinking )

I've added the code tags for you! Don't you think it looks better!

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

    Dim olkns As Outlook.NameSpace
    Dim olAcc As Outlook.aCCOUNT
    Dim items As Outlook.items
    Dim item As Object
    Dim attempt As Integer
    Set olkapp = Outlook.Application
    '###
 
    'Set olAcc = Outlook.Accounts.Session(2)

Set olkns = olkapp.GetNamespace("MAPI")

   
    attempt = 1
   
findSentItem_start:
    With olkns.GetDefaultFolder(olFolderSentMail)
        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
      '  Pause (0.1)is 0.1 second
      Pause (3)
       'Call Sleep(SLEEP_TIME)
        GoTo findSentItem_start
    End If
    Set FindSentItem = Nothing
   
End Function
 
Last edited by a moderator:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:24
Joined
Jul 9, 2003
Messages
16,280
Do you actually read people's comments?
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
Sorry - Todays internet is driving me crazy - its really wobbly (I also mis -read it and thought it said postcode --lol)
 

bastanu

AWF VIP
Local time
Today, 12:24
Joined
Apr 13, 2010
Messages
1,402
Maybe try something like this (written in Notepad, not tested):
Code:
Private Function FindSentItem(itemID As String, sentFromTime As Date) As Outlook.MailItem
Const MAX_TRY_COUNT = 3
Const SLEEP_TIME = 1000

Dim olkapp As Outlook.Application

Dim olkns As Outlook.NameSpace
Dim olAcc As Outlook.aCCOUNT
Dim items As Outlook.items
Dim item As Object
Dim attempt As Integer
Dim oStore as object,oFolder as Object

Set olkapp = Outlook.Application
'###

'Set olAcc = Outlook.Accounts.Session(2)

Set olkns = olkapp.GetNamespace("MAPI")
For Each oAccount In olkapp.Session.Accounts
     If oaccount ="1@email.com" then
     Set oStore = oaccount.DeliveryStore
     Set oFolder = store.GetDefaultFolder(olFolderSent) 'here it selects the sentfolder of account.
Next

attempt = 1

findSentItem_start:
'With olkns.GetDefaultFolder(olFolderSentMail)
With oFolder
   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
' Pause (0.1)is 0.1 second
Pause (3)
'Call Sleep(SLEEP_TIME)
GoTo findSentItem_start
End If
Set FindSentItem = Nothing

End Function
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:24
Joined
Jul 9, 2003
Messages
16,280
Sorry - Todays internet is driving me crazy - its really wobbly (I also mis -read it and thought it said postcode --lol)

Strangely enough, when I spoke those words (I use Google transcribe) it actually transcribed postcode instead of post code!
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
lol OK - I got this working in a fashion.. but not happy with it -
the field on form - I have coded so that it populates the email account on the form - so if it was Dave on the account form then it will check "dave"s email- however I really want it to look at the current user which maybe "susie"

so I need it to look at current user - I tried getnamespace that just was not playing ball
.....


Dim AccNa As String
Dim AccNaFile As String


AccNa = Me.EmailAccounttoUSe (Field on form)

AccNaFile = AccNa & "\" & "Sent Items"
Set sFolder = GetFolderPath(AccNaFile)
attempt = 1

findSentItem_start:
With sFolder
 

Isaac

Lifelong Learner
Local time
Today, 12:24
Joined
Mar 14, 2017
Messages
8,777
You mean you need to send an email to the person who is currently using the Access database?
 

bastanu

AWF VIP
Local time
Today, 12:24
Joined
Apr 13, 2010
Messages
1,402
Current user of what? Access or Outlook? What you show say in post # 13 does match your original request to check the Sent folder of a non-default Outlook account. Do you mean that you have loaded in your Outlook your default account, and also Dave's and Sussie's (with the proper permissions)?

Cheers,
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
#Sorry not made myself clear
OK what I have done is on my form I have "if User Dave and EU = true then use Dave@eutest.com and if Dave and EU = false use Dave@noraml.com" for each user but the Dave is the account exec on the form

Susie might send an email from this form - she is not dave therefore my temporary solutions doesn't work when Susie is sending an email on a Dave record (dave is the account executive - Susie might be the helper for Dave

so what I am after is that the form - checks whos email account has just sent an email - then I can get it to find and file the email

Record owner - Dave
who might send an email
Dave
Susie
anybody else ...#
 

Isaac

Lifelong Learner
Local time
Today, 12:24
Joined
Mar 14, 2017
Messages
8,777
Instead of figuring out who the current user is based on whose Outlook account is in session/has just sent the email, maybe try figuring out the current user based on things like

environ("username")
CreateObject("WScript.Network").UserName

..etc
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
Ah .. but I want to search their sent emails

the above code is just the bit that had the issue the producure - create email send from account ( each user has two accounts EU and non EU)
but also a third option which was if a Account Assistant sent an email on behalf of xxx the underlying form knows what record is being looked at (EU or non EU) so the email sending will use account 1 or 2 depending upon this

the above code used to only search Default email sent - which if their was only one account works a treat - however need to tweak it to search when not default sent folder (account no 2) *temporary solved but messy it woudl just be easier to know what email account (emailaddress) had been used to send the email and do a search on this account
 

bastanu

AWF VIP
Local time
Today, 12:24
Joined
Apr 13, 2010
Messages
1,402
Still not very clear as you should know what email account has been used to send the email as you are the one that chooses it in code. The code in post # 10 could be tweaked to choose the EU or non-EU account:

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

Dim olkapp As Outlook.Application

Dim olkns As Outlook.NameSpace
Dim olAcc As Outlook.aCCOUNT
Dim items As Outlook.items
Dim item As Object
Dim attempt As Integer
Dim oStore as object,oFolder as Object

Set olkapp = Outlook.Application

Set olkns = olkapp.GetNamespace("MAPI")
For Each oAccount In olkapp.Session.Accounts     
    If Me.EU=True then
        If Instr(oaccount,"eutest.com")>0 then 'choose EU account
            Set oStore = oaccount.DeliveryStore
                 Set oFolder = store.GetDefaultFolder(olFolderSent) 'here it selects the sentfolder of account.
        End If
    Else
        If Instr(oaccount,"noraml.com")>0 then 'choose non-EU account
            Set oStore = oaccount.DeliveryStore
                 Set oFolder = store.GetDefaultFolder(olFolderSent) 'here it selects the sentfolder of account.
        End If
    End If
    
Next

attempt = 1

findSentItem_start:

With oFolder
   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
' Pause (0.1)is 0.1 second
Pause (3)
'Call Sleep(SLEEP_TIME)
GoTo findSentItem_start
End If
Set FindSentItem = Nothing

End Function

Also, if all you need is to file the message somewhere wouldn't it be easier to simply include a Bcc to a general admin email when you create the original message and you will have them all in there with all the info you need (the From will show who sent the message)...

Cheers,
Vlad
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 12:24
Joined
Nov 8, 2005
Messages
3,294
I will try this -
as to the filing each record has a folder say 12345 and the way this is set up it will file all emails into this folder on the server rather than filling up outlook (with a Bcc option - I did consider this but outlook would fall over very quickly if 10 users all bcc into a dummy email address-)

I will test what you have done ..
I do appricate your time

Kind regards
 

Users who are viewing this thread

Top Bottom