I am creating a Word MailMerge to Email program using VBA controls. I have the main core set up (With ActiveDocument.Mailmerge….) but before the merge is initiated I want to change the email account to be used when the merge creates the outgoing messages, and then restore the default account once finished. Is there a VB/VBA command or similar that can read the computer’s default email account name, change the account, and then restore the original? Or is there a registry key that can be changed using the Windows Script ‘RegWrite’ method to accomplish this?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Code to change default email account (Word 2000 / VBA)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Code to change default email account (Word 2000 / VBA)
- This topic has 17 replies, 5 voices, and was last updated 18 years, 7 months ago.
AuthorTopicWSd_rasley
AskWoody LoungerOctober 4, 2006 at 12:39 pm #435890Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerOctober 4, 2006 at 1:29 pm #1031668The default profile name is stored in HKEY_CURRENT_USERSoftwareMicrosoftWindowsNTCurrentVersionWindows Messaging SubsystemProfilesDefaultProfile.
In Outlook, you can use the Logon method of the NameSpace object to log on under a specific profile. The following is the example in the Outlook VBA help:
Set myOlApp = CreateObject(“Outlook.Application”)
Set myNameSpace = myOlApp.GetNameSpace(“MAPI”)
myNameSpace.Logon “myProfile”, “myPassword”, True, True -
WSd_rasley
AskWoody LoungerOctober 4, 2006 at 3:15 pm #1031710Thanks. Found a workable solution myself though, using WshShell.RegWrite. I had tried using this previously without success – I could manipulate a registry key but Outlook wouldn’t recognize the change. Today I found the key to the puzzle: close Outlook before changing the registry keys, and when Outlook is re-opened the changed values are recognized.
The mostly-finished code (I’d love a variable subject line, using data pulled from the mailmerge source file):
Code:Dim WshShell Sub macStartMerge() On Error GoTo Errors 'Create the Shell object Set WshShell = CreateObject("WScript.Shell") 'Close Outlook before manipulating the system default email account Shell "C:Program FilesAccessoriesTaskill.exe outlook.exe" 'Change the default email account to the desired sending account WshShell.RegWrite "HKCUSoftwareMicrosoftOfficeOutlookOMI Account Manager" _ & "Default Mail Account", "00000002", "REG_SZ" 'Run the mail merge With ActiveDocument.MailMerge .Destination = wdSendToEmail .MailAsAttachment = False .MailAddressFieldName = "EMAIL" .MailSubject = "Subject" .SuppressBlankLines = False With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=True End With 'Restore the default email account WshShell.RegWrite "HKCUSoftwareMicrosoftOfficeOutlookOMI Account Manager" _ & "Default Mail Account", "00000001", "REG_SZ" 'Open Outlook to send the messages Shell "C:Program FilesMicrosoft OfficeOfficeoutlook.exe" 'Everything complete, finish program GoTo Done Errors: 'Display a message box with the error encountered MsgBox ("The program encountered the following error:" & Chr(13) & Chr(10) & _ Chr(10) & Err.Description) Exit Sub Done: End Sub
Taskill is a stand-alone program that can end a running process. http://www.ozemail.com.au/~nulifetv/freezip/freeware%5B/url%5D
-
WSjscher2000
AskWoody LoungerOctober 5, 2006 at 1:18 am #1031788Is this running in Word? If so, you might be able to change the registry key using the System.PrivateProfileString property. Cribbing from the help file a bit:
Code:varOldValue = System.PrivateProfileString("", _ "HKEY_CURRENT_USERSoftwareMicrosoft" _ & "WindowsCurrentVersionInternet Settings", "EmailName") System.PrivateProfileString("", _ "HKEY_CURRENT_USERSoftwareMicrosoft" _ & "WindowsCurrentVersionInternet Settings", "EmailName") = varNewValue
One less external library to invoke.
-
WSd_rasley
AskWoody LoungerOctober 5, 2006 at 2:47 am #1031793Unfortunately, I have to admit that the code is not working as intended. I was hoping that the MailMerge-generated messages would use the changed default email account as the sending account, but they actually used the default account at time they were being sent. The default account was being changed to the alternate account as intended and the messages generated, but by the time they were being sent the default had been changed back — and that was the account being used when Outlook’s ‘send’ took effect. One, maybe two, messages went out using the changed account, but the rest go out with the ‘restored’ default account.
Word doesn’t support a ‘Wait’ command, but Excel’s VBA library does (and the ‘Help’ file gives an excellent example of how to use this code) so I added Excel’s library to the standard ‘Word’ library list and was able to use it to build in a delay before executing the code to restore the original default account. What would have been a 10-second ‘Wait’ delay in Excel ran much longer in Word and still left open the possibility for Outlook to begin sending with the wrong account.
It looks like I may have to use MailMerge to generate the messages and then find a way to determine when Outlook’s ‘Outbox’ folder empties before restoring the default account back to the original value (ie, If count of outgoing messages = 0 then continue with change to default email account).
UNLESS — Would your code example allow the created outgoing messages to use the changed email account as the outgoing account regardless of the default account set at the time the messages are actually sent?
It might help to clarify why these changes are necessary. The MailMerge will be processed and sent out from our IT department but the replies need to be received through a different account. The recipients of the outgoing messages need to be able to ‘Reply’ and have their message go to the desired email account (100% chance the reply would be sent to the correct receiving account, no margin for error allowable). This could easily be accomplished by using a separate PC to create the outgoing messages but there’s no way to reasonably justify the cost of setting up a single-purpose PC just for what is expected to be a temporary (weeks/months vs years) situation.
-
WSjscher2000
AskWoody LoungerOctober 5, 2006 at 3:33 am #1031794The properties I listed do not change your overall process at all.
Is this a frequent task? Would it be too much to ask the user to manually change the default back after completing the merge/send process?
How about setting a Reply To address? Actually, it doesn’t appear that the MailMerge object provides access to this option. Because it’s a property of the default “form” for email messages, I don’t suggest making a global change.
Does the Application_ItemSend event fire when you send messages using mail merge? If so, you could rig a procedure to recognize your merged messages (perhaps by a repetitive subject line?), and switch the account on the fly at the time of sending. Perhaps similar to the thread Prompt for Account (2003), although maybe that won’t work because there is no visible message compose window…
Well, after all that, I endorse the “two profiles” method described above.
-
WSd_rasley
AskWoody LoungerOctober 5, 2006 at 11:48 am #1031836Is this a frequent task? Would it be too much to ask the user to manually change the default back after completing the merge/send process?
This program would be run every couple of days, in a busy computer room with a history of not following procedures explicitly — past experience tells me that anything more complicated than a button-click leaves the process open to too many human-induced errors.
Perhaps an application-modal messagebox to halt the code’s execution immediately after completing the merge: “Merge complete. Now go to Outlook and make sure the messages get sent before you click this ‘OK’ button.” If they actually wait until the outbox is empty before switching back to Word and clicking the messagebox button, that might be good enough to do the trick.
-
WSjscher2000
AskWoody LoungerOctober 5, 2006 at 6:13 pm #1031911I’m not sure you need user intervention. Let’s say you close Outlook and either change the default account and restart without specifying a profile, or restart with an alternate profile locked to the “special” account. You run the merge and Outlook sends. Your procedure in Word watches for all of the messages in the Outbox to be gone. Then it close Outlook, switches the account back and restarts, or restarts with the default profile.
The “trick,” such as it is, is to take control over the Outlook session you start. If you use the automation code in Hans’ post, then you have an application object reference you can use to examine the contents of the Outbox. Use the API function Sleep to space your Outbox checks to decent intervals (e.g., every 2000 milliseconds) and then close Outlook once sending is complete (e.g., using the .Quit method of your Outlook application object).
It might be a good idea to display a non-modal userform that says “Please Wait, Generating Messages” so the user doesn’t try to slip around and play with Outlook while you are sending…
-
WSd_rasley
AskWoody LoungerOctober 5, 2006 at 6:37 pm #1031918What, precisely, do you mean by loading a profile for Outlook? All of our installations are set up as Internet Mode Only, and I’ve never seen an option for selecting a profile to use.
I would like to have a procedure in Word to watch Outlook’s Outbox and pause Word until the outgoing message count = 0 and thereby eliminate the need for an artificial pause, but for now I don’t know how to access the folder’s information.
-
WSStuartR
AskWoody Lounger -
WSd_rasley
AskWoody Lounger -
WSStuartR
AskWoody Lounger -
WSjscher2000
AskWoody Lounger -
WSd_rasley
AskWoody LoungerOctober 5, 2006 at 7:25 pm #1031938Hmmmm….. The
solution of having an extra button-click in the program versus
to update a lot of computers and train-up? Which way would you go?
Maybe when we upgrade the PC’s to Vista…
Anyhow, I’m going to consider my initial questions solved.
I’ve got code that works (maybe not the best but good enough nonetheless), and I’ve learned a good deal in the process.
-
WSjscher2000
AskWoody LoungerOctober 6, 2006 at 5:47 pm #1032102Here’s an example that puts together the two ideas I described above: using Word’s System.PrivateProfileString method to change the registry, and automating the shut down and restart of Outlook and monitoring of the Outbox using VBA. I can’t test this very well on my system, as I don’t have the merge document and I don’t have a default account. Perhaps, though, it will help you somehow.
The attached is an exported .BAS module, renamed to a .txt extension. To import it into your template or document, rename it to .BAS and use the VBE’s File>Import feature.
-
WSd_rasley
AskWoody LoungerOctober 6, 2006 at 6:50 pm #1032116I tested your code and couldn’t get all of it to work. Beginning with your first group of code to change the account, strUsersAccount = … equated to “” and then errored on the System.PrivateProfileString command. Perhaps it still doesn’t like IMO-setups?
The next oddity was Set olApp = CreateObject(“Outlook.Application”) – other than a pause nothing visible happened to indicate Outlook started; no splash screen, no taskbar button. I went to the Task Manager and there it was. Not a problem if Outlook is left closed at the end of the program.
The code to check the outgoing item count worked perfectly
and I’ll use that instead of my messagebox solution.
-
WSjscher2000
AskWoody Lounger -
Andrew Lockton
AskWoody_MVPOctober 7, 2006 at 9:41 pm #1032300Have you investigated any other methods of attacking this problem? This post 590,463 shows a method for sending emails with attachments where you can set the return address as a string.
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Sometimes I wonder about these bots
by
Susan Bradley
3 hours, 56 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
5 hours, 54 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
6 hours, 54 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
6 hours, 20 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
2 hours, 49 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
9 hours, 36 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
9 hours, 38 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
3 hours, 31 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
17 hours, 45 minutes ago -
0Patch, where to begin
by
cassel23
11 hours, 47 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 7 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
19 hours, 8 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 15 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 6 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
18 hours, 2 minutes ago -
Installer program can’t read my registry
by
Peobody
43 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 4 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 12 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 3 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 12 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 13 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
59 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 16 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
18 minutes ago -
Outdated Laptop
by
jdamkeene
2 days, 22 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 3 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 3 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
5 hours, 31 minutes ago -
May 2025 updates are out
by
Susan Bradley
7 hours, 13 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 9 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.