none
Accessing a document's Title and Subject values... RRS feed

  • Question

  • I would really appreciate some help here. I am desperately trying to find a way to cut the Subject and Title fields that are displaying in my documents. They qwere originally inserted using the Properties dialog.

    From what I can see they are not standard fields, ALT+F9 does not show any field code for Subject and Title. Word Help refers to these as Preset Properties, but even so, I cannot find any details on actually acessing them programatically.

    I have tried to return field values by using ranges of indexes, but I am getting Count of 0 fields on the field collections.

    Please, where are these in the object model and how do I access them? All I want to do is cut them from the documents, preferably by name and not index (if they even have one).

    Thanks
    Robin

    Monday, October 6, 2014 9:38 PM

Answers

  • Something like this:

    Sub GetSetDocProps()
          If Documents.Count > 0 Then
             Dim dp As Object
             Set dp = ActiveDocument.BuiltInDocumentProperties
             ' Retrieve and display the active document's
             ' title by using the constant "wdPropertyTitle."
             MsgBox dp(wdPropertyTitle)
          End If
       End Sub
    

    See for all the properites below article:

    Sample VBA Code to Set, Retrieve Summary Information

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by rkiskan Monday, October 6, 2014 10:14 PM
    Monday, October 6, 2014 9:51 PM

All replies

  • Something like this:

    Sub GetSetDocProps()
          If Documents.Count > 0 Then
             Dim dp As Object
             Set dp = ActiveDocument.BuiltInDocumentProperties
             ' Retrieve and display the active document's
             ' title by using the constant "wdPropertyTitle."
             MsgBox dp(wdPropertyTitle)
          End If
       End Sub
    

    See for all the properites below article:

    Sample VBA Code to Set, Retrieve Summary Information

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by rkiskan Monday, October 6, 2014 10:14 PM
    Monday, October 6, 2014 9:51 PM
  • Thanks Daniel,

    That worked wonders. Interesting is that I previously ran a count on the BuiltInProperties and it gave 34. I then deleted everything expect these two fields, and it gave a count of 0? This told me that they weren't built in, but your script sure does work.

    Is there a method to remove this field so that it does not display in the document?

    thanks

    Robin

    Monday, October 6, 2014 10:14 PM
  • You can't remove the field, but you can set the field to "Blank" if that's what you mean.

    You can find the code in the same document:

       Sub SetSummaryInfo ()
          Dim dp As Object
          Dim sTitle As String
          If Documents.Count > 0 Then
             Set dp = Dialogs(wdDialogFileSummaryInfo)
             ' Retrieve value of "Title" into a variable.
             sTitle  = dp.Title
             ' Set "Title" to a new value.
       '---->' NOTE: I set the Title to BLANK   <----
             dp.Title = ""
             ' Set the value without showing the dialog.
             dp.Execute
             ' Save the changes
             ActiveDocument.Save
          End If
       End Sub
    

    Hope this helps,

    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    Monday, October 6, 2014 10:18 PM
  • OK. What I anm trying to do is use the field value in a text box as opposed to having it on the cover page as a field.

    By using it in a text box I can then move it with other fields as necessary when the corporate style changes (as it regularly does). So the field name and field value must still be in the document and must still be available, but they must only display in the text box.

    thanks

    Robin

    Monday, October 6, 2014 10:29 PM
  • Hi Robin,

    What kind of Text Box are you thinking of placing the Document Property?


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    Tuesday, October 7, 2014 3:27 AM
  • Hi Daniel,

    The text box I am using is a Simple Text Box that is available in the Building Blocks. What I am doing is creating a text box and placing the Subject and Title properties as fields in that text box. I save this as a Building Block that I can add to the document and position as necessary. This seems to work fine.

    The problem I have with setting the property value to a null value is that the property still appears on the document's cover page - even though it is null in value. I see a grey frame Title and content [TITLE] and another for Subject.

    As soon as I assign the fields in the text box a value, these fields that were NULL also get a value, so they are appearing twice - once in the text box and once on the cover page. I do not want the cover page fields (those not in the text box) to show at all.

    When working in the document I can manually delete these property fields from the cover page, so I would expect that this could be done programatically. If they could be cut then that would also be OK, but maybe I have misunderstood something here? 

    thanks

    Robin  

    Tuesday, October 7, 2014 5:26 AM
  • From your description, what you have inserted are content controls connected to the builtin properties in the XML data store.

    The difference between the content control version and the old "field" versions of built-in properties is that when you modify the value of the content control version, the change is propagated automatically to the XML data store and to any other content controls that point to the same property.

    If you iterate the ActiveDocument.ContentControls collection you should see these controls. If you look at the value of the .XMLMapping.XPath property of one of the controls, you should see how they are connected to the XML data store.

    You can create this kind of content control by creating a plain text content control then using its XMLMapping.Setmapping method.


    Peter Jamieson

    Tuesday, October 7, 2014 3:39 PM
  • Hi Peter,

    Thanks for the feedback, but this goes way beyond my Word knowledge. I just find it somewhat inconsistent that the Insert function in Word allows you to place the same content in two different ways:

    Insert > Quick Part > Field and
    Insert > Quick Part > Document Property.

    From a standard user point of view there is nothing that appears limiting in either appropach, which is why were not aware of the complication.

    At the end of the day I still do not want the Title and Subject controls to appear in my document, though their values must still be part of the document object. I just want these values to be hidden from the user.

    Robin

    Tuesday, October 7, 2014 5:55 PM
  • Word has a long history now and does have a number of ways to do these things. I don't have relevant code to hand, so all I can really do is spell out what you are likely to find (and how you might get rid of it) and also what you might end up having to look for...

    If you need to delete standard content controls, which is what I think you were first asking about, you can either iterate through the controls and delete them all, or iterate through the controls and try to identify the ones that are connected to the properties you identified (Title and Author).

    Deleting the controls does not change the value of the underlying property. You may need to iterate through multiple storyranges in the document to remove them.

    If the documents have been created using standard templates or using the list of properties in the quick parts menu, in an English language version of Word the content controls for Author and Title should have their title property set to "Author" and "Title" respectively, and no other controls should have these titles.

    However, because these things can be changed (e.g. the Author property's title could be changed to "Writer" or some such, and a propety that isn't the Author could be titled "Author", it is probably better to look at each content control's .XMLMapping.IsMapped, property, and if mapped, look at the .XPath. These should be language-independent and they would typically always be the same, e.g. for Author the XML would be

    /ns1:coreProperties[1]/ns0:creator[1]

    and for Title it would be
    /ns1:coreProperties[1]/ns0:title[1]

    Once again, because you can express the XPath to these property values in different ways, matching that XPath exactly is also potentially unreliable. Off the top of my head, I am not sure what a completely reliable way to do that match might be, but stripping out any namespace names and element IDs and matching with /coreProperties/creator would catch a lot of variations.

    If the properties might also have been inserted using field codes, then you would also have to iterate through those - in practice, AUTHOR is usually inserted using an { AUTHOR } field, and TITLE via a { TITLE } field. But they could in theory be inserted using an { INFO AUTHOR } or { INFO TITLE } field. IN modern versions of Word, the English language field names are used regardless of stuff such as regional settings etc. You might even find a { DOCPROPERTY AUTHOR } or { DOCPROPERTY TITLE } field. As usual, there is a further complication in that if there is actually a custom document property called Author or Title, the DOCPROPERTY field will insert the value of the Custom property, not the Builtin property.


    Peter Jamieson

    Wednesday, October 8, 2014 9:50 AM