I am using Outlook 2013 and an up-to-date version of WinBatch. I am trying to employ the SendUsingAccount property of the Outlook MailItem object. While this works fine in VBA (so I don't think it is an Outlook issue), I cannot seem to get it to work using the same command structure in WinBatch. Here is the code:
OutApp = CreateObject("Outlook.Application")
oAccount = Outapp.Session.Accounts.Item(2) ; this is the non-default "second" account
;message("", oAccount.DisplayName) ; this correctly displays the second account
oMail = Outapp.CreateItem(0)
oMail.Subject = "Test123"
oMail.Recipients.Add("Michael Moran")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
;oMail.SendUsingAccount = Outapp.Session.Accounts.Item(2)
oMail.Save
oMail.Display
Exit
__________________________________
When I execute this using WinBatch, the message is "From:" the default (first) account. When I execute this code's VBA counterpart, the email is "From:" the second, non-default account.
I get no OLE complaints about the command in WinBatch; it just doesn't work.
Any guidance would be most appreciated.
Mike Moran
Maybe try changing this line:
oAccount = Outapp.Session.Accounts.Item(2)
to
oAccount = Outapp.Session.Accounts.Item(2).Value
Still no dice Deana. Outapp.Session.Accounts.Item(2).Value generates a COM error (unknown name) likely because "Value" is not a member of the Accounts object. The funny thing is that all indications show that the correct, secondary account is selected but SendUsingAccount still sends from the default account. It must be a syntax error, but I've exhausted every possbility I can come up with.
Mike
I reviewed the code pretty closely, and it appears that you have it coded correctly. Maybe post the .vbs script and post the DebugTrace output of the .wbt script.
UAC might be a factor....
By default our WinBatch Studio environment and all .wbt files use the Manifest settings: Requested Execution Level = HighestAvailable and UI Access = True. WinBatch uses these default Manifest settings in attempt to obtain the highest level of access for all scripts. However these manifest settings are not always relevant for the script you are running. For example if your script does not require UI access then you should not manifest the script using UI Access = True. An example of a script that would require UI Access = True is using any of the Control manager Extender functions that attempt to access controls in other running processes. In short you will need to keep in mind what minimum Manifest settings your script will require.
If you would like to test your un-compiled scripts using the properly manifested WinBatch studio, then you should run this version of the WinBatch studio:
C:\Program Files (x86)\WinBatch\System\WBStudio_IF.exe (Requested Execution Level = AsInvoker and UI Access = False).
Note: you can also give your .WBT a special file extension of .WBT_IF, which will run the appropriately manifested version of the WinBatch interpreter when it is clicked on.
Reference:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Manifest+UAC~Manifest~Setting~Triggers~1129~Error~.txt
Let me know if changing the manifest resolves the issue.
Hi again Deana.
First, thank you for your continued help. I have UAC turned off, but ran the WinBatch with the _IF extension addon (I did not know about that) and still the same results.
Here is the VBA code:
Option Explicit
Sub SendFromReporter()
Dim objOutlookApp As Outlook.Application
Dim objOutlookEmail As Outlook.MailItem
Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookEmail = objOutlookApp.CreateItem(olMailItem)
objOutlookEmail.Subject = "Testing Again"
objOutlookEmail.Recipients.Add "Michael Moran"
objOutlookEmail.Recipients.ResolveAll
objOutlookEmail.SendUsingAccount = objOutlookApp.Session.Accounts.Item(2)
objOutlookEmail.Save
objOutlookEmail.Display
End Sub
________________________________
I beginning to thin that SendUsing Account is temperamental. Some VBA posters have said that a "Set" is required before the objOutlookEmail.SendUsingAccount = objOutlookApp.Session.Accounts.Item(2) command while other disagree. The debugtrace is unremarkable. All OLE objects passed muster.
Mike
Running out of ideas...Maybe try:
objOutlookEmail.SendUsingAccount.Value = objOutlookApp.Session.Accounts.Item(2)
I suspect you might be dealing with a security issue. To confirm move the display to the beginning of the script:
OutApp = CreateObject("Outlook.Application")
oAccount = Outapp.Session.Accounts.Item(2) ; this is the non-default "second" account
;message("", oAccount.DisplayName) ; this correctly displays the second account
oMail = Outapp.CreateItem(0)
oMail.Display
oMail.Subject = "Test123"
oMail.Recipients.Add("Michael Moran")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
;oMail.SendUsingAccount = Outapp.Session.Accounts.Item(2)
oMail.Save
Exit
See if you get a Security warning. If so you might need to use a tool like redemption. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WIL~Extenders/MAPI+MAPI~and~Outlook~Security.txt
Hi again Deana. While security did not seem to be the problem (SendUsingAccount still sent from the default account using Redemption's SafeItem), I was able to use Redemption's RDO objects successfully! Here is the code:
Session = CreateObject("Redemption.RDOSession")
Session.Logon
ForEach olAccountTemp In Session.Accounts
message("", olAccountTemp.Name)
If (olAccountTemp.Name == "[second account]@nycourts.gov")
Account = olAccountTemp
Break
EndIf
Next
;message("", oAccount.displayname)
;Drafts = Session.GetDefaultFolder(4); Outbox
;Drafts = Session.GetDefaultFolder(16); Drafts
Drafts = Session.GetSharedDefaultFolder("[second account]@nycourts.gov",16); Drafts
;message("",Drafts.Name)
Msg = Drafts.Items.Add
Msg.Account = Account
;Msg.To = Join(ToRecipientsArray, "; ")
Msg.To = "xyz@nycourts.gov"
Msg.Subject = "Here We Go!"
Msg.Body = "Again"
Msg.Save
;Msg.Display
;Msg.Send
_______________________________
All's well that ends well. And may thanks for all of you help and pointing me to Redemption.
Mike
Current versions of Outlook really limit the types of operations that can be automated by scripts for security reasons. Glad to hear Redemption's SafeItem resolved the issue for you.
There is more than one way to skin a cat (actually, I like cats). Here's another way without using Redemption:
OutApp = ObjectCreate("Outlook.Application")
objSession = OutApp.Session
;Session.Logon
ForEach olAccountTemp In objSession.Accounts
If (olAccountTemp.Smtpaddress == "[secondary account]@nycourts.gov")
Account = olAccountTemp
Break
EndIf
Next
myRecipient = objSession.CreateRecipient("secondary account]@nycourts.gov")
;message("", Account.smtpaddress)
;Drafts = Session.GetDefaultFolder(4); Outbox
;Drafts = objSession.GetDefaultFolder(16); Drafts
;message("", Drafts)
;Exit
Drafts = objSession.GetSharedDefaultFolder(myRecipient,16); Drafts
;message("",myRecipient)
Msg = Drafts.Items.Add(0)
;Msg.Account = Account
;Msg.To = Join(ToRecipientsArray, "; ")
Msg.To = "xyz@nycourts.gov"
Msg.Subject = "Here We Go!"
Msg.Body = "Again"
Msg.Save
Msg.Display
;Msg.Send
________________________________________
Mike