<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Life...as it Relates to Technology. &#187; vba</title>
	<atom:link href="http://www.hobbub.com/tag/vba/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hobbub.com</link>
	<description>Computers, Websites, Programming, Gadgets...</description>
	<lastBuildDate>Thu, 01 Sep 2011 15:38:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Understanding the VBA Editor</title>
		<link>http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/</link>
		<comments>http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 18:27:59 +0000</pubDate>
		<dc:creator>scott</dc:creator>
				<category><![CDATA[VBA/VSTO]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[vbe]]></category>

		<guid isPermaLink="false">http://www.hobbub.com/?p=1238</guid>
		<description><![CDATA[VBA has an easy-to-use interface called the Visual Basic Editor (VBE). In order to access the VBE, you first need to display the Developer tab. To show the Developer tab,  open a 2007 Office System application, for example Word 2007. Click the Microsoft Office Button, and then click Word Options. In the Top options for [...] <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/">Understanding the VBA Editor</a></span>]]></description>
			<content:encoded><![CDATA[<p>VBA has an easy-to-use interface called the Visual Basic Editor (VBE). In order to access the VBE, you first need to display the Developer tab. To show the Developer tab,  open a 2007 Office System application, for example Word 2007. Click the Microsoft Office Button, and then click Word Options. In the Top options for working with Word section of the Word Options dialog box, click Personalize. Select Show Developer tab in the Ribbon, as shown in the next figure. Click OK. </p>
<p>Once the Developer tab is displayed,  you can access in Word by choosing Visual Basic (or press ALT+F11). If you’ve only been a Word user up until now, the first thing you will notice in Figure below is that the VBE doesn’t resemble anything close to a document. The VBE is a very powerful development environment. </p>
<p>The application window is divided into three subwindows when you first enter the VBE. On the right are the Project Explorer and Property windows. The main window is the Module window. This is where you’ll be writing the majority of your code. You can add other debugging windows to monitor variable values while you step through code. These debugging windows will be covered later.</p>
<p><a rel="attachment wp-att-1239" href="http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/attachment/vba_editor/"><img class="aligncenter size-medium wp-image-1239" title="vba_editor" src="http://www.hobbub.com/wp-content/uploads/2010/11/vba_editor-300x217.jpg" alt="VBA Editor Example" width="300" height="217" /></a></p>
<h2>VBE Component Basics</h2>
<p>The Visual Basic Editor contains a number of development tools that were once found only in advanced development environments. Each of these components appears in a different window and each window may be resized, moved, or docked (double-click the left mouse button on the title bar or choose Tools|Options|Docking). If you need to view Help for an individual window within VBE, position your cursor within the window and press F1. Each of these windows is described below.</p>
<h4><em>Module Window</em></h4>
<p>The main window that occupies the majority of the screen is the Module window, shown in Figure 4-1. This is where you enter the VBA code that lies behind Word template or document. The Module window displays the VBA code that is associated with the selected object. If you are viewing an object, you can display the Module window by double-clicking the object. For example, add a UserForm, then double-click the UserForm to display the Module ­window (the process of double-clicking will also automatically create a subroutine for the UserForm_DoubleClick event).</p>
<h4><em>Object Window and Procedure List</em></h4>
<p>At the top of the Module window are two drop-down menus. The drop-down menu on the left is called the Object List. It contains a list of all objects in the active module. For instance, when the active module is a UserForm, all of the controls on the UserForm will appear in the drop-down. Finally, most modules also contain a General section. The General section is a place where general declarations can be typed. We’ll cover general declarations later, but basically these include any option statements, such as Option Explicit (to force variable declaration) or Option Compare Text (to compare text rather than ascii character values). The drop-down list on the right is the Procedure and Event List. This will contain all of the available events for the current object.</p>
<h4><em>Project Explorer</em></h4>
<p>In the upper-left corner is the Project Explorer window, shown in figure below. In Word, this window has a tree structure representing all open documents and templates. These documents and templates are projects as far as VBA is concerned. A project is basically a warehouse for Objects and the code behind them. You can click on any of the small plus signs to expand the branch; this will expose any of the five possible nodes: ThisDocument, UserForms, Modules, Class Modules, and References. In addition, there are two small buttons toward the top of the window that can be used to switch between viewing a form and its accompanying code.</p>
<p><a rel="attachment wp-att-1240" href="http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/attachment/project_window/"><img class="aligncenter size-medium wp-image-1240" title="project_window" src="http://www.hobbub.com/wp-content/uploads/2010/11/project_window-284x300.jpg" alt="Project Window" width="284" height="300" /></a></p>
<h4><em>UserForms</em></h4>
<p>The UserForms branch will contain any customized UserForms attached to the document or template. The term UserForm is often used interchangeably with dialog box (generally, in the VBA vernacular, a UserForm is one that is custom created, and a dialog box is one that is built into the program). It may help to think of a UserForm as a blank object that can be turned into a dialog box through the use of controls and code. Controls facilitate certain tasks by executing the code behind the control’s events. The actual UserForm is manipulated by changing its properties or adding controls from the toolbox (Edit|Toolbox).</p>
<p><a rel="attachment wp-att-1241" href="http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/attachment/displaying_userforms/"><img class="aligncenter size-medium wp-image-1241" title="displaying_userforms" src="http://www.hobbub.com/wp-content/uploads/2010/11/displaying_userforms-300x105.jpg" alt="Displaying Userforms" width="300" height="105" /></a></p>
<h3>Modules</h3>
<p>Modules are basically storage areas for procedures (functions and subroutines). Variables may also be declared outside of procedures in the Modules section.</p>
<h4><em>Class Modules</em></h4>
<p>Class modules are a specialized form of a module that allow for the creation of an object. Class modules can be used in creative ways to capture events for many intrinsic objects that are dynamically created or accessed.</p>
<h3>References</h3>
<p>The References section here allows for one project (usually a document) to call procedures in another project (usually the template used to create that document). You will see that references are invaluable for using Office automation. In Word, all documents will contain a reference to the template that was used to create them.</p>
<p>Tip:</p>
<p>Do not confuse a reference displayed in the Project Explorer with one that shows up in the Tools|References section. References in Word’s Project Explorer are generally other templates. References in the Tools|References section refer to the object libraries available to the project (usually DLL files).</p>
<h3>ThisDocument</h3>
<p>The ThisDocument section contains the properties of the actual document or template. This section operates in a very similar manner to a standard code module. If you are familiar with any of the recent Word macro viruses, you probably have seen how they use this section to propagate.</p>
<h4><em>The Properties Window</em></h4>
<p>The Properties window appears immediately below the Project window. See Figure 4-4. This window displays the design time properties for the object that has the focus.</p>
<p><em>Word VBA Tip</em>: The object may have other properties that do not show in the Properties window. Several objects have properties that are only available at run time.</p>
<p>As a general rule, you can think of properties as the adjectives of an object. They determine an object’s characteristics. Common properties include height, width, background color, font, etc. In addition to these simple properties, all controls have advanced properties that allow greater manipulation to achieve desired results.</p>
<p><a rel="attachment wp-att-1242" href="http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/attachment/properties_window/"><img class="aligncenter size-medium wp-image-1242" title="properties_window" src="http://www.hobbub.com/wp-content/uploads/2010/11/properties_window-272x300.jpg" alt="Properties Window" width="272" height="300" /></a></p>
<p><em>  Note:</em>  Some properties may be changed only at run time and thus will not show up in the Properties window, while others are read-only and do not allow the alteration of their values.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/" target="_blank"><img src="http://www.hobbub.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/" target="_blank" title="Share on Facebook">Share on Facebook</a></p><img src="http://www.hobbub.com/?ak_action=api_record_view&id=1238&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.hobbub.com/vba-vb-vsto/understanding-the-vba-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterating over a Directory with a Dir Loop (Loop Through Directory) VB</title>
		<link>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/</link>
		<comments>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 20:46:03 +0000</pubDate>
		<dc:creator>scott</dc:creator>
				<category><![CDATA[VBA/VSTO]]></category>
		<category><![CDATA[dir function]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/</guid>
		<description><![CDATA[VBA is potentially the greatest desktop &#8216;hammer&#8217; you have at your disposal. You can pop open any Office application, hit ALT+F11 and be writing code before Visual Studio will even be open on most machines.
For doing ad hoc work around the office, VBA allows you to do things that applications alone simply aren&#8217;t powerful enough [...] <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/">Iterating over a Directory with a Dir Loop (Loop Through Directory) VB</a></span>]]></description>
			<content:encoded><![CDATA[<p>VBA is potentially the greatest desktop &#8216;hammer&#8217; you have at your disposal. You can pop open any Office application, hit ALT+F11 and be writing code before Visual Studio will even be open on most machines.</p>
<p>For doing ad hoc work around the office, VBA allows you to do things that applications alone simply aren&#8217;t powerful enough to do. It seems like I&#8217;m always running into a situation where I need to get information about a bunch of files in a directory. Let&#8217;s say that you just need to retrieve the names of all files in a certain directory and write them to a column in an Excel spreadsheet.</p>
<p>Unfortunately, Excel doesn&#8217;t provide a native way to accomplish such a task. However, the task is reduced to less than 15 lines of code using VBA. The <strong>VBA Dir Function</strong> is great for iterating over a directory.</p>
<h3>Understanding the Dir Function</h3>
<p>This function returns a <strong>String</strong> value containing the name of a file, directory, or folder that matches a specified pattern or file attribute. If you&#8217;re working at the root level of a file structure, the Dir function will also return the volume label of a drive.<strong> </strong>returns the first file name that matches <em>pathname</em> parameter. The trick to looping through a directory is that <strong><em>calling Dir with no arguments</em></strong> returns any additional file names that match <em>pathname</em>. You can see an example of this in the code snippet below: the line with the comment, &#8220;get next entry&#8221; is where the Dir function will retrieve the next matching filename.</p>
<p>Dir</p>
<p>When <strong>Dir</strong> can no longer return a match, it returns a zero-length string (&#8220;&#8221;). If a zero-length string is returned, either a new <em>pathname</em> must be passed or an error occurs. You can change to a new <em>pathname</em> without retrieving all of the file names that match the current <em>pathname</em>.</p>
<p>NOTE: Do not nest <strong>Dir loops</strong> and don&#8217;t attempt to call the <strong>Dir</strong> function recursively. Calling <strong>Dir</strong> with the <strong>vbDirectory</strong> attribute does not continually return subdirectories. If you think you need to recurse directories, first consider whether you can copy the results of a simple Windows Search to a new directory to achieve your desired results.</p>
<h4>Looping Over Files with Dir, Looping Through Files, Looping Through Directories&#8230;</h4>
<p>The following VB code snippet is pretty easy to understand. There are three variables: strPath, strFile and x. strPath is used to pass the initial parameter to the Dir function. This simply tells Dir which directory to traverse. strFile is used to hold the file name as the code executes and continues to retrieve file names. x is used simply as a counter so that the correct cell in the Excel spreadsheet is updated with the correct filename (Dir doesn&#8217;t return files in any specific order, so you may need to sort them afterward).</p>
<div style="padding-left: 30px;"><span style="font-family: monospace;"><span style="color: #000080;">Sub LoopThruDirectory()<br />
   <br />
Dim strPath As String<br />
Dim strFile As String<br />
Dim x As Integer</span><span style="font-family: monospace;"><span style="font-family: monospace;"><span style="color: #000080;">    strPath = &#8220;C:\temp\datajunction\XMLOUT\&#8221;<br />
    strFile = Dir(strPath)<br />
       <br />
    Do While strFile &lt;&gt; &#8220;&#8221;<br />
        x = x + 1<br />
        Sheet1.Cells(x, 1) = strFile<br />
        strFile = Dir    &#8216; Get next entry.<br />
    Loop<br />
   <br />
End Sub<br />
</span></span></span></span></div>
<p>Wise Man Says, <a title="bird in hand" href="http://www.hobbub.com/aphorisms/a-bird-in-hand-is-worth-two-in-a-bush/">&#8220;A Bird in Hand is Worth Two in Bush&#8221;</a></p>
<p>Quick Note: <a title="Recursively Traversing Nested Folders" href="http://www.hobbub.com/vba-vb-vsto/vbavb-recursing-through-nested-directories/">Recursing a Directory Function is Here</a></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/" target="_blank"><img src="http://www.hobbub.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/" target="_blank" title="Share on Facebook">Share on Facebook</a></p><img src="http://www.hobbub.com/?ak_action=api_record_view&id=1049&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

