{"id":67113,"date":"2006-06-14T18:51:00","date_gmt":"2006-06-14T18:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/14\/how-can-i-get-a-list-of-available-metadata-for-microsoft-office-documents\/"},"modified":"2006-06-14T18:51:00","modified_gmt":"2006-06-14T18:51:00","slug":"how-can-i-get-a-list-of-available-metadata-for-microsoft-office-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-available-metadata-for-microsoft-office-documents\/","title":{"rendered":"How Can I Get a List of Available Metadata for Microsoft Office Documents?"},"content":{"rendered":"<p><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\" \/><\/p>\n<p>Hey, Scripting Guy! How can I get a list of available metadata for Microsoft Office documents?<\/p>\n<p>&#8212; JR<\/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, JR. If we understand your question correctly, you&rsquo;d like to know if it&rsquo;s true that July 21, 2006 will mark the 500<sup>th<\/sup><i>Hey, Scripting Guy!<\/i> column; in addition, you&rsquo;d like to know whether we are planning to do anything to mark this auspicious occasion. Well, the answer to both questions is yes.&nbsp;<\/p>\n<p>And, yes, technically you <i>can<\/i> send us an electronic postcard. But that doesn&rsquo;t sound half as much fun as getting a <i>real<\/i> postcard, if you know what we mean.<\/p>\n<p>Anyway, thanks for writing, JR. Hope we were able to help.<\/p>\n<p>Hmmm, now that you mention it, we guess your question <i>did<\/i> say something about metadata, didn&rsquo;t it? Wonder how we could have missed <i>that<\/i>? But, what the heck: we have some time to kill while we wait for the postcards to start rolling in, so let&rsquo;s see if we can show you how to get a list of the available metadata for a Microsoft Office document.<\/p>\n<p>To begin with, we should note that there is actually a separate COM object (Dsofile.exe) that you can <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=9ba6fac6-520b-4a0a-878a-53ec8300c4c2&amp;DisplayLang=en\"><strong>download<\/strong><\/a> and use to get metadata from Microsoft Office documents. The advantage of using Dsofile is that it can retrieve metadata from <i>any<\/i> Microsoft Office document. The disadvantage to Dsofile? You&rsquo;ll obviously have to download and install it on every computer where you might need to run your metadata-retrieving script. Because a lot of people don&rsquo;t like to download and install additional utilities, we won&rsquo;t discuss Dsofile today; if you&rsquo;re interested, take a look at the <i>Tales From the Script<\/i> column <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee692828.aspx#EFAA\"><strong>Dsofile: The Untold Story<\/strong><\/a>.<\/p>\n<p>Instead, what we&rsquo;re going to do today is show you how to get at metadata (also referred to as <i>document properties<\/i>) using plain old Microsoft Office. The nice thing about this, of course, is that there&rsquo;s nothing to download or install: if you&rsquo;ve got Microsoft Office up and running then you&rsquo;ve already got everything you need. The one disadvantage: you&rsquo;ll have to modify your script slightly depending on the type of document you want to examine. But don&rsquo;t worry, we&rsquo;ll explain how to do that. Because you asked a fairly simple question &#8211; How can I get a list of available metadata for Microsoft Office documents? &#8211; let&rsquo;s start off with an equally-simple script, one that retrieves metadata for an Excel spreadsheet (C:\\Scripts\\Test.xls):<\/p>\n<pre class=\"codeSample\">Set objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\n\nFor Each strProperty in objWorkbook.BuiltInDocumentProperties\n    Wscript.Echo strProperty.Name\nNext\n<\/pre>\n<p>Yes, it <i>is<\/i> a very simple little script. We start out by creating an instance of the <strong>Excel.Application<\/strong> object. And you&rsquo;re right: because we&rsquo;re using Excel we&rsquo;ve pretty much limited ourselves to retrieving information about Excel spreadsheets. What if we wanted to retrieve information about Word documents? In that case, we&rsquo;d need to use the <strong>Word.Application<\/strong> object. OK, then how about PowerPoint presentations? That&rsquo;s easy: we&rsquo;d need to use the <strong>PowerPoint.Application<\/strong> object. Microsoft Publisher? That&rsquo;s right: <strong>Publisher.Application<\/strong>. And so on.<\/p>\n<p>After creating the Excel.Application object we use this line of code to make Excel visible onscreen:<\/p>\n<pre class=\"codeSample\">objExcel.Visible = True\n<\/pre>\n<p>Needless to say, we don&rsquo;t need to do this: the script works just fine even if you <i>can&rsquo;t<\/i> see Excel onscreen. We do this primarily to help you visualize what&rsquo;s going on. If you want to run the script without ever seeing the spreadsheet then simply leave out this line of code. Of course, each time you run the script you&rsquo;ll also end up with an invisible copy of Excel running in the background. To combat that, just make sure you call the <strong>Quit<\/strong> method to dismiss Excel when the script finishes. The &ldquo;invisible&rdquo; version of this script looks like this:<\/p>\n<pre class=\"codeSample\">Set objExcel = CreateObject(\"Excel.Application\")\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\n\nFor Each strProperty in objWorkbook.BuiltInDocumentProperties\n    Wscript.Echo strProperty.Name\nNext\n\nobjExcel.Quit\n<\/pre>\n<p>After getting Excel up and running we then use this line of code to open the spreadsheet C:\\Scripts\\Test.xls:<\/p>\n<pre class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\n<\/pre>\n<p>So far so good. Now, how do we get to the metadata? Well, it turns out that Microsoft Office documents store their metadata in a collection named <strong>BuiltInDocumentProperties<\/strong>. To get a list of available metadata for a Microsoft Excel spreadsheet all we have to do is set up a For Each loop to walk through this collection, echoing back the <strong>Name<\/strong> of each item (each piece of metadata) found in the collection:<\/p>\n<pre class=\"codeSample\">For Each strProperty in objWorkbook.BuiltInDocumentProperties\n    Wscript.Echo strProperty.Name\nNext\n<\/pre>\n<p>What does that give us? That gives us something very much like this:<\/p>\n<pre class=\"codeSample\">Title\nSubject\nAuthor\nKeywords\nComments\nTemplate\nLast author\nRevision number\nApplication name\nLast print date\nCreation date\nLast save time\nTotal editing time\nNumber of pages\nNumber of words\nNumber of characters\nSecurity\nCategory\nFormat\nManager\nCompany\nNumber of bytes\nNumber of lines\nNumber of paragraphs\nNumber of slides\nNumber of notes\nNumber of hidden Slides\nNumber of multimedia clips\nHyperlink base\nNumber of characters (with spaces)\n<\/pre>\n<p>There&rsquo;s your available metadata.<\/p>\n<p>But wait: we aren&rsquo;t done yet. After all, that script shows us all the metadata that <i>could<\/i> be configured for Test.xls. But what if we&rsquo;d like to find out which metadata <i>has<\/i> been configured for Test.xls? Can we get at that information? Of course we can; this script returns metadata property names <i>and<\/i> configured values:<\/p>\n<pre class=\"codeSample\">On Error Resume Next\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\n\nFor Each strProperty in objWorkbook.BuiltInDocumentProperties\n    Wscript.Echo strProperty.Name &amp; \" - \" &amp; strProperty.Value\nNext\n<\/pre>\n<p>The only difference between this script and our first script? This line right here, where we echo back not only the item Name but also the item <strong>Value<\/strong>:<\/p>\n<pre class=\"codeSample\">Wscript.Echo strProperty.Name &amp; \" - \" &amp; strProperty.Value\n<\/pre>\n<p>Oh: and we also added <strong>On Error Resume Next<\/strong> to the beginning of the script. That&rsquo;s important: in some cases the script will crash upon encountering a property that has never been configured. To guard against that possibility, just use On Error Resume Next.<\/p>\n<p>The net result will be similar to this, depending on which properties have values assigned to them and which ones do not:<\/p>\n<pre class=\"codeSample\">Title - Metadata Test\nSubject - Excel Test Scripts\nAuthor - Ken Myer\nKeywords - testing, scripts\nComments - This is a sample spreadsheet used for testing purposes.\nTemplate -\nLast author - Ken Myer\nRevision number -\nApplication name - Microsoft Excel\nCreation date - 6\/13\/2006 8:40:17 PM\nLast save time - 6\/13\/2006 9:07:15 AM\nSecurity - 0\nCategory -\nFormat -\nManager -\nCompany - Microsoft Corporation\nHyperlink base -\n<\/pre>\n<p>Ah, we had a feeling you were going to ask that. Yes, you <i>can<\/i> use a script to programmatically set the value of some of these properties. Some properties &#8211; such as the document <strong>Creation date<\/strong> or <strong>Number of characters<\/strong> &#8211; are read-only, for obvious reasons; however, other properties &#8211; such as <strong>Title<\/strong>, <strong>Subject<\/strong>, and <strong>Author<\/strong> &#8211; can be configured using a script similar to this:<\/p>\n<pre class=\"codeSample\">On Error Resume Next\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Book1.xls\")\n\nFor Each strProperty in objWorkbook.BuiltInDocumentProperties\n    If strProperty.Name = \"Title\" Then\n        strProperty.Value = \"Test Title\"\n    End if\nNext\n<\/pre>\n<p>Again, this is fairly simple. After setting up our For Each loop we use this line of code to check each document property to see if the Name of the property is equal to <i>Title<\/i>:<\/p>\n<pre class=\"codeSample\">If strProperty.Name = \"Title\" Then\n<\/pre>\n<p>If it is, then we use this line of code to set the Value of the property to <i>Test Title<\/i>:<\/p>\n<pre class=\"codeSample\">strProperty.Value = \"Test Title\"\n<\/pre>\n<p>Give it a try and see what happens.<\/p>\n<p>Two quick notes. Yes, there might be a slightly faster way to get at the value of an individual property like Title. As usual, though, we went for the approach we thought was easier rather than the approach that might have been a tiny bit faster. Also, as you doubtless know, you can add your own, custom metadata to Microsoft Office documents. Can you get at <i>those<\/i> properties using a script? You bet: just use the <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/Mspauto\/html\/diproCustomDocumentProperties_HV01040610.asp\"><strong>CustomDocumentProperties<\/strong><\/a> collection instead of the BuiltInDocumentProperties collection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of available metadata for Microsoft Office documents? &#8212; JR Hey, JR. If we understand your question correctly, you&rsquo;d like to know if it&rsquo;s true that July 21, 2006 will mark the 500thHey, Scripting Guy! column; in addition, you&rsquo;d like to know whether we are planning to [&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-67113","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 get a list of available metadata for Microsoft Office documents? &#8212; JR Hey, JR. If we understand your question correctly, you&rsquo;d like to know if it&rsquo;s true that July 21, 2006 will mark the 500thHey, Scripting Guy! column; in addition, you&rsquo;d like to know whether we are planning to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67113","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=67113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67113\/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=67113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}