{"id":19,"date":"2010-12-02T16:08:57","date_gmt":"2010-12-03T00:08:57","guid":{"rendered":"http:\/\/gis.qtools.com\/blog\/"},"modified":"2016-04-11T11:19:21","modified_gmt":"2016-04-11T18:19:21","slug":"part-2-adding-the-vba-utility-to-arcmap","status":"publish","type":"page","link":"https:\/\/gis.qtools.com\/blog\/tutorials\/vba-to-c-add-in\/part-2-adding-the-vba-utility-to-arcmap\/","title":{"rendered":"Part 2: Adding the VBA Utility to ArcMap"},"content":{"rendered":"<p><strong>Tutorial Navigation<\/strong> | Previous: <a href=\"..\/part-1-overview-of-the-vba-utility\/\">Part 1: Overview of the VBA Utility<\/a> | Next: <a href=\"..\/part-3-setting-up-visual-studio\/\">Part 3: Setting Up Visual Studio<\/a><\/p>\n<p>This step demonstrates the process of adding a VBA utility to ArcMap.<\/p>\n<h1>Resources<\/h1>\n<h3>Files<\/h3>\n<p>The related code and form files can be downloaded here: <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/data\/VBA_Src_SampleData.zip\">VBA Source and Sample Data<\/a> (141 KB)<br \/>\nContents (click to expand for more information):<\/p>\n<ul>\n<li>VBA_Tutorial_01_v9.mxd (<a href=\"#\" onclick=\"xcollapse('X7146');return false;\">+\/-<\/a>)\n<div id=\"X7146\" style=\"display: none; background: transparent;\">\nSample map document to use when following tutorial to add VBA elements to global &#8220;normal.mxt&#8221; template in ArcMap 9.3.<\/p>\n<\/div>\n<\/li>\n<li>VBA_Tutorial_01_VBAembed_v9.mxd (<a href=\"#\" onclick=\"xcollapse('X6363');return false;\">+\/-<\/a>)\n<div id=\"X6363\" style=\"display: none; background: transparent;\">\nSame map document as above with the addition of the VBA elements stored in the mxd. The toolbar and button will appear on the tool panels when the document is opened, and the code and form can be found in the visual basic editor under the &#8220;Project&#8221; tree.<\/p>\n<\/div>\n<\/li>\n<li>VBA_Tutorial_01_VBAembed_v10.mxd (<a href=\"#\" onclick=\"xcollapse('X7769');return false;\">+\/-<\/a>)\n<div id=\"X7769\" style=\"display: none; background: transparent;\">\nSame map document as above saved in ArcMap version 10. You will need to have installed the separate VBA environment into ArcMap 10 in order to use and view the embedded VBA code and form.<\/p>\n<\/div>\n<\/li>\n<li>sample_data.mdb (<a href=\"#\" onclick=\"xcollapse('X3412');return false;\">+\/-<\/a>)\n<div id=\"X3412\" style=\"display: none; background: transparent;\">\nPersonal geodatabase with one feature class, &#8220;Hwy_94_Overlaps&#8221;, containing 13 line features of various lengths that overlap on the same highway. <\/p>\n<\/div>\n<\/li>\n<li>vba_source\\form_OffsetFeatures (.frm, .frx) (<a href=\"#\" onclick=\"xcollapse('X303');return false;\">+\/-<\/a>)\n<div id=\"X303\" style=\"display: none; background: transparent;\">\nImportable VBA form file that provides a simple UI dialog. <\/p>\n<\/div>\n<\/li>\n<li>vba_source\\offset_features.bmp (<a href=\"#\" onclick=\"xcollapse('X10509');return false;\">+\/-<\/a>)\n<div id=\"X10509\" style=\"display: none; background: transparent;\">\nBitmap to use for the utilities UI button. <\/p>\n<\/div>\n<\/li>\n<li>vba_source\\OffsetFeatures.txt (<a href=\"#\" onclick=\"xcollapse('X9405');return false;\">+\/-<\/a>)\n<div id=\"X9405\" style=\"display: none; background: transparent;\">\nVBA source code for the utility that can be copy and pasted into the VBA editor. <\/p>\n<\/div>\n<\/li>\n<\/ul>\n<h3>Video Demonstrations<\/h3>\n<p>The following videos are also linked from the relevant sections below. (opens in a new browser window or tab)<\/p>\n<p><a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt1.swf.html\" target=\"_blank\">Adding the VBA code and form to ArcMap<\/a> (duration 0:58)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt2.swf.html\" target=\"_blank\">Adding a new toolbar in ArcMap<\/a> (duration 0:30)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt3.swf.html\" target=\"_blank\">Adding a button control to the toolbar in ArcMap<\/a> (duration 1:04)<\/p>\n<h1>Use the Embedded VBA Code and Forms<\/h1>\n<p>In <strong>ArcMap 10.0<\/strong>, simply open up the map document that has the VBA Utility embedded in the MXD (<em>VBA_Tutorial_01_VBAembed_v10.mxd<\/em>). Before doing so, ensure that the VBA environment for ArcMap has been installed and activated. Activation requires requesting an authorization code from ESRI and entering it into the ArcGIS Adminstrator application. ArcMap 10 does <em>not<\/em> support placing VBA code in a <em>Normal.mxt<\/em> to make it available to all documents.<br \/>[<em>NOTE: ArcMap 10.1 will not support VBA at all<\/em>. (<a href=\"http:\/\/downloads2.esri.com\/support\/TechArticles\/ArcGIS10and101Deprecation_Plan.pdf\">ESRI Deprecation Document<\/a>)]<\/p>\n<p>In <strong>ArcMap 9.x<\/strong>, the VBA environment is installed by default. Either open up the map document that has the VBA Utility embedded in the MXD (VBA_Tutorial_01_VBAembed_v9.mxd), or follow the instructions below on how to manually add the utililty to the ArcMap 9.x &#8220;Normal.mxt&#8221; so that it is available to any map document. In that case use the map document without the embedded VBA (<em>VBA_Tutorial_01_v9.mxd<\/em>).<\/p>\n<h1>Manually Add the Code, Form, Toolbar and Button in ArcMap 9.x<\/h1>\n<h2>Add the VBA Code and Form to ArcMap<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt1.swf.html\" target=\"_blank\">Adding the VBA code and form to ArcMap<\/a> (duration 0:58)<\/p>\n<p>Open the provided <em>VBA_Tutorial_01_v9.mxd<\/em> file.<\/p>\n<h3>Open the VBA editor<\/h3>\n<p>ArcMap&#8217;s VBA editor can be opened by either pressing <em>ALT+F11<\/em>, or selecting it on the <em>Tools | Macros<\/em> menu.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_01.png\" \/><\/p>\n<h3>Import the VBA form file<\/h3>\n<p>Right-click on <em>Normal (Normal.mxt)<\/em> in the VBA editor&#8217;s project inspector and select <em>Import File&#8230;<\/em> on the context menu.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_02.png\" \/><\/p>\n<p>Browse to the provided form file (<em>form_OffsetFeatures.frm<\/em>), where you unzipped it on your system.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_03.png\" \/><\/p>\n<p>After importing the form, a new <em>Forms<\/em> folder will be added in the project inspector. Expand that folder and double-click on the new <em>form_OffsetFeatures<\/em> item to view the form in the designer.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_04.png\" \/><\/p>\n<h3>Copy and paste the VBA source code<\/h3>\n<p>Expand the <em>ArcMap Objects<\/em> folder and double-click on the <em>ThisDocument<\/em> item to open the code editor. Locate the <em>OffsetFeatures.txt<\/em> file which contains the source code and open it in a simple text editor such as Notepad. Select all the text (<em>CTRL+A<\/em>) and copy it to the clipboard (<em>CTRL+C<\/em>), then close the text editor. Click in the code area in the VBA code editor and paste the code (<em>CTRL+V<\/em>).<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_05.png\" \/><\/p>\n<h2>Add a New Empty Toolbar<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt2.swf.html\" target=\"_blank\">Adding a new toolbar in ArcMap<\/a> (duration 0:30)<\/p>\n<p>Close the VBA Editor. Select <em>Customize&#8230;<\/em> from ArcMap&#8217;s <em>Tools<\/em> menu. On the <em>Customize<\/em> dialog, select the <em>Toolbars<\/em> tab. Click the <em>New&#8230; <\/em>button. Enter a name for the toolbar and confirm whether <em>Save in<\/em> is set to your preference. Click <em>OK<\/em> and the new toolbar will appear floating over the ArcMap application. Drag the toolbar to an empty space on a tool panel to dock it.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_06.png\" \/><\/p>\n<h2>Add a Button Control to the Toolbar in ArcMap<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/VBA_StaggerOffset_Setup_Demo_pt3.swf.html\" target=\"_blank\">Adding a button control to the toolbar in ArcMap<\/a> (duration 1:04)<\/p>\n<h3>Add a new button linked to the VBA code<\/h3>\n<p>Select the <em>Commands<\/em> tab page. Ensure the <em>Save In <\/em>setting is at your preference. Scroll to the very bottom of the <em>Categories <\/em>list and select the <em>[UIControls]<\/em> item. Click the <em>New UIControl&#8230; <\/em>button and select the <em>UIButtonControl <\/em>item on the subsequent dialog and click <em>OK<\/em>. This will enter a new item in the <em>Commands<\/em> list with a default name. Click on the name once to select it, and again (after a delay) to place the text in edit mode. Change the part of the name after the dot to <em>OffsetFeatures<\/em>. It must exactly match the sub and function names used in the VBA code (i.e. <em>OffsetFeatures_click() <\/em>) for the button to be linked to the code. Press <em>Enter <\/em>after editing the name to save the change, but do not close the <em>Customize <\/em>dialog.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_07.png\" \/><\/p>\n<h3>Add the button to the toolbar and set its icon<\/h3>\n<p>Drag the small button icon to the left of the button control&#8217;s name in the <em>Commands <\/em>list to the empty toolbar you already created. To change the button&#8217;s icon, right-click on the button on the toolbar to open its context menu. Hover over the <em>Change Button Image <\/em>item and then click <em>Browse&#8230;<\/em> and locate the <em>offset_features.bmp <\/em>file provided. The <em>Customize <\/em>dialog must remain open during this operation.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/VBA_StaggerOffset_Setup_08.png\" \/><\/p>\n<p>Close the <em>Customize <\/em>dialog.<\/p>\n<p>That&#8217;s it! You have hooked the button to the code. To test it, start an edit session and select the <em>Hwy_94_Overlaps <\/em>layer in ArcMap. Click the new <em>Stagger Offsets Features <\/em>button, enter 350 in the <em>East-West Route <\/em>box, and click <em>OK<\/em>.<\/p>\n<p><strong>Tutorial Navigation<\/strong> | Previous: <a href=\"..\/part-1-overview-of-the-vba-utility\/\">Part 1: Overview of the VBA Utility<\/a> | Next: <a href=\"..\/part-3-setting-up-visual-studio\/\">Part 3: Setting Up Visual Studio<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial Navigation | Previous: Part 1: Overview of the VBA Utility | Next: Part 3: Setting Up Visual Studio This step demonstrates the process of adding a VBA utility to ArcMap. Resources Files The related code and form files can be downloaded here: VBA Source and Sample Data (141 KB) Contents (click to expand for [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":8,"menu_order":2,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-19","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":3,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/19\/revisions"}],"predecessor-version":[{"id":66,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/19\/revisions\/66"}],"up":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/8"}],"wp:attachment":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}