VBA to Change Outlook Account (1 Viewer)

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 07:12
Joined
Nov 8, 2005
Messages
3,296
Hi Guys

I have had a stab at this and i seem to fall over ..

Is there a way (VBA) to change the default outlook profile from 1 (default) to 2 -

I can send an email and get Access 2010 to know which account to send from
but after I have sent an email the D/b then checks sent items and copies the email into a folder ( refernced accordingly)

However - it only searches the "default" sent folder - 1

so my logic is -keep it simple change the default profle at the start of the process and then change back to true default profile (1) at the end


(I have posted stuff on this before - but I think I have over complicated it )


With mi

.To = Nz(Me.email)

.Subject = Subjectz

.HTMLBody = Me.emailrichtxt & bqe & bqe & "." & Signature

.Categories = ID



If Me.EU = True Then

.SendUsingAccount = ol.Session.Accounts(2)

End If



If Me.EU = False Then

.SendUsingAccount = ol.Session.Accounts(1)

End If
******* the above works


***** the below always checks the true default sent box - not the one I want it to

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 items As Outlook.items

Dim item As Object

Dim attempt As Integer

Set olkapp = Outlook.Application

'###







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
 

Minty

AWF VIP
Local time
Today, 14:12
Joined
Jul 26, 2013
Messages
10,355
Please enclose your code in code tags (its the </> symbol in the editor)

This line
With olkns.GetDefaultFolder(olFolderSentMail)
Will always get the default folder, whereas you want the one you set up earlier.
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 07:12
Joined
Nov 8, 2005
Messages
3,296
Please enclose your code in code tags (its the </> symbol in the editor)

This line
With olkns.GetDefaultFolder(olFolderSentMail)
Will always get the default folder, whereas you want the one you set up earlier.
thats for this - thats the bit that throws me - how do I reference
with olkns. xxxxxxxx(old=olfolderasentmail) to see the right profile?

thats the bit that throws me - (which is why I suggested changing the default profile as an easier option)
the trigger would be if me. eu = false - default (or 1) if me eu = true (2)
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 07:12
Joined
Nov 8, 2005
Messages
3,296
*Thanks for that
(typo)
this is the thing I either need to reset the default or change
olkns .getdefaultfolder(olfoldersentmail)
to olkns .Account.Session.item2 (olfoldersentmail) and put an if statement in there if false then ....
I tried this and it does not like it
 

GaryPanic

Smoke me a Kipper,Skipper
Local time
Today, 07:12
Joined
Nov 8, 2005
Messages
3,296
Dim olAcc As outlook.Account
Set OlAccSess = outlook.Session.Accounts.item(2) -two being the alternative and then I cna play with an if statement
 

Users who are viewing this thread

Top Bottom