Downloads
15442.zip

Les Landau, a reader from Australia, wants to use Outlook VBA to change the reply address for an outgoing message. Of course, in the Microsoft Outlook user interface (UI), which Figure 1 shows, he can change the reply address on a message's Options dialog box by typing the new address into the Have replies sent to: field—but that is a lot of clicking and typing. In this installment of Outlook VBA on Demand, I show you how to change the current message's reply address by adding a macro to an Outlook toolbar button. I also provide some useful techniques for getting input from a user and working with recipients.

Listings 1 and 2 present two versions of the necessary core code. Listing 1 provides a user option through which you specify the reply address you want to use, whereas Listing 2 calls a function—Listing 3's GetReplyAddress()—that prompts the user to enter an address (or the name of a Microsoft Exchange Server mailbox) every time. You might run Listing 1's ChangeReplyAddrNoPrompt procedure for the reply address that you use most frequently, and you might use Listing 2's ChangeReplyAddrWithPrompt procedure to specify a less commonly used reply address. Both Listing 1 and Listing 2 pass the current message and the reply address as arguments to AddReplyRecip, which is the procedure you see in Listing 4, page 158. AddReplyRecip updates the message with the new reply address.

The Outlook message property that contains a custom reply address is the ReplyRecipients property. If you press F2 in the Outlook VBA window to look up ReplyRecipients in the Object Browser, which Figure 2, page 158, shows, you might be discouraged that the item appears as "read-only," and you might incorrectly assume that you can't modify the reply address. However, remember that ReplyRecipients is a collection—a group of similar objects. You can't directly modify the ReplyRecipients collection (which is why Object Browser lists it as read-only), but you can use the collection's methods to modify the collection. Virtually every collection in Outlook lets you use the Add method to add a new item. In Listing 4, for example, you add a new recipient to the ReplyRecipients collection by providing the Add method with the name or address as an argument:

Set objReplyRecip = _
  colReplyRecips.Add(strName)

To ensure that this reply recipient has a valid address, Listing 4 uses the Resolve method to try to resolve the address for the objReplyRecip Recipient object. (This action is the rough equivalent of clicking Check Names in the UI.) If the address doesn't resolve, the procedure uses the Delete method to delete objReplyRecip and gives you the opportunity to try again.

An important component of many programs is getting information from a user. Listing 3 shows one of the simplest techniques to do so, the InputBox() function, which uses the following syntax:

