{"id":12199,"date":"2014-06-03T15:58:15","date_gmt":"2014-06-03T15:58:15","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/wp\/?p=3774"},"modified":"2018-03-21T21:08:56","modified_gmt":"2018-03-21T21:08:56","slug":"read-write-document-properties-excel-macro","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2014\/06\/read-write-document-properties-excel-macro\/","title":{"rendered":"Read and Write Document Properties of an excel Document – VBA"},"content":{"rendered":"
In this Article we will learn how to get document property of an excel workbook using Excel VBA. Before I jump in to the VBA code let’s have a look Where to see document properties in Excel Document? <\/strong><\/p>\n Follow the below steps to see the properties for the document:<\/p>\n Step 1.<\/strong> Click on Office Button in Excel 2007 \nNow you know how to read and write the properties of an excel document. Now I will show you how to do the same activity using VBA code. Using Workbook.BuiltinDocumentProperties<\/strong> property user can read and write the document property of an excel workbook.\n<\/p><\/blockquote>\n This is a Microsoft Excel Workbook property which allows users to read and write the document property. This returns a document properties object which has a collection of all the properties of the document. <\/p>\n Use the below vba syntax to get the document property of the excel document:<\/p>\n prop1 = ThisWorkbook.BuiltinDocumentProperties(“<Prop1Name>”).value<\/i><\/p>\n Prop1 :<\/strong> is a variable where you want to store the value of a property Name of the item where Author Name property value is stored is author<\/strong>. Therefore to get the author name from the document property vba code will look like this:<\/p>\n If you want to display all the properties of an Excel Workbook you can use For Loop<\/i> to traverse all the items of the BuiltinDocumentProperties collection. Below is the VBA code which will list all the proprties Name and corresponding values in your excel sheet.<\/p>\n This is very simple. All you need to do is reverse the operation. Now you need to assign a value to the property. Hence the VBA code synatx will look like this:<\/p>\n ThisWorkbook.BuiltinDocumentProperties(“<Prop1Name>”).value = prop1<\/i><\/p>\n Prop1 :<\/strong> is the value of a property (Like Author Name = Vishwa) Name of the item where Author Name property value is stored is author<\/strong>. Therefore to update the author name in the document property vba code will look like this:<\/p>\n After running the above code all the values will be set to the corresponding property field in document property. refer the below picture: <\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" In this Article we will learn how to get document property of an excel workbook using Excel VBA. Before I jump in to the VBA code let’s have a look Where to see document properties in Excel Document? How to see document property of an Excel document Follow the below steps to see the properties […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1673,1246,1676,1678,1682],"tags":[],"class_list":["post-12199","post","type-post","status-publish","format-standard","hentry","category-excel-functions","category-macro","category-excel-tips","category-interesting-vba-functions","category-popular-articles"],"yoast_head":"\nHow to see document property of an Excel document<\/h2>\n
\nStep 2.<\/strong> Roll over Prepare Option<\/strong> available as shown in the below picture:
\nStep 3.<\/strong> Now click on Properties Option visible in Right hand side
\n
\nStep 4.<\/strong> Now you can see the document property below the Ribbon as shown in the below picture:
\n
\n
\nHere you can read the document property which is already updated. You can write the property here and save it. It means you can read and write a document property from the above screen.<\/p>\nWhat is Workbook.BuiltinDocumentProperties ?<\/i><\/h3>\n
1. How to Read Document Property using Workbook.BuiltinDocumentProperties<\/i><\/h3>\n
Syntax<\/h3>\n
Where:<\/h3>\n
\nProp1Name :<\/strong> This is the Name of the property by which it is referred in the returned collection of properties<\/p>\nExample 1: Get the Author Name from the document property<\/h2>\n
\r\nFunction Get_Author_Name()\r\n Dim AuthorName As String\r\n AuthorName = ThisWorkbook.BuiltinDocumentProperties(\"author\").Value\r\nEnd Function\r\n<\/code><\/pre>\n
Example 2: Few more frequently used properties<\/h2>\n
\r\nFunction Get_Document_Properties()\r\n Dim LastAuthorName\r\n Dim CreatedOn\r\n Dim LastSavedOn\r\n Dim Title\r\n Dim Comments\r\n 'To retrieve the last Author of the File\r\n LastAuthorName = ThisWorkbook.BuiltinDocumentProperties(\"last author\").Value\r\n 'To get the created on date and time\r\n CreatedOn = ThisWorkbook.BuiltinDocumentProperties(\"creation date\").Value\r\n 'to get the date and time when last time document was saved\r\n LastSavedOn = ThisWorkbook.BuiltinDocumentProperties(\"last save time\").Value\r\n 'to get the title of document updated in document property\r\n Title = ThisWorkbook.BuiltinDocumentProperties(\"title\").Value\r\n 'to get the Author's comment added in document proprty\r\n Comments = ThisWorkbook.BuiltinDocumentProperties(\"title\").Value\r\nEnd Function\r\n<\/code><\/pre>\n
Example 3: VBA code to display all the Properties Name its values<\/h2>\n
\r\nFunction list_All_Properties()\r\nDim iRow As Integer\r\niRow = 1\r\nOn Error Resume Next\r\n'below loop will traverse for all items of this collection of properties\r\nFor Each prop In ThisWorkbook.BuiltinDocumentProperties\r\n Range(\"A\" & i).Value = prop.Name 'Property Name like \"Author\" etc\r\n Range(\"B\" & i).Value = prop.Value 'Property value like Author name updated in proprty\r\n iRow = iRow + 1\r\nNext\r\nEnd Function\r\n<\/code><\/pre>\n
2. How to Write Document Property using Workbook.BuiltinDocumentProperties<\/i><\/h3>\n
Syntax<\/h3>\n
Where:<\/h3>\n
\nProp1Name :<\/strong> This is the Name of the property which you want to update with the above value<\/p>\nExample 1: Set the Author Name in document property using VBA<\/h2>\n
\r\nFunction Update_Author_Name()\r\n Dim AuthorName As String\r\n AuthorName = \"Vishwa\"\r\n ThisWorkbook.BuiltinDocumentProperties(\"author\").Value = AuthorName\r\nEnd Function\r\n<\/code><\/pre>\n
Example 2: Few more frequently used properties<\/h2>\n
\r\nFunction Update_Document_Properties()\r\n Dim Subject\r\n Dim Status\r\n Dim Category\r\n Dim Title\r\n Dim Comments\r\n 'Assign property value to the variables\r\n Subject = \"Subject of the Document\"\r\n Title = \"My Title\"\r\n Comments = \"Author's comment\"\r\n Status = \"Complete\"\r\n Category = \"Finance\"\r\n 'To update the title of document in document property\r\n ThisWorkbook.BuiltinDocumentProperties(\"title\").Value = Title\r\n 'to update the Author's comment in document proprty\r\n ThisWorkbook.BuiltinDocumentProperties(\"comments\").Value = Comments\r\n 'to update Subject in document proprty\r\n ThisWorkbook.BuiltinDocumentProperties(\"subject\").Value = Subject\r\n 'to update the status in document proprty\r\n ThisWorkbook.BuiltinDocumentProperties(\"content status\").Value = Status\r\n 'to update the Category in document proprty\r\n ThisWorkbook.BuiltinDocumentProperties(\"category\").Value = Category\r\nEnd Function\r\n<\/code><\/pre>\n
\n
\n <\/p>\nIf you have any doubt or suggestion do let me know through comment or on my facebook fan page<\/a> <\/h2>\n