+ Reply to Thread
Results 1 to 6 of 6

Is there a 'tag'-like attribute anywhere in the word object model

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    286

    Is there a 'tag'-like attribute anywhere in the word object model

    One of the delights of Access controls (and other controls, too) is the availability of a Tag attribute, which allows the programmer to stash away data useful to the programmer, and guaranteed not to be used by Microsoft!

    I'm engaging in a situation where I will, for a short while, have three different documents open (which could, in reality, all be the same document). I'm opening one as HostDoc, one as SourceDoc, and one as TargetDoc. I'm building a test routine to simply display the names of the open documents, and which is the ActiveDocument at the time.

    I have not been able to find Tag as an attribute of the Document object (boo), but would dearly love to be able to print the Documents collection and get what the logical name of the codument is (the name I'm using in the source code. If we call this hypothetical attribute SuperTag, then I'd be able, when I open the document to say

    HostDoc.SuperTag="HostDoc"
    SourceDoc.SuperTag="SourceDoc"
    TargetDoc.SuperTag="TargetDoc"
    That way I don't have to waste coding and debugging time figuring out which I'm working with (some of these documents may be closed and reopened during the course of the program). Does anyone have any ideas how I might accomplish this? One totally off the wall idea would be to have a non-displayed form with three controls, and to stash into the tag of those controls the name of the source document, but (a) that does not respect the actual Documents collection, and (b) returns the document name (which I can already get to), and not the path to the document (the name of the document in the code module.

    Thanks, in advance, for any suggestions (including "Give Up!")

    Tony

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Is there a 'tag'-like attribute anywhere in the word object model

    Custom document properties?
    Last edited by shg; 04-07-2016 at 04:31 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    286

    Re: Is there a 'tag'-like attribute anywhere in the word object model

    I encountered these while browsing a document a long time ago and in what felt like another galaxy. However, I gave it a look, and indeed they are present. So....

    I set up a macro recording session to capture the steps necessary.


    NOTHING

    OK, let's take a look at the object model for Document. Sure enough there is CustomDocumentProperties (nothing about non-custom DocumentProperties). No type given, just Object. Really helpful. However, in the model (using F2) there are entries for CustomProperties, Custom Property, DocumentProperties and DocumentProperty.

    OK, not too many possibilities, let's throw some code at it:

    Sub TestPrivateProperties()
    
    Dim PrivateProperties As Object
    Dim PPCount As Long
    Dim PPInstance As Object
    
    Dim PPName As String
    Dim PPValue As String
    
    Set PrivateProperties = ActiveDocument.CustomDocumentProperties
    PPCount = PrivateProperties.Count
    Set PPInstance = PrivateProperties.Item(PPCount)
    PPName = PPInstance.Name
    PPValue = PPInstance.Value
    
    End Sub
    It ran - and found the single custom property I had created manually (while trying to record the macro). OK, Let's redeclare PPInstance as a CustomProperty:

    Sub TestPrivateProperties()
    
    Dim PrivateProperties As Object
    Dim PPCount As Long
    Dim PPInstance As DocumentProperty
    Dim TIInstance As DocumentProperty
    
    Dim PPName As String
    Dim PPValue As String
    
    Set PrivateProperties = ActiveDocument.CustomDocumentProperties
    PPCount = PrivateProperties.Count
    Set PPInstance = PrivateProperties.Item(PPCount)
    PPName = PPInstance.Name
    PPValue = PPInstance.Value
    
    End Sub
    OK - an entry in CustomDocumentProperties does indeed seem to be a DocumentProperty. Let's see if we can change the values:

    Sub TestPrivateProperties()
    
    Dim PrivateProperties As Object
    Dim PPCount As Long
    Dim PPInstance As DocumentProperty
    Dim TIInstance As DocumentProperty
    
    Dim PPName As String
    Dim PPValue As String
    Dim PPType As Long
    
    Dim TIName As String
    Dim TIValue As String
    Dim TIType As Long
    
    Set PrivateProperties = ActiveDocument.CustomDocumentProperties
    PPCount = PrivateProperties.Count
    Set PPInstance = PrivateProperties.Item(PPCount)
    PPName = PPInstance.Name
    PPValue = PPInstance.Value
    PPType = PPInstance.Type
    
    If PPValue = "New Value" Then
       PPInstance.Value = "Modified value"
    Else
       PPInstance.Value = "New value"
    End If
    
    
    End Sub
    Glory be! It works. Only problem is that I have to create the CustomProperty manually. I wonder if I can add an entry.
    (Short cut)
    Browsing the MSDN site for info on this, I got:
    
    Function Add ( _
    	Name As String, _
    	LinkToContent As Boolean, _
    	Type As Object, _
    	Value As Object, _
    	LinkSource As Object _
    ) As DocumentProperty
    'Usage
    Dim instance As DocumentProperties
    Dim Name As String
    Dim LinkToContent As Boolean
    Dim Type As Object
    Dim Value As Object
    Dim LinkSource As Object
    Dim returnValue As DocumentProperty
    
    returnValue = instance.Add(Name, LinkToContent, _
    	Type, Value, LinkSource)
    Hmm, I read the Type value as an integer, but it wants an Object? Turns out there are objects to specify the type (a simnple constant would be fartoo easy, I guess.
    The description under the example in MSDN says msoPropertyTypeString should work.

    Sub TestPrivateProperties()
    
    Dim PrivateProperties As Object
    Dim PPCount As Long
    Dim PPInstance As DocumentProperty
    Dim TIInstance As DocumentProperty
    
    Dim PPName As String
    Dim PPValue As String
    Dim PPType As Long
    
    Dim TIName As String
    Dim TIValue As String
    Dim TIType As Long
    
    Set PrivateProperties = ActiveDocument.CustomDocumentProperties
    PPCount = PrivateProperties.Count
    Set PPInstance = PrivateProperties.Item(PPCount)
    PPName = PPInstance.Name
    PPValue = PPInstance.Value
    PPType = PPInstance.Type
    
    If PPValue = "New Value" Then
       PPInstance.Value = "Modified value"
    Else
       PPInstance.Value = "New value"
    End If
    
    
    TIName = "NewProperty"
    TIValue = "New Value"
    
    '   Try to createa new  DocumentProperty
    On Error Resume Next
    Set TIInstance = PrivateProperties(PPCount + 1)
    
    If TIInstance Is Nothing Then
       On Error GoTo 0
       Set TIInstance = ActiveDocument.CustomDocumentProperties.Add(Name:=TIName, _
                             LinkToContent:=False, _
                             Type:=msoPropertyTypeString, _
                             Value:=TIValue, _
                             LinkSource:=Nothing)
       PPCount = PrivateProperties.Count
       Set TIInstance = PrivateProperties(PPCount)
       With TIInstance
           .Name = TIName
           .Type = PPInstance.Type
           .Value = TIValue
       End With
    Else
       With PrivateProperties(PPCount)
           .Value = "Changed using only VBA"
       End With
    End If
    
    End Sub
    The Add fails with an automation error (long negative number signifying nothing).

    I'm stumped - perhaps it's time for an adult beverage. If anyone has any ideas, or has done this before, some code examples would be great......

    Tony

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Is there a 'tag'-like attribute anywhere in the word object model

    Macropod will be along by and by. In the meanwhile, here's some code I use to set custom ducument properties in a proposal template:

    Private Sub btnSetWordProps_Click()
        ' assigns custom document properties to a selected doc
        ' and saves one directory up from ThisWorkbook
        ' with the correct name
        Dim appWd       As Word.Application
        Dim oDoc        As Word.Document
        Dim sPath       As String
        Dim sFile       As String
    
        With ThisWorkbook
            sPath = Left(.Path, InStrRev(.Path, "\"))
        End With
    
        Set appWd = New Word.Application
        With appWd
            .Visible = True
            .ChangeFileOpenDirectory sPath
            If .Dialogs(wdDialogFileOpen).Show = 0 Then
                .Quit
                Exit Sub
            End If
            Set oDoc = .ActiveDocument
        End With
    
        Do While oDoc.CustomDocumentProperties.Count
            oDoc.CustomDocumentProperties(1).Delete
        Loop
    
        With oDoc.CustomDocumentProperties
            .Add Name:="0PropNo", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrPropName").Text
            .Add Name:="0Program", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrPgmLong").Text
            .Add Name:="0ProgramShort", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrPgmShort").Text
            .Add Name:="0Buyer", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrBuyerLong").Text
            .Add Name:="0BuyerShort", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrBuyerShort").Text
            .Add Name:="0Scope", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=Me.Range("ptrScopeLong").Text
            .Add Name:="0SubmDate", LinkToContent:=False, Type:=msoPropertyTypeDate, Value:=Me.Range("ptrSubmDate").Value
        End With
    
        oDoc.Fields.Update
    
        sFile = sPath & _
                Me.Range("ptrPropName").Text & _
                Format(Me.Range("ptrSubmDate").Value, " yyyy-mmdd") & _
                " - Proposal"
        oDoc.SaveAs Filename:=sFile, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=True
        oDoc.Close
        appWd.Quit
        Beep
    End Sub
    Last edited by shg; 04-07-2016 at 06:42 PM.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Is there a 'tag'-like attribute anywhere in the word object model

    For code to add & update custom document properties, see: https://support.microsoft.com/en-us/kb/212618
    Word also has document variables that can be used in much the same way.

    The essential difference is that custom document properties are UI-accessible whereas document variables are not.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    286

    Re: Is there a 'tag'-like attribute anywhere in the word object model

    Thanks, shgand Paul,

    I'll explore this and get back to you.

    Tony

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Excel's Object Model
    By nygwnj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2014, 07:08 AM
  2. ==> HOW find specif word on cells and attribute a logic value ??
    By WILKER in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2012, 11:06 PM
  3. Guidance on which Object Model to use
    By SMB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2011, 03:19 AM
  4. Unpublished Object Model Code
    By Jim JBC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2008, 09:25 PM
  5. Dynamic Object Attribute
    By gabch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2006, 01:35 PM
  6. Confusion about how the Window object fits into the Excel object model
    By Josh Sale in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-18-2005, 10:06 AM
  7. Excel 2002 Object Model
    By Hiep in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-24-2005, 09:06 PM
  8. [SOLVED] Chart Object Model
    By Joe Maki in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1