Matching Title metadate to File Name

My question stems from my recent adoption of Sharepoint as a document tracking tool.  The Search feature in Sharepoint wasn't bringing up the results I was expecting and some research leads me to believe that it functions, by default, using document Titles, not File Names.  Many of my documents had titles unrelated to the documents actual purpose due to some issues with Template use, but that has been resolved with one exception.

When I save a word document in Word 2013 after creating it from a template, the Title field in the metadata is blank.

I would like that field to include the file name, or, if it must, the first line of the document.

NOTE: I can manually add the title when I save the file, but I don't want to have to.  It would like it taken by default.
This can be achieved with an event handler in a global template (add-in).

Create a new document and save it as a macro-enabled template (*.dotm) file, temporarily in your Templates folder.

Open the macro editor, and insert a module and a class module. Using the Properties pane, rename the module as AppEvents and rename the class module as ThisApplication.

In the module, paste this code:

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
    Set oAppClass.oApp = Word.Application
End Sub

In the class module, paste this code:

Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    With ActiveDocument
        If .Type <> wdTypeDocument _
           Or .BuiltInDocumentProperties(wdPropertyTitle) = .Name _
              Then Exit Sub
       
        If .Path = "" Then
            .BuiltInDocumentProperties(wdPropertyTitle) = .Range.Sentences(1)
        Else
            .BuiltInDocumentProperties(wdPropertyTitle) = .Name
        End If
       
        .Saved = False ' force a save
    End With
End Sub


Then save the template, and copy it to the folder %appdata%\Microsoft\Word\Startup. Templates stored in that folder are global; they are loaded whenever Word starts up. Restart Word to make the code active.

When you create a new document (and presumably save it one or more times while working on it), the Title property will be empty. When you close the document or shut down Word, the code in the add-in will run. If the Title has already been changed to the file's name, the code will do nothing. If the document has never been saved, the .Path will be empty, so the Title will be set to the first sentence (Word doesn't really know what a "line" is!). If the file has been saved before, but the Title is different from the file name, then the Title will be changed to the file name and the document will be saved again.
_____________________________
https://jay-freedman.info

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 6, 2024 Views 2,730 Applies to: