{"id":64093,"date":"2007-09-05T01:52:00","date_gmt":"2007-09-05T01:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/09\/05\/how-can-i-add-a-background-picture-to-an-excel-worksheet\/"},"modified":"2007-09-05T01:52:00","modified_gmt":"2007-09-05T01:52:00","slug":"how-can-i-add-a-background-picture-to-an-excel-worksheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-background-picture-to-an-excel-worksheet\/","title":{"rendered":"How Can I Add a Background Picture to an Excel Worksheet?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I add a background picture to an Excel worksheet?<BR><BR>&#8212; GA<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GA. You know, being the health-conscious type, each morning the Scripting Guy who writes this column eats some non-fat raspberry yogurt \u2026 right before he eats his daily doughnut. (But don\u2019t worry: it\u2019s a medically-proven fact that eating yogurt counteracts any potentially harmful or fattening effects of a doughnut.) This morning, as he was taking the lid off the yogurt container, he noticed the words <B>Sell By<\/B>, followed by this:<\/P>\n<P>06-4902<\/P>\n<P>Now, to be honest, the Scripting Guy who writes this column isn\u2019t exactly sure how to interpret this; however, it sure <I>looks<\/I> as though the yogurt maker is suggesting that this product must be sold no later June, 4902. If that\u2019s the case, then this Scripting Guy only has 2,895 years in which to finish off all the yogurt in his refrigerator. Needless to say, he\u2019s beginning to regret having bought a 3,000-year supply of nonfat raspberry yogurt. What was he thinking?<\/P>\n<TABLE class=\"dataTable\" id=\"EFD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. According to urban legend, Hostess Twinkies have a shelf life of anywhere from 50 to 100 years; pretty lame compared to nonfat raspberry yogurt, but pretty good for a pastry product. As a matter of fact, there are people who claim that Hostess stopped making Twinkies decades ago, and has simply been selling the old ones ever since. (The story is that Hostess terribly overestimated the number of Twinkies it would sell, and ended up making billions more than it needed.) Food experts say that, in reality, Twinkies have a shelf life of about 25 days, but there are <A href=\"http:\/\/en.wikipedia.org\/wiki\/Twinkie\" target=\"_blank\"><B>unconfirmed reports<\/B><\/A> of Twinkies surviving reasonably well for more than 30 years.<\/P>\n<P>Which is something none of the Script Guys can claim to have done.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, we can\u2019t be sure if yogurt really <I>does<\/I> have a shelf life of 2,895 years; most likely it doesn\u2019t. However, we <I>are<\/I> sure that today\u2019s <I>Hey, Scripting Guy!<\/I> column has a shelf life of at <I>least<\/I>, 2,895 years, especially when you consider that it deals with a subject like adding a background picture to an Excel worksheet:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add\nSet objWorksheet = objWorkbook.Worksheets(1)\nobjWorksheet.SetBackgroundPicture &#8220;C:\\Scripts\\Test.jpg&#8221;\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EGE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. We really <I>do<\/I> guarantee that this column has a shelf life of at least 2,895 years. If, come June, 4902, you are unable to locate this particular column, well, email <A href=\"mailto:scripter@microsoft.com\"><B>scripter@microsoft.com (in English, if possible)<\/B><\/A> and we will refund your money, no questions asked.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We have to admit that we found this to be an interesting little exercise; to be honest, we didn\u2019t even know you <I>could<\/I> add a background picture to an Excel worksheet. (We knew you could <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0714.mspx\"><B>add background pictures to a graph<\/B><\/A>, but didn\u2019t know you could do that to a worksheet.) As you can see, however, not only <I>can<\/I> you add a background picture to a worksheet, but you can do so with just a few lines of code. And that\u2019s good: after all, we only have 2,895 years to eat our yogurt <I>and<\/I> to add a background picture to a worksheet. At the typical Scripting Guys\u2019 pace, that\u2019s not all that much time.<\/P>\n<P>As for the script itself, we start out by creating an instance of the <B>Excel.Application<\/B> object and then setting the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Excel that we can see onscreen. We then use the following two lines of code to add a new workbook to our instance of Excel and bind to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Add\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>From there, setting a background picture is as easy as calling the <B>SetBackgroundPicture<\/B> method, passing the path to the image file as the sole method parameter:<\/P><PRE class=\"codeSample\">objWorksheet.SetBackgroundPicture &#8220;C:\\Scripts\\Test.jpg&#8221;\n<\/PRE>\n<P>Is that really all we need to do? You bet it is:<\/P><IMG height=\"366\" alt=\"Spacer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/excelbackground.jpg\" width=\"400\" border=\"0\"> \n<P><BR>Remember, this merely sets the picture in question as the background picture for the worksheet. If you just want to insert a picture into a worksheet (without making it the background), then you need to use code similar to this:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objWorksheet.Pictures.Insert(&#8220;C:\\Scripts\\Test.jpg&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is referencing the <B>Pictures<\/B> collection and calling the <B>Insert<\/B> method; that adds the image file C:\\Scripts\\Test.jpg to the worksheet. By default, this positions the picture so that the graphic\u2019s upper left-hand corner is in cell A1. Suppose you\u2019d rather have the upper left-hand corner of the picture in cell B10. That\u2019s fine; the following script makes cell B10 the active cell (<B>objExcel.Range(&#8220;B10&#8221;).Activate<\/B>) and <I>then<\/I> inserts the picture:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objExcel.Range(&#8220;B10&#8221;).Activate\nobjWorksheet.Pictures.Insert(&#8220;C:\\Scripts\\Test.jpg&#8221;)\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"ERG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Office Excel 2007:<\/B> The scripts in this article will work just fine in Excel 2007. The only difference is that the active cell doesn\u2019t seem to impact the position of the picture. So the preceding script, where we set B10 as the active cell, will simply make B10 the active cell; the position of the picture won\u2019t be any different than it was when the active cell was A1. If you want to specifically set the position of the picture, you can use the <B>AddPicture<\/B> method of the <B>Shapes<\/B> object:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objWorksheet.Shapes.AddPicture &#8220;C:\\Scripts\\Sunset.jpg&#8221;,1,0,0,0,100,200\n<\/PRE>\n<P>The first parameter to AddPicture is (obviously) the path to the image. The second and third parameters have to do with how the picture is embedded. The final four parameters \u2013 in this case 0,0,100,200 \u2013 are the coordinates within the worksheet where the picture will appear and the size of the picture. See the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/Bb209605.aspx\" target=\"_blank\"><B>Excel Developer Reference<\/B><\/A> for more details.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That should do it, GA; let us know if you have any questions. Oh, and be sure to tune in again on June 1, 4092; on that day we\u2019ll let everyone know whether or not nonfat raspberry yogurt really <I>does<\/I> have a shelf life of 2,895 years.<\/P>\n<P>For now, however, we\u2019ve got a doughnut to eat. How long is the shelf life of a doughnut? Let\u2019s put it this way: if the Scripting Guy who writes this column is around, no doughnut is going to last more than a few minutes, max.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a background picture to an Excel worksheet?&#8212; GA Hey, GA. You know, being the health-conscious type, each morning the Scripting Guy who writes this column eats some non-fat raspberry yogurt \u2026 right before he eats his daily doughnut. (But don\u2019t worry: it\u2019s a medically-proven fact that eating yogurt [&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":[710,711,48,49,3],"class_list":["post-64093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add a background picture to an Excel worksheet?&#8212; GA Hey, GA. You know, being the health-conscious type, each morning the Scripting Guy who writes this column eats some non-fat raspberry yogurt \u2026 right before he eats his daily doughnut. (But don\u2019t worry: it\u2019s a medically-proven fact that eating yogurt [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64093","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=64093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64093\/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=64093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}