{"id":54903,"date":"2008-11-13T11:19:00","date_gmt":"2008-11-13T11:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/11\/13\/hey-scripting-guy-how-can-i-count-how-many-images-are-embedded-in-microsoft-word-documents\/"},"modified":"2008-11-13T11:19:00","modified_gmt":"2008-11-13T11:19:00","slug":"hey-scripting-guy-how-can-i-count-how-many-images-are-embedded-in-microsoft-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-count-how-many-images-are-embedded-in-microsoft-word-documents\/","title":{"rendered":"Hey, Scripting Guy! How Can I Count How Many Images Are Embedded in Microsoft Word Documents?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\" \/> <\/h2>\n<p>Hey Scripting Guy! You are not going to believe this. I have just been given the task of counting how many images are embedded in Word documents. The document folder, of course, has hundreds of documents. I am not sure why they want to know how many documents have embedded images, but I think it has something to do with updating a catalog we produce with new images. I know this sounds crazy, but is there anything you can do to help?<\/p>\n<p>&#8211; JZ<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hi JZ,<\/p>\n<p>As they say, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Andre_Agassi\">image is everything<\/a>. This is true whether you are playing tennis, selling cameras, or looking for images. The main problem with this is to figure out which object you need to use. When you have found the appropriate object, the script basically writes itself. Here it&nbsp;is:<\/p>\n<pre class=\"codeSample\">$folder = \"c:\\fso\\*\"\n$include = \"*.doc\",\"*.docx\"\n$word = new-object -comobject word.application\n$word.visible = $false\nGet-ChildItem -path $folder -include $include |\nForEach-Object `\n{\n $doc = $word.documents.open($_.fullname)\n $_.name + \" has \" + $doc.inlineshapes.count + \" images in the file\"\n}\n $word.quit()\n<\/pre>\n<p>When we talk about images in Word documents, we mean those images that are embedded in the document itself. In this image, we see a typical Word document with an embedded image:<\/p>\n<p><img decoding=\"async\" height=\"413\" alt=\"Image of an image embedded in a Word document\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1113\/hsg_img1.jpg\" width=\"450\" border=\"0\" \/><\/p>\n<p>This particular Word document happens to have an image of an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Elasmobranchii\">Elasmobranches<\/a> embedded in it. To be able to identify such Word documents we need to query the InlineShapes property to return an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211988.aspx\">InlineShapes collection<\/a> object. If we look at the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb243492.aspx\">members<\/a> of the InlineShapesCollection object, we find there is a property named count. This is the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jaguar_shark\">jaguar shark<\/a> we have been so diligently searching for. Let&#8217;s dive in. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jaws_%28film%29\">The water is fine<\/a>. (Cue the creepy music.)<\/p>\n<p>We begin the script by assigning a string value to the variable <b>$folder<\/b>. The <b>$folder<\/b> variable will be fed to the <b>Get-ChildItem<\/b> cmdlet, and it is used to govern the folder that will be searched for Word documents. Note that the value of the string has a trailing <a href=\"http:\/\/en.wikipedia.org\/wiki\/Wildcard_character\">wildcard character<\/a> (<b>*<\/b>). This is seen here:<\/p>\n<pre class=\"codeSample\">$folder = \"c:\\fso\\*\"<\/pre>\n<p>Next we define the extensions we will look for. We do this by assigning an array of string values to the <b>$include<\/b> variable. We use <b>*.doc<\/b> to represent legacy Word documents and <b>*.docx<\/b> to represent the Word 2007 format. We will use this variable with the <b>Get-ChildItem<\/b> cmdlet. This line of code is seen here:<\/p>\n<pre class=\"codeSample\">$include = \"*.doc\",\"*.docx\"<\/pre>\n<p>To work with Word we need to create an instance of the <b>Word.Application<\/b> object. This is the main object in the Word automation model, and it is always created when working with Word. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb225979.aspx\">application<\/a> object is very rich and is worth perusing because there is no end to the scripts you can write using its methods and properties. This line of code is seen here:<\/p>\n<pre class=\"codeSample\">$word = new-object -comobject word.application<\/pre>\n<p>We do not need to see the documents as we work our way through the collection, and we set the visible property to <b>$false<\/b>. This is seen here:<\/p>\n<pre class=\"codeSample\">$word.visible = $false<\/pre>\n<p>When you set the visible property to <b>false<\/b>, it is important to remember to use <b>quit<\/b> to exit Word. If you do not, you could end up with multiple copies of Word running on your system.<\/p>\n<p>We pipeline the results of the <b>Get-ChildItem<\/b> cmdlet to the <b>ForEach-Object<\/b> cmdlet. We use <b>Get-ChildItem<\/b> to search the folder we specified in the <b>$folder<\/b> variable for the file types we specified in the <b>$include<\/b> variable. In this example, we are searching the <b>C:\\fso<\/b> folder for all .doc and .docx files. It is possible such a query could return a large result set, and we choose to <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipeline<\/a> the results rather than store them in a variable. This will improve performance as it allows the <b>ForEach-Object<\/b> cmdlet to begin processing as soon as the first document is found. This code is seen here:<\/p>\n<pre class=\"codeSample\">Get-ChildItem -path $folder -include $include |<\/pre>\n<p>We use the <b>ForEach-Object<\/b> cmdlet to allow us to work with the items as they come across the pipeline. To allow us to match up the curly brackets of the code block, we use line continuation via the back tick. This is seen here:<\/p>\n<pre class=\"codeSample\">ForEach-Object `<\/pre>\n<p>We have curly brackets to denote our code block. The code within these curly brackets will be executed once for each item that comes across the pipeline. The first thing we want to do is open the Word document. We use the <b>documents<\/b> property to return the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211902.aspx\">documents collection<\/a> object. The <b>documents<\/b> object is not a very large object, but it does have the <b>open<\/b> method, which is useful if you want to open a Word document. The <b>open<\/b> method has a number of interesting parameters, but today we will content ourselves with passing the only required parameter, which is the file name to open. We need to give it the entire path to the document, so we use the <b>fullname<\/b> property from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.fileinfo(VS.80).aspx\">system.io.fileinfo<\/a> object that was given to us by the <b>Get-ChildItem<\/b> cmdlet. When we have the document open, we want to query the <b>count<\/b> property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211988.aspx\">InlineShapes<\/a> object to find out how many images are in the <b>shape<\/b> collection. We print out the name of the file by using the <b>file<\/b> property. We then concatenate by using the <b>+<\/b> symbol, and use <b>&#8221; &#8220;<\/b> to set off a string. We then concatenate again to print out the count of the images, and then we concatenate yet again the string \u201c images in the file\u201d. This sounds worse than it is. Here is the&nbsp;code:<\/p>\n<pre class=\"codeSample\">{\n $doc = $word.documents.open($_.fullname)\n $_.name + \" has \" + $doc.inlineshapes.count + \" images in the file\"\n}\n<\/pre>\n<p>When we run the script we receive this output:<\/p>\n<p><img decoding=\"async\" height=\"331\" alt=\"Image of the script's output\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1113\/hsg_img2.jpg\" width=\"668\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>We are done. So we need to exit the Word application. To do this, we use the <b>quit<\/b> method seen here.<\/p>\n<pre class=\"codeSample\">$word.quit()<\/pre>\n<p>There is good news, JZ. Because you can save so much time on your new assignment, you will have time to go scuba diving. If you are lucky, you will get to see a shark. If you are really lucky, you can get close enough to him to take a nice picture. It is a wonder the picture above turned out as well as it did, considering how much the camera was shaking. See you tomorrow.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey Scripting Guy! You are not going to believe this. I have just been given the task of counting how many images are embedded in Word documents. The document folder, of course, has hundreds of documents. I am not sure why they want to know how many documents have embedded images, but I think it [&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":[122,84,123,49,3,45,395],"class_list":["post-54903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphics","tag-microsoft-word","tag-multimedia","tag-office","tag-scripting-guy","tag-windows-powershell","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey Scripting Guy! You are not going to believe this. I have just been given the task of counting how many images are embedded in Word documents. The document folder, of course, has hundreds of documents. I am not sure why they want to know how many documents have embedded images, but I think it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54903","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=54903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54903\/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=54903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}