<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>VBA on Much Ado About IT</title><link>https://it.knightnet.org.uk/tags/vba/</link><description>
Recent content about VBA from Much Ado About IT |
Ramblings and rantings from IT Architect &amp; Designer, Julian Knight</description><generator>Hugo | gohugo.io | Theme twenty-sixteen</generator><language>en-gb</language><copyright>This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</copyright><lastBuildDate>Mon, 24 Apr 2023 21:27:28 +0000</lastBuildDate><atom:link href="https://it.knightnet.org.uk/tags/vba/feed.xml" rel="self" type="application/rss+xml"/><item><title>Macro to open a web URL from Outlook</title><link>https://it.knightnet.org.uk/kb/ms-office/outlook-macro-open-web-url/</link><guid isPermaLink="true">https://it.knightnet.org.uk/kb/ms-office/outlook-macro-open-web-url/</guid><pubDate>Wed, 25 Apr 2018 10:31:45 +0000</pubDate><guid>https://it.knightnet.org.uk/kb/ms-office/outlook-macro-open-web-url/</guid><description><div>Many of us spend most of our work time in a few applications such as Outlook (email) and a web browser. This example macro for Microsoft Outlook will let you add a button to the Outlook Ribbon that will open a specific website URL.</div><div>&lt;p>To use this macro:&lt;/p>
&lt;ul>
&lt;li>Open the VBA Editor window (&lt;!-- raw HTML omitted -->ctrl&lt;!-- raw HTML omitted -->-&lt;!-- raw HTML omitted -->F11&lt;!-- raw HTML omitted -->)&lt;/li>
&lt;li>Double-click on Project1 &amp;gt; Microsoft Outlook Objects &amp;gt; ThisOutlookSession in the Project pane (top-left)&lt;/li>
&lt;li>Paste the code below&lt;/li>
&lt;li>Customise the Ribbon&lt;/li>
&lt;li>Add a new Group (you can&amp;rsquo;t add your own entries to the standard groups)&lt;/li>
&lt;li>Add the &lt;code>openGoogle&lt;/code> macro (or whatever you&amp;rsquo;ve added), rename and change the icon if desired&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Note&lt;/strong>: This version will only work for the 64-bit version of Microsoft Outlook.
To use on the 32-bit version, remove the word &lt;code>PtrSafe&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-basic" data-lang="basic">&lt;span class="line">&lt;span class="cl">&lt;span class="vg">Option&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Explicit&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="vg">Private&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Declare&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">PtrSafe&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Function&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">ShellExecute&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">Lib&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s2">&amp;#34;shell32.dll&amp;#34;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Alias&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s2">&amp;#34;ShellExecuteA&amp;#34;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">hWnd&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Long&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Operation&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">String&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Filename&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">String&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">Optional&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Parameters&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">String&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">Optional&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Directory&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">String&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">Optional&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">ByVal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">WindowStyle&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Long&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">vbMinimizedFocus&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">_&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Long&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&amp;#39; WARINING: THIS MUST ONLY BE GIVEN SAFE URLs!&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&amp;#39; DO NOT USE WITH DIRECT USER INPUT&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="vg">Private&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Sub&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">openUrl&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="vg">url&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">String&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">Dim&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">lSuccess&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">As&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Long&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">lSuccess&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">ShellExecute&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="il">0&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s2">&amp;#34;Open&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">url&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="vg">End&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Sub&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="vg">Public&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Sub&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">openGoogle&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="vg">openUrl&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s2">&amp;#34;https://google.com&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="vg">End&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="vg">Sub&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/categories/microsoft">Microsoft</category><category domain="https://it.knightnet.org.uk/categories/software">Software</category><category domain="https://it.knightnet.org.uk/tags/microsoft">Microsoft</category><category domain="https://it.knightnet.org.uk/tags/office">Office</category><category domain="https://it.knightnet.org.uk/tags/outlook">Outlook</category><category domain="https://it.knightnet.org.uk/tags/vba">VBA</category></item><item><title>A simple mail filer for Microsoft Outlook (VBA)</title><link>https://it.knightnet.org.uk/2015/06/simple-mail-filer-microsoft-outlook-vba-2.html</link><guid isPermaLink="true">https://it.knightnet.org.uk/2015/06/simple-mail-filer-microsoft-outlook-vba-2.html</guid><pubDate>Fri, 05 Jun 2015 19:56:06 +0000</pubDate><guid>https://it.knightnet.org.uk/2015/06/simple-mail-filer-microsoft-outlook-vba-2.html</guid><description><div/><div>&lt;p>Like many people I receive an unmanageable amount of email each day. Many days I get through only around 1/3 of the email I receive.
However, the role I am in professionally requires me to retain a large proportion of correspondence. Some because it relates to ongoing projects, other because of security, audit or compliance reasons.&lt;/p>
&lt;p>In addition, I work across many projects. It isn’t unusual for me to be involved in two dozen projects at any one time on top of my day-to-day management work.&lt;/p>
&lt;p>So I have many folders – hundreds in fact – and filing email into the right folder has become a real drag. It can take an appreciable amount of time to hunt down the correct folder and Outlook does not provide any way to search/filter folder names in the UI.&lt;/p>
&lt;p>Thankfully, I have access to VBA in Outlook. While the experience of using VBA macro’s to control Outlook is rarely pleasant, it does get the job done – mainly.&lt;/p>
&lt;p>My requirements for the utility were as follows:&lt;/p>
&lt;ul>
&lt;li>Must let me select multiple emails, if any have already been filed, show me the folder(s) so I can quickly file new email to the same folder as the rest of the conversation.&lt;/li>
&lt;li>Must give me a list of all my folders with a simple way of filtering the list by typing a few letters.&lt;/li>
&lt;li>Must also let me open a folder for viewing instead of filing or cancelling.&lt;/li>
&lt;/ul>
&lt;p>A couple of hours later, I was able to create a new utility. This has been published to Github and you can find it at:
&lt;!-- raw HTML omitted -->&lt;a href="https://github.com/TotallyInformation/outlook-filer">https://github.com/TotallyInformation/outlook-filer&lt;/a>&lt;!-- raw HTML omitted -->&lt;/p></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/microsoft">Microsoft</category><category domain="https://it.knightnet.org.uk/categories/software">Software</category><category domain="https://it.knightnet.org.uk/categories/windows">Windows</category><category domain="https://it.knightnet.org.uk/tags/email">email</category><category domain="https://it.knightnet.org.uk/tags/microsoft">Microsoft</category><category domain="https://it.knightnet.org.uk/tags/outlook">Outlook</category><category domain="https://it.knightnet.org.uk/tags/vba">VBA</category></item><item><title>Nearest Postcode Search in Microsoft Excel</title><link>https://it.knightnet.org.uk/2009/09/nearest-postcode-search-microsoft.html</link><guid isPermaLink="true">https://it.knightnet.org.uk/2009/09/nearest-postcode-search-microsoft.html</guid><pubDate>Tue, 29 Sep 2009 09:52:30 +0000</pubDate><guid>https://it.knightnet.org.uk/2009/09/nearest-postcode-search-microsoft.html</guid><description><div/><div>&lt;p>I need to find a load of addresses by proximity to a postcode. I have the addresses in a Microsoft Access database.&lt;/p>
&lt;ol>
&lt;li>Download &lt;!-- raw HTML omitted -->Paul Jenkinsâ€™s UK Postcode csv&lt;!-- raw HTML omitted --> and import into Access as a table&lt;/li>
&lt;li>Create a query on your own table adding the following calculated field:&lt;/li>
&lt;/ol>
&lt;pre>&lt;code>&amp;lt;pre&amp;gt;PCregion: Trim( Left( [My Table]![Postcode], InStr( [My Table]![Postcode], &amp;amp;#8221; &amp;amp;#8221; ) ) )&amp;lt;/pre&amp;gt;
&lt;/code>&lt;/pre>
&lt;ol start="3">
&lt;li>Create a second query that joins the above query to the imported postcode table. Join on the “PCregion” field created above. Also add in the latitude and longitude fields from the postcodes table&lt;/li>
&lt;li>…
Well, I couldn’t be bothered to fight with Access so I exported the key tables and used Excel instead!
The principals are the same. Load the tables as tabs, load the Postcode data as a tab. Create a front sheet containing the reference Postcode (Create a Named Cell for this “STARTREGION”). Translate the reference Postcode to Latitude and Longitude by doing a VLOOKUP to the Postcode table, e.g.:&lt;/li>
&lt;/ol>
&lt;!-- raw HTML omitted -->
&lt;p>Where RC2 contains the reference Postcode and “UK_PostCodes!C1:C4” is the postcodes table (column 3 contains the Latitude and 4 the Longitude). Note that I’ve used R1C1 reference style as it is easier to mix fixed and relative references.
Now use the same formula in the main data tables so that you have Lat. and Long. columns added based on the Postcode on each line.
Finally add a “Distance” column to each data table with the following formula:&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;p>In this case, we’ve used the STARTREGION named cell and the Lat./Long. data is in columns 28 &amp;amp; 29 respectively.
The calculation and Postcode reference data came from HM2K’s website: [How to search by nearest uk postcode in php][1].
[1]: &lt;a href="http://www.hm2k.com/posts/how-to-search-by-nearest-uk-postcode-in-php">http://www.hm2k.com/posts/how-to-search-by-nearest-uk-postcode-in-php&lt;/a>&lt;/p></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/categories/microsoft">Microsoft</category><category domain="https://it.knightnet.org.uk/categories/windows">Windows</category><category domain="https://it.knightnet.org.uk/tags/maps">Maps</category><category domain="https://it.knightnet.org.uk/tags/office">Office</category><category domain="https://it.knightnet.org.uk/tags/vba">VBA</category></item></channel></rss>