#1
|
|||
|
|||
Changing custom properties in multiple word documents
Hello, I am trying to see a way where I can have the custom properties defined and then have a macro to change other multiple word documents. These custom properties are the same in all word documents, and I think its a bit much to go into each word document and change the properties. I am hoping there is a better solution to this. I will attach the custom properties that are in the word document. Hoping you could help me create a macro where all these custom properties can be updated once for multiple word documents. Thanks so much for your help. |
#2
|
||||
|
||||
The following macro function when used in conjunction with http://www.gmayor.com/document_batch_processes.htm as a custom process will add the custom docproperty 'Department' to either the current document or a folder full of documents (including sub folders if you wish). If the property is already present it will be updated with the value entered, which from your illustrations appears to be 'Money'
You can change the Constants to reflect any custom property name, value or type Code:
Option Explicit Function DocProp(oDoc As Document) As Boolean Dim oProp As DocumentProperty Dim bProp As Boolean Const strName As String = "Department" Const strValue As String = "Money" Const lngType As Long = 4 'Text On Error GoTo err_Handler bProp = False For Each oProp In oDoc.CustomDocumentProperties If oProp.name = strName Then oProp.Value = strValue bProp = True Exit For End If Next oProp If Not bProp Then oDoc.CustomDocumentProperties.Add _ name:=strName, _ LinkToContent:=False, _ Value:=strValue, _ Type:=lngType End If DocProp = True lbl_Exit: Exit Function err_Handler: DocProp = False Resume lbl_Exit End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
I don't think the code does exactly what I am looking for. What I am looking for is a way to update multiple documents with the same properties all at once. Lets say I update one document, but have 4 other documents that I have reviewed, but do not want to individually update one by one. Is there a way where all 4 documents can be updated at the same time?
I was hoping there was a code that when entered, changes the custom properties, like title, author, company, and other custom properties that I have defined in the attachment. For example, date completed, version number, approved by, department, and classification. |
#4
|
||||
|
||||
There is no way to link the custom document properties in one document to custom document properties in another. If all the documents are in the same folder (and there are no other documents in that folder), Graham's code can be used to propagate the custom document properties that are input into the macro to all of those documents. If you need to replicate the custom document properties in the activedocument to just a selection of other documents, that can be done via hard-coding those documents' names into a macro or by providing a dialogue box one can use to select the documents. You need to specify precisely what your requirements are.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Paul, unless Graham has changed his code, his works the same as mine. "(...and there are not other documents in that folder") shouldn't matter. The function will simply error and the return a false value for any document in the folder that fails to process.
|
#6
|
||||
|
||||
Hi Greg,
Graham's code in post #2 above adds the properties to any documents that don't have them so, in that sense, it does matter. Graham says as much. That wasn't the main thrust of my post, though, which was to address the OP's desire to dynamically update the properties in one or more related documents when the properties in one of them is changed. Word provides no means of doing this, though there are ways of doing it automatically (e.g. via a Document_Close macro), provided certain conditions are met.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
This is an old thread, but a user has reported that the macro I posted doesn't actually work, at least not with the latest versions of my (or Greg's) add-ins. The reason appears to be that simply changing the document property with the posted code is not seen by the document as something that needs to be saved, and so it isn't. So to use it you need to change the code to flag it as unsaved as shown below.
Code:
Option Explicit Function DocProp(oDoc As Document) As Boolean Dim oProp As DocumentProperty Dim bProp As Boolean Const strName As String = "Department" Const strValue As String = "Money" Const lngType As Long = 4 'Text On Error GoTo err_Handler bProp = False For Each oProp In oDoc.CustomDocumentProperties If oProp.Name = strName Then oProp.value = strValue bProp = True Exit For End If Next oProp If Not bProp Then oDoc.CustomDocumentProperties.Add _ Name:=strName, _ LinkToContent:=False, _ value:=strValue, _ Type:=lngType End If For Each oProp In oDoc.CustomDocumentProperties If oProp.Name = strName Then oProp.value = strValue Exit For End If Next oProp oDoc.Saved = False DocProp = True lbl_Exit: Exit Function err_Handler: DocProp = False Resume lbl_Exit End Function https://www.gmayor.com/document_batch_processes.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com Last edited by gmayor; 06-23-2020 at 12:21 AM. |
#8
|
|||
|
|||
Can you please share the macro?
Quote:
Hello wwballar42, I have the same issue. A lot of documents all of them with the same properties, and I would like to mass update the properties for all of them. What I'm thinking is to create an Excel macro to first list all the custom properties from all the document which in a folder and sub-folders, after all the custom properties are displayed in the Excel Sheet, modify them and mass update. Currently what I have is a Macro to read all files and list in column 1 the file path, and in column 2, the file name. I'm currently not able to get the custom properties . Here is the Excel VBA code mentioned above, in case someone knows how to do it. Thanks in advance for your help. HTML Code:
Sub getfiles() 'Get the files name from Folder and Subfolder Dim oFSO As Object Dim oFolder As Object Dim oFile As Object, sf Dim i As Integer, colFolders As New Collection, ws As Worksheet Dim selectedFolder As Variant Dim fd As FileDialog Set ws = ActiveSheet Set oFSO = CreateObject("Scripting.FileSystemObject") 'Prompt user to select a folder Set fd = Application.FileDialog(msoFileDialogFolderPicker) With fd .Title = "Select Folder" .AllowMultiSelect = False If .Show = -1 Then selectedFolder = .SelectedItems(1) Else MsgBox "No folder selected. Operation aborted." Exit Sub End If End With Set oFolder = oFSO.getfolder(selectedFolder) colFolders.Add oFolder 'start with this folder Do While colFolders.Count > 0 'process all folders Set oFolder = colFolders(1) 'get a folder to process colFolders.Remove 1 'remove item at index 1 For Each oFile In oFolder.Files If oFile.DateLastModified > Now - 7 Then ws.Cells(i + 1, 1) = oFolder.Path ws.Cells(i + 1, 2) = oFile.Name 'ws.Cells(i + 1, 3) = oFile.DateLastModified i = i + 1 End If Next oFile 'add any subfolders to the collection for processing For Each sf In oFolder.subfolders colFolders.Add sf Next sf Loop End Sub |
#9
|
||||
|
||||
You probably need to open each doc to get at the custom properties.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Hello all, first of all the reason why I cannot see "all" the document properties, is because the missing properties were created via SharePoint. The document gets created then uploaded in Sharepoint, I think those attributes/properties (in ShapePoint) are stored probably in a XML file embedded in the document. Don't know how to extracted, to later list them via VBA.
Any ideas?. Thanks. |
#11
|
||||
|
||||
You are correct in thinking that Sharepoint adds custom XML to a document for attributes/properties configurable via the Sharepoint properties. That custom XML can be extracted in entirety (each of the custom XML files) or you can drill into those files to find a particular xml element value and extract that. It isn't a simple task to extract a spreadsheet summarising document properties because the number of custom xml files will likely vary from doc to doc and there could be somewhere between 2 and 10 of them - each one containing a variable number of nested xml elements.
In my experience, Sharepoint adds more than one xml file and the namespace and elements will most likely vary depending on how your Sharepoint library is configured. If you have a task of updating a particular Sharepoint property on a large number of files held in a Sharepoint library, the easiest way would be to create a Table View in that library which includes that property and then use copy/paste to fill that property's column. The alternative to this is much more time consuming: 1. Download all the files to your hard drive 2. Work out which custom xml file contains the field you want to update - you need its namespace and the xml element's unique name. 3. Create a macro to open each of the documents, update that xml element's value, save & close. 4. Upload all the docs back into Sharepoint.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#12
|
|||
|
|||
Hello Guessed,
First of all, thanks for take time to answer my question. The alternative mentioned by you, is the one that I'm currently using. When it come to the file status update it is fine because it can be "black or white", but in many cases I also need to update the file name, which change from file to file, that's why I was thinking if I can extract it to an Excel, there do the changes and then re-upload the file back again, but I guess it is too complex. In any case, thanks for your time. |
#13
|
||||
|
||||
If it is a file name change then I would think that it probably needs to be done by hand in Sharepoint. Otherwise you would lose the file version history and have to delete the original named file when you reload the newly named file back into Sharepoint.
Thought: We can view Sharepoint sites in Windows File Explorer and rename the files there. It might be possible to automate that via VBA.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Add custom doc properties in "Doc properties control" | eazysnatch | Word | 2 | 09-13-2017 08:08 PM |
Searching with Custom Properties | jpb103 | Word VBA | 6 | 05-30-2014 07:08 AM |
Changing Default Author on New Word 2013 Documents | chaplaindoug | Word | 1 | 01-09-2014 12:00 PM |
Custom Properties | b-baker | Word | 1 | 03-01-2012 01:15 AM |
Looping though Custom Properties in VBA | suekay | Misc | 0 | 05-19-2006 06:10 AM |