{"id":59643,"date":"2023-09-30T10:17:57","date_gmt":"2023-09-30T09:17:57","guid":{"rendered":"https:\/\/ekiwi-blog.de\/59643\/vba-outlook-set-cursor-to-specific-position-in-e-mail\/"},"modified":"2025-05-01T09:51:02","modified_gmt":"2025-05-01T08:51:02","slug":"vba-outlook-set-cursor-to-specific-position-in-e-mail","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/59643\/vba-outlook-set-cursor-to-specific-position-in-e-mail\/","title":{"rendered":"VBA Outlook: Set cursor to specific position in e-mail"},"content":{"rendered":"<p><abbr title=\"Visual Basic for Application\">VBA<\/abbr> is wonderfully suited to automating certain work processes in the Microsoft Office world and thus keeping daily tedious work and unnecessary clicks at bay. For example, you can programme a macro that automatically replies to an e-mail with a certain text. To save further clicks, you can position the cursor at a certain point in the e-mail so that you can continue writing at this position, e.g. because you want to insert an individual salutation.<\/p>\n<p>For example, the automatic reply text could contain a salutation with a placeholder:<\/p>\n<p><em><strong>&#8220;Hello &#8230;, [&#8230;]&#8221;<\/strong><\/em>.<\/p>\n<p>where <em>&#8220;&#8230;&#8221;<\/em> should then be used to enter an individual name, for example.<\/p>\n<h2>Use SendKeys function<\/h2>\n<p>To achieve this, one can use the <strong>SendKeys<\/strong> function in the VBA code.<\/p>\n<pre><code><span style=\"color: #0000ff;\">Sub<\/span> ReplyMSG()\r\n    <span style=\"color: #0000ff;\">Dim<\/span> olItem As Outlook.MailItem\r\n    <span style=\"color: #0000ff;\">Dim<\/span> olReply As MailItem <span style=\"color: #008000;\">' Reply<\/span>\r\n\r\n    <span style=\"color: #0000ff;\">Set<\/span> olReply = olItem.Reply\r\n    olReply.Display\r\n    <strong>SendKeys \"{Right}{Right}{Right}{Right}{Right}\", <span style=\"color: #0000ff;\">True<\/span><\/strong>.\r\n<span style=\"color: #0000ff;\">End Sub\r\n<\/span><\/code><\/pre>\n<p>In the code example shown above, a reply email is generated and displayed using the <em>&#8220;Display&#8221;<\/em> function. This is important because the <em>SendKeys<\/em> function works as if you were making a mouse or keyboard input, except that the individual keyboard or mouse inputs are passed by code.<\/p>\n<p>The <em>display<\/em> call displays the reply mail and automatically places the cursor at the beginning of the text. Therefore, in our example, we pass the parameter <em>{Right}<\/em> 6 times to move the cursor 6 positions to the right. <code>{RIGHT}<\/code> stands for the right arrow key on the keyboard. This positions the cursor exactly in front of the <em>&#8220;&#8230;&#8221;<\/em> so that you can type in the name or salutation straight away.<\/p>\n<p>With <a href=\"https:\/\/learn.microsoft.com\/en-en\/office\/vba\/language\/reference\/user-interface-help\/sendkeys-statement\" target=\"_blank\" rel=\"noopener\"><em>SendKeys<\/em><\/a> all kinds of keyboard commands can be realised. For example, with <code>{F1}<\/code> you can trigger the F1 key or with <code>{ENTER}<\/code> you can simulate the Enter\/Return key and thus cause a <a title=\"How to insert a line break in Visual Studio Resource Editor and which keyboard shortcut to use.\" href=\"https:\/\/ekiwi.de\/en\/index.php\/6598\/visual-studio-line-break-in-resource-file\/\" target=\"_blank\" rel=\"noopener noreferrer\">line break<\/a> in the e-mail.<\/p>\n<p>The second bool-type parameter that can be passed to the SendKeys function is the optional <em>[wait]<\/em> parameter. In our example, the parameter is not relevant and could be omitted. However, if further code follows <em>SendKeys<\/em>, then the parameter can be of importance. For the parameter <em><strong>[wait] = true<\/strong><\/em> determines whether to wait until <em>SendKeys<\/em> has executed all keyboard commands before executing the subsequent code. If <em><strong>[wait] = false<\/strong><\/em>, which is the default value, then the subsequent code in the function or procedure is executed immediately without waiting until all keyboard commands have been executed. This can lead to unsightly effects under certain circumstances, but depends on the VBA code in question.<\/p>","protected":false},"excerpt":{"rendered":"<p>VBA is wonderfully suited to automating certain work processes in the Microsoft Office world and thus keeping daily tedious work<\/p>\n","protected":false},"author":2,"featured_media":6532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[2714,2713,2252,3035,1707,2737,1857,1858,2168,1569,1700,1861,1862,2068],"class_list":["post-59643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-automate","tag-automatic","tag-code-en","tag-e-mail-en","tag-macro","tag-macros","tag-microsoft-en-2","tag-microsoft-office-en-2","tag-office-en","tag-outlook-en","tag-programming","tag-vba-en-2","tag-visual-basic-for-application-en-2","tag-windows-en-2"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/59643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=59643"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/59643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/6532"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=59643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=59643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=59643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}