Link to home
Start Free TrialLog in
Avatar of Pavel Celba
Pavel CelbaFlag for Czechia

asked on

Outlook automation - SendUsingAccount readonly in VFP...?

I am trying to change the default Outlook account from VFP via automation but it seems to be readonly for new mail messages... Does somebody know the solution?

My code:
oOutlook = CREATEOBJECT("Outlook.Application")
oNamespace = oOutlook.GetNameSpace("MAPI")
oNamespace.Logon()
loItem = oOutlook.CreateItem(0)
loitem.To = 'some@email.com'
loitem.Body = "BODY"

*-- Here is the problem
loitem.SendUsingAccount = oOutlook.Session.Accounts.Item[2]  && I am sure this is POP3 account

*-- Account should be changed now BUT
? loitem.SendUsingAccount   && Still returns null (it is not an object)

loItem.Send()  && sends the message to default account address

*-- After issuing
loitem.Display
*-- and changing Account manually via UI everything works as it should - means the SendUsingAccount property is no more Readonly and I can assign different accounts via automation commands...

So, the question is: How to create mail item having the SendUsingAccount property R/W using automation?
Subquestion is: Does it work same (wrong) way for you in VFP?


Similar code executed from VB.NET works correctly... (see e.g. http://msdn.microsoft.com/en-us/library/bb207787.aspx)

Thanks for all responses
Avatar of rr_miles
rr_miles

Outlook configuration can be scripted via VFP. Checkout

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")

' 3  Deleted Items
' 4  Outbox
' 5  Sent Items
' 6  Inbox
' 9  Calendar
'10  Contacts
'11  Journal
'12  Notes
'13  Tasks
'16  Drafts
'18  All Public Folders

for i = 0 to 20
  Set ofChosenFolder = mynamespace.GetDefaultFolder(i)
  print i, "  ", ofchosenfolder
next
Avatar of Pavel Celba

ASKER

Hmm, thanks. How this could help to solve my problem?
If I understand what you are doing, you want to create a new Outlook Account for sending. Since you have already created the account on your test machine, you want to be able to create the account on another machine? Try scripting Outlook by using the Registry. A good example is http://vbnet.mvps.org/index.html?code/reg/regoutlookaccounts.htm
BTW, my Outlook ref lists;

MailItem.SendUsingAccount Property
Outlook Developer Reference
Returns or sets an Account object that represents the account under which the MailItem is to be sent. Read/write.

Yes, I would like to assign value (account object reference) to SendUsingAccount property.

The problem is this property is read/only when accessed from VFP using automation (see code sample in my question). Based on all MSFT descriptions and articles this property is never r/o.

So, I would like to know how to do it. Does some work around exist?
I think you may need to go behind Outlook and use the registry. Did you look at the link http://vbnet.mvps.org/index.html?code/reg/regoutlookaccounts.htm ?
I think you may need to go behind Outlook and use the registry. Did you look at the link http://vbnet.mvps.org/index.html?code/reg/regoutlookaccounts.htm ?
I have the account info available in FoxPro already. The  oOutlook.Session.Accounts  collection contains all accounts and they are correct. The account I am trying to assign is valid POP3 account.

Anyway, thanks for the link, the VB code may be useful.
BTW; POP3 is for recieving email and SMTP is for sending email.
BTW; POP3 is for recieving email and SMTP is for sending email.
:-)  Do you have separate accounts for sending and receiving in your Outlook?

It could also mean Microsoft is doing it wrong in this example: http://msdn.microsoft.com/en-us/library/bb207787.aspx
Yes. My Outlook setup page reads "Incoming mail server: (POP3)" and "Outgoing mail server (SMTP)". Some IPSs may combine them, but my experience is that many do not. I also find the “Test account settings” button very helpful for troubleshooting. I use Microsoft Office 2003. My laptop is dual boot Vista and XP. Regarding Microsoft, it would not be the first time. Many times I find examples on the Internet better than MSDN examples.
 
Hmm, are you able to set just Outgoing mail server and leave the Incoming one empty in your Outlook?
Interesting idea. I will try it using only one.
Any news how to make SendUsingAccount read/write in VFP?
ASKER CERTIFIED SOLUTION
Avatar of mikegagnon
mikegagnon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Finally, I have to say this is the only working solution.