{"id":516,"date":"2013-03-03T13:47:02","date_gmt":"2013-03-03T13:47:02","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=516"},"modified":"2020-04-23T10:57:28","modified_gmt":"2020-04-23T10:57:28","slug":"excel-vba-lesson-21-the-workbook-object","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/","title":{"rendered":"Excel VBA Lesson 21: The Workbook Object"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-20-worksheet-object\/\">&lt;&lt;Lesson 20&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-22-checkbox\/\">&lt;&lt;Lesson 22&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>In the previous lesson, we have learned to write code associated with the worksheet object. In this lesson, we shall learn about the Workbook object. The Workbook object at the top of the hierarchy of the Excel VBA objects. We will deal with properties and methods associated the Workbook object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>21.1 The Workbook Properties<\/strong><\/h3>\n\n\n\n<p>When we write Excel VBA code involving the Workbook object, we use Workbooks. The reason is that we are dealing with a collection of workbooks most of the time, so using Workbooks enables us to manipulate multiple workbooks at the same time.When will deal with multiple workbooks, we can use indices to denote different workbooks that are open, using the syntax Workbooks (i), where i is an index. For example, Workbooks (1) denotes Workbook1, Workbooks (2) denotes Workbook2 and more.Workbooks have a number of properties. Some of the common properties are Name, Path and FullName Let\u2019s look at the following example:<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.1<\/strong><\/h4>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nMsgBox Workbooks(1).Name<br>\nEnd Sub<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"179\" height=\"152\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\" alt=\"Excel VBA\" class=\"wp-image-519\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Running the program produces a message box that displays the first workbook name, i.e. workbook_object1.xls as shown in Figure 17.1 below:<br>\n\n<\/p>\n\n\n\n<p><strong>Figure 21.1<\/strong><\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>\nIf we have only one open workbook, we can also use the syntax ThisWorkbook in place of Workbook (1), as follows:<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click ()<br>\nMsgBox ThisWorkbook.Name<br>\nEnd Sub<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.2<\/strong><\/h4>\n\n\n\n<p>Private Sub CommandButton1_Click ()<br>\nMsgBox ThisWorkbook.Path<br>\nEnd SubOr you can use the following code:<\/p>\n\n\n\n<p>Private Sub CommandButton1Click ()<br>\nMsgBox Workbooks (&#8220;workbook_object1.xls&#8221;).Path<br>\nEnd Sub<\/p>\n\n\n\n<p>The output is shown below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"168\" height=\"134\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.2.jpg\" alt=\"Excel VBA\" class=\"wp-image-520\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 21.2<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.3<\/strong><\/h4>\n\n\n\n<p>This example will display the path and name of the opened workbook. The code is:<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click ()<br>\nMsgBox ThisWorkbook.FullName<br>\nEnd Sub<\/p>\n\n\n\n<p>Or<\/p>\n\n\n\n<p>Private Sub CommandButton1Click()<br>\nMsgBox Workbooks(&#8220;workbook_object1.xls&#8221;).Fullname<br>\nEnd Sub<\/p>\n\n\n\n<p>The output is shown in Figure 17.3.\n<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.3.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"287\" height=\"152\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.3.jpg\" alt=\"Excel VBA\" class=\"wp-image-521\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 21.3<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>21.2 The Workbook Methods<\/strong><\/h3>\n\n\n\n<p>There are a number of methods associated with the workbook object. These methods are Save, SaveAs, Open, Close and more.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.4<\/strong><\/h4>\n\n\n\n<p>In this example, when the user clicks on the command button, it will open up a dialog box and ask the user to specify a path and type in the file name, and then click the save button, not unlike the standard windows SaveAs dialog, as shown in Figure 17.4.<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nfName = Application.GetSaveAsFilename<br>\nThisWorkbook.SaveAs Filename:=fName<br>\nEnd Sub<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.4.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"405\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.4.jpg\" alt=\"Excel VBA\" class=\"wp-image-523\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 21.4<\/strong><\/p>\n\n\n\n<p>Another method associated with the workbook object is open. The syntax is<br>\nWorkbooks.Open (&#8220;File Name&#8221;)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.5<\/strong><\/h4>\n\n\n\n<p>In this example, when the user clicks on the command button, it will open the file workbook_object1.xls under the path C:\\Users\\liewvk\\Documents\\<br>\nPrivate Sub CommandButton1_Click()<br>\nWorkbooks.Open (&#8220;C:\\Users\\liewvk\\Documents\\workbook_object1.xls&#8221;)<br>\nEnd Sub<\/p>\n\n\n\n<p>The close method is the command that closes a workbook. The syntax is<br>\nWorkbooks (i).Close<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 21.6<\/strong><\/h4>\n\n\n\n<p>In this example, when the user clicks the command button, it will close Workbooks (1).<br>\nPrivate Sub CommandButton1_Click()<br>\nWorkbooks (1).Close<br>\nEnd Sub<br>\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-16\/\">&lt;&lt;Lesson 20&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-22-checkbox\/\">&lt;&lt;Lesson 22&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 20&gt;&gt; [Contents] &lt;&lt;Lesson 22&gt;&gt; In the previous lesson, we have learned to write code associated with the worksheet object. In this lesson, we shall learn about the Workbook object. The Workbook object at the top of the hierarchy of the Excel VBA objects. We will deal with properties and methods associated the Workbook object. &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 21: The Workbook Object&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-516","page","type-page","status-publish","hentry","category-object"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson21.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson21.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T10:57:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"179\" \/>\n\t<meta property=\"og:image:height\" content=\"152\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm\",\"name\":\"Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\",\"datePublished\":\"2013-03-03T13:47:02+00:00\",\"dateModified\":\"2020-04-23T10:57:28+00:00\",\"description\":\"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson21.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg\",\"width\":179,\"height\":152},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson21.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 21: The Workbook Object\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/vba_lesson21.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object","og_url":"https:\/\/excelvbatutor.com\/vba_lesson21.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:57:28+00:00","og_image":[{"width":179,"height":152,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/","url":"https:\/\/excelvbatutor.com\/vba_lesson21.htm","name":"Excel VBA Lesson 21: The Workbook Object - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg","datePublished":"2013-03-03T13:47:02+00:00","dateModified":"2020-04-23T10:57:28+00:00","description":"This Excel VBA lesson demonstrate how to write code to manipulate Workbook Object","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson21.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson21.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson21.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure17.1.jpg","width":179,"height":152},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson21.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 21: The Workbook Object"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=516"}],"version-history":[{"count":33,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/516\/revisions"}],"predecessor-version":[{"id":3465,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/516\/revisions\/3465"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}