strText = InputBox(, _
, _<br /> <default>)</div></div> <p> InputBox() returns a text string. The only required argument is the prompt. You can omit the title and the default text. Here are a few tips for using InputBox:</p> <ul class="articlelist"> <li>Build the prompt argument as a separate string variable. Doing so will make your program easier to debug. </li> <li>When you want to put text in quotation marks, use the Quote() function. You'll find the Quote() function in <a href="http://windowsitpro.com/content/content/15442/listing_05.txt">Listing 5</a>. </li> <li>The prompt can include as many as 1000 characters. To make long prompts easier to read, use vbCrLf & vbCrLf to insert a blank line, and divide the prompt into paragraphs. (The vbCrLf constant built into VBA represents a carriage return and line feed.) </li> <li>Test the string that the InputBox() function returns to determine whether the string is empty. The user might have clicked OK in the dialog box that InputBox() presents without typing in anything.</li> </ul> <p> You can also use the MsgBox() function to get user input. Other procedures use MsgBox() simply to display a message to a user. When you use MsgBox() as a function in your code, you can specify the buttons it displays. MsgBox() returns an integer that tells you which button a user clicked. The basic syntax is</p> <div class="geshifilter"><div class="text geshifilter-text" style="font-family:monospace;">intRes = MsgBox(<prompt>, _<br /> <buttons>, _<br /> <title>)</div></div> <p> As with InputBox(), the prompt is the only argument that the MsgBox() function requires. For the buttons argument, you can combine built-in Visual Basic (VB) constants. The MsgBox() example that you see in Listing 4 puts the Yes and No buttons on the message box, adds a question mark icon, and sets No as the default.</p> <p> You now have two more useful Outlook VBA macros to add to your collection. You also know how to use the Add method on a Recipients collection to add a new address and the Delete method on an individual Recipient to remove it from its parent collection. Finally, you've seen how to use the InputBox() and MsgBox() functions to get simple responses from a user.</p></div> <fieldset class="fieldgroup group-editors-pick"> </fieldset> <div class="field-syndicate"> Syndicate </div> <fieldset class="fieldgroup group-publication-info"><legend>Publication Info</legend> </fieldset> </div> <!-- we don't need $links or associated processing - CL --> </div> </div> <div class="print-footer"></div> <hr class="print-hr" /> <div class="print-source_url"><strong>Source URL:</strong> <a href="http://windowsitpro.com/exchange-server/changing-reply-address">http://windowsitpro.com/exchange-server/changing-reply-address</a></div> <!-- <div class="print-links"></div> links unnecessary in print--> <script type="text/javascript" src="//api.demandbase.com/api/v2/ip.json?token=b155ec5d50b5dcb41662f36b4d10a6f7702c87e6&callback=dbase_parse"></script> <!-- SiteCatalyst code version: H.13 Copyright 1997-2009 Omniture, Inc. More info available at http://www.omniture.com --> <script type="text/javascript" language="JavaScript" src="/sites/all/modules/custom/penton_omniture/s_code.js?r=1.26?E"></script> <script type="text/javascript" language="JavaScript"><!-- s.pageName="article:Changing the Reply Address"; s.pageType="article"; s.prop1="Changing the Reply Address"; s.prop6="article"; s.list1="Exchange Server|Windows Client|Outlook"; s.prop10="Aug 25, 2000"; s.prop12="p"; s.prop37="Windows IT Pro"; s.prop41="author"; s.eVar9="Sue Mosher"; s.eVar72="d093893c-86eb-47a6-9989-16d19946e7a0"; /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/ var s_code=s.t();if(s_code)document.write(s_code)//--></script> <script language="JavaScript" type="text/javascript"><!-- if(navigator.appVersion.indexOf('MSIE')>=0)document.write(unescape('%3C')+'\!-'+'-') //--></script><noscript><a href="http://www.omniture.com" title="Web Analytics"><img src="http://metrics.pisces-penton.com/b/ss/pentondev/1/H.21--NS/0/3866178/1086899" height="1" width="1" border="0" alt="" /></a></noscript><!--/DO NOT REMOVE/--> <!-- End SiteCatalyst code version: H.13 --> <div id="user_relationships_popup_form" class="user_relationships_ui_popup_form"></div> <img src="http://d.adroll.com/ipixel/IXZC4QKE6BDSVNBBOX54JQ/3BQ2N447KNEODAIZTDTY2B?ar_ed_visitor=corporate" style="position: absolute;width:1px;height:1px;left:-9999px;"/> <iframe id="DivShim" src="/sites/all/modules/custom/pm_doubleclick/blankIframe.html" scrolling="no" frameborder="0" style="height: 0;"></iframe> <div id="roadblockbackground" style="height: 0;"></div> <div id="roadblockcontainer" class="ad640x480 transparent" style="height: 0; overflow: hidden;"> <div id="roadblock"> <p id="closeRB" class="close">Sponsored Introduction <span id="showCount"><a href="javascript:void(0);" onclick="clickAndHide();return false;">Continue on to <span id="roadblockSiteName"></span></a> (or wait <span id="rc"></span> seconds) <a class="xButton" href="#">×</a></span></p> <div id="gpt-interstitial" class="introstitialAd "> <script type="text/javascript"> <!--//--><![CDATA[//><!-- var cookieEnabled = (navigator.cookieEnabled) ? true : false; if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) { document.cookie = "testcookie" cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false; } if (cookieEnabled != null) { if (cookieEnabled) { function setCookie(name, value, expires, path, domain, secure) { var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } var one_day = 1000 * 60 * 60 * roadblock.roadblockdisplay, nextday = 0, index = document.cookie.indexOf('intro'); if (one_day != 0) { nextday = new Date(); nextday.setTime(nextday.getTime() + one_day); } if (index == -1) { setCookie('intro', 1, nextday, '/'); $('#gpt-' + roadblock.roadblockposition).addClass('processed'); googletag.display('gpt-interstitial'); } } } //--><!]]> </script> </div> </div> </div> <div id="gpt-pm-oop"><script type="text/javascript">googletag.display('gpt-pm-oop');</script></div><script type="text/javascript" src="/sites/all/modules/contrib/swftools/onepixelout/onepixelout.js?E"></script> <script type="text/javascript" src="/sites/all/modules/custom/pm_doubleclick/navigation-ads.js?E"></script> <script type="text/javascript"> <!--//--><![CDATA[//><!-- jQuery.extend(Drupal.settings, {"chartbeat":{"uid":53678,"domain":"windowsitpro.com","useCanonical":true,"noCookies":false,"sections":"Exchange Server,Windows Client,Outlook","title":"Changing the Reply Address"}}); //--><!]]> </script> <script type="text/javascript"> <!--//--><![CDATA[//><!-- _bizo_data_partner_id = 594; //--><!]]> </script> <script type="text/javascript"> <!--//--><![CDATA[//><!-- (function() { var s = document.getElementsByTagName("script")[0];var b = document.createElement("script");b.type = "text/javascript";b.async = true;b.src = (window.location.protocol === "https:" ? "https://sjs" : "http://js") + ".bizographics.com/insight.min.js";s.parentNode.insertBefore(b, s);})(); //--><!]]> </script> <script type="text/javascript"> <!--//--><![CDATA[//><!-- adroll_adv_id = "VLH7HVC6ZNEIDEKM72ATRL"; adroll_pix_id = "2ITXTLLMW5EXHKWNE3UNNJ"; (function () {var _onload = function(){if (document.readyState && !/loaded|complete/.test(document.readyState)){setTimeout(_onload, 10);return}if (!window.__adroll_loaded){__adroll_loaded=true;setTimeout(_onload, 50);return}var scr = document.createElement("script");var host = (("https:" == document.location.protocol) ? "https://s.adroll.com" : "http://a.adroll.com");scr.setAttribute('async', 'true');scr.type = "text/javascript";scr.src = host + "/j/roundtrip.js";((document.getElementsByTagName('head') || [null])[0] || document.getElementsByTagName('script')[0].parentNode).appendChild(scr);};if (window.addEventListener) {window.addEventListener('load', _onload, false);}else {window.attachEvent('onload', _onload)}}()); //--><!]]> </script> <script type="text/javascript"> <!--//--><![CDATA[//><!-- var _sf_async_config=Drupal.settings.chartbeat; (function(){ function loadChartbeat() { window._sf_endpt=(new Date()).getTime(); var e = document.createElement('script'); e.setAttribute('language', 'javascript'); e.setAttribute('type', 'text/javascript'); e.setAttribute('src', '//static.chartbeat.com/js/chartbeat.js'); document.body.appendChild(e); } var oldonload = window.onload; window.onload = (typeof window.onload != 'function') ? loadChartbeat : function() { oldonload(); loadChartbeat(); }; })(); //--><!]]> </script> </body> </html>