{"id":65553,"date":"2007-02-09T00:42:00","date_gmt":"2007-02-09T00:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/09\/hey-scripting-guy-how-can-i-save-an-office-excel-chart-as-a-picture\/"},"modified":"2007-02-09T00:42:00","modified_gmt":"2007-02-09T00:42:00","slug":"hey-scripting-guy-how-can-i-save-an-office-excel-chart-as-a-picture","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-save-an-office-excel-chart-as-a-picture\/","title":{"rendered":"Hey, Scripting Guy! How Can I Save an Office Excel Chart as a Picture?"},"content":{"rendered":"<h2><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/> <\/h2>\n<p>Hey, Scripting Guy! How can I save an Office Excel chart as a picture?<\/p>\n<p>&#8212; JB<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, JB. You know, yesterday was a good day for the Scripting Guy who writes this column. If the phone rings at the Scripting House and if it&rsquo;s for the Scripting Guy who writes this column, well, it&rsquo;s never <i>really<\/i> for the Scripting Guy who writes this column; instead, it&rsquo;s someone trying to sell him something, or someone wondering if they can count on him to donate to their annual fund-raising drive. (Not being particularly smart, the Scripting Guy who writes this column always thought annual meant &ldquo;once &ndash; and only once &ndash; a year.&rdquo;) The same is true for the mail: if something is addressed to the Scripting Guy who writes this column (as opposed to Occupant or Current Resident) then it&rsquo;s a bill.<\/p>\n<p>Either that, or a restraining order from one of the other Scripting Guys.<\/p>\n<p>Yesterday, however, was different: yesterday the Scripting Guy who writes this column actually got a letter addressed specifically to him. As it turns out, a new European day spa was opening in town, and they were offering the Scripting Guy who writes this column a chance to get a complete makeover, at 50% off the regular price. <i>(Editor&rsquo;s Note: Obviously they&rsquo;ve never seen the Scripting Guy who writes his column, or they&rsquo;d <\/i>never<i> have offered a discount on a complete makeover.<\/i><i> They would have charged extra.<\/i><i>)<\/i><\/p>\n<p>Now, admittedly, the Scripting Guy who writes this column thought these folks were being a bit na&iuml;ve: after all, if they really <i>do<\/i> think they can give him a complete makeover in just one day, well &hellip;. Still, he was intrigued by the offer. What we&rsquo;re about to say might come as a shock to those of you who read this column regularly, and have understandably pictured the Scripting Guy who writes this column as very cultured and refined. But it&rsquo;s true: the Scripting Guy who writes this column has never been to a European day spa. Never. &ldquo;Maybe it&rsquo;s time to give this a try,&rdquo; he thought.<\/p>\n<p>With that in mind he perused the different offerings, and immediately discovered that he was way out of his league. For example, he wasn&rsquo;t totally sure about the Full Body Chocolate Raspberry Massage, a massage that promised to give him &ldquo;skin like you&rsquo;ve never had before.&rdquo; Usually if something includes the tem <i>Chocolate Raspberry<\/i> the Scripting Guy who writes this column will take at least one. This was a little different, however. Plus there was the promise about skin like he&rsquo;s never had before; what exactly did that mean? After all, the Scripting Guy who writes this column has never had skin like an iguana or a pterodactyl before.<\/p>\n<p>Which isn&rsquo;t to say that having skin like an iguana or pterodactyl might not be an improvement over the skin he currently has.<\/p>\n<p>Even more confusing was the Heated Swiss Chocolate Milkshake Foot Bath and Beverage. Philosophically, the Scripting Guy who writes this column had difficulty figuring out how you could heat a milkshake and still have a milkshake. Far more important, he wasn&rsquo;t sure whether this offer consisted of one item or two. Do you get a foot bath and then, on top of the foot bath, get a beverage as well? Or was this one item, a combination foot bath and beverage? To tell you the truth, the Scripting Guy who writes this column didn&rsquo;t really want to know.<\/p>\n<p>As it turned out, though, it was all a moot point: even at 50% off a day at the European day spa cost $299. For $299, the Scripting Guy who writes those column can make 100 or so trips to Dairy Queen and heat his own milkshakes.<\/p>\n<p>Besides, he doesn&rsquo;t have time to lounge around at a European day spa; he has work to do:<\/p>\n<pre class=\"codeSample\">Set objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nSet objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)\n\nobjWorksheet.Cells(1,1) = \"Operating System\"\nobjWorksheet.Cells(2,1) = \"Windows Server 2003\"\nobjWorksheet.Cells(3,1) = \"Windows XP\"\nobjWorksheet.Cells(4,1) = \"Windows 2000\"\nobjWorksheet.Cells(5,1) = \"Windows NT 4.0\"\nobjWorksheet.Cells(6,1) = \"Other\"\n\nobjWorksheet.Cells(1,2) = \"Number of Computers\"\nobjWorksheet.Cells(2,2) = 145\nobjWorksheet.Cells(3,2) = 987\nobjWorksheet.Cells(4,2) = 611\nobjWorksheet.Cells(5,2) = 41\nobjWorksheet.Cells(6,2) = 56\n\nSet objRange = objWorksheet.UsedRange\nobjRange.Select\n\nSet colCharts = objExcel.Charts\ncolCharts.Add()\n\nSet objChart = colCharts(1)\nobjChart.Activate\n\nobjChart.Export \"C:\\Scripts\\Test.jpg\", \"JPG\"<\/pre>\n<p>Well, OK: not <i>too<\/i> much work. We&rsquo;re actually not going to explain the details behind creating a chart in Microsoft Excel; that&rsquo;s because we have entire <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><b>series of articles<\/b><\/a> devoted to that topic. Instead, we&rsquo;ll just briefly note that our script starts out by creating an instance of the <b>Excel.Application<\/b> object and then sets the <b>Visible<\/b> property to True; that gives us a running instance of Excel that we can see onscreen. We use the <b>Add<\/b> method to add a new, blank workbook to our running instance of Excel, then use this line of code to bind to the first worksheet in that workbook:<\/p>\n<pre class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(1)<\/pre>\n<p>That&rsquo;s all pretty easy. What follows next are several lines of code designed to add data to the worksheet. When that&rsquo;s done, we select all the data (using the <b>UsedRange<\/b> property and the <b>Select<\/b> method), then use these two lines of code to create a chart:<\/p>\n<pre class=\"codeSample\">Set colCharts = objExcel.Charts\ncolCharts.Add()<\/pre>\n<p>The resulting chart will look a little like this:<\/p>\n<p><img decoding=\"async\" height=\"243\" width=\"335\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/office\/tips\/charts\/graph3.jpg\" alt=\"Microsoft Excel\" border=\"0\" \/><\/p>\n<p>And no, sorry, it&rsquo;s just a plain old chart. We haven&rsquo;t yet figured out how to make a combination chart and foot bath, although we plan to continue the research.<\/p>\n<p>Now that we have a chart it&rsquo;s time to export said chart as a picture. In order to do that we need to create an object reference to the chart itself. That&rsquo;s something we can do by binding to the chart (which, of course, is the first &ndash; and only &ndash; chart in our Charts collection) and then using the <b>Activate<\/b> method to make this the active chart:<\/p>\n<pre class=\"codeSample\">Set objChart = colCharts(1)\nobjChart.Activate<\/pre>\n<p>At that point, saving the chart as a picture is as easy as calling the <b>Export<\/b> method:<\/p>\n<pre class=\"codeSample\">objChart.Export \"C:\\Scripts\\Test.jpg\", \"JPG\"<\/pre>\n<p>As you can see, all we do here is call the Export method, handing that method two parameters: the complete path to the resulting picture file, and the type of picture file we&rsquo;re creating. In this example, we&rsquo;re creating a file named C:\\Scripts\\Test.jpg and we want the resulting picture to be a .JPG file; hence the <b>&ldquo;JPG&rdquo;<\/b> parameter. Can we use other graphic types? You bet; for example, we know for sure that charts can be saved as .GIF files or as .PNG files. Can charts be saved in <i>other<\/i> formats? Probably, but we didn&rsquo;t try any other format. We were saving that to help pass the time while getting our Chocolate Raspberry massage.<\/p>\n<p>Of course, we know the question that&rsquo;s now on everyone&rsquo;s mind: will the Scripting Guys be giving away Heated Swiss Chocolate Milkshake Foot Baths and Beverages as part of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2007 Winter Scripting Games<\/b><\/a>? Just a second; we&rsquo;ll check on that &hellip;. <\/p>\n<p>No, sorry; we won&rsquo;t be. However, we <i>are<\/i> giving away 250 <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><b>Dr. Scripto Bobblehead dolls<\/b><\/a>. On top of that, the good folks at <a target=\"_blank\" href=\"http:\/\/www.manning.com\/\"><b>Manning Publications<\/b><\/a> are giving away 20 copies of Bruce Payette&rsquo;s new book <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><i>Windows PowerShell in Action<\/i><\/a>. Is there a better and more definitive book on Windows PowerShell? Well, if there is, we haven&rsquo;t seen it. This is a prize worth winning.<\/p>\n<p>And how can you win one of these great prizes? Well, the requirements are pretty steep: you have to enter at least one event in the Scripting Games. That&rsquo;s it. Enter an event (you don&rsquo;t even have to successfully <i>complete<\/i> the event!) and you&rsquo;ll automatically be entered in a random drawing for one of these great prizes. We&rsquo;re sorry that we can&rsquo;t give Heated Swiss Chocolate Milkshake Foot Baths and Beverages. But we think the bobbleheads and Windows PowerShell books make for pretty good consolation prizes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save an Office Excel chart as a picture? &#8212; JB Hey, JB. You know, yesterday was a good day for the Scripting Guy who writes this column. If the phone rings at the Scripting House and if it&rsquo;s for the Scripting Guy who writes this column, well, it&rsquo;s never [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[711,48,49,3,5],"class_list":["post-65553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I save an Office Excel chart as a picture? &#8212; JB Hey, JB. You know, yesterday was a good day for the Scripting Guy who writes this column. If the phone rings at the Scripting House and if it&rsquo;s for the Scripting Guy who writes this column, well, it&rsquo;s never [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}