none
Unique ID for a document RRS feed

  • Question

  • Hi;

    I've been using the Document object as a key in a Dictionary to store data I have for each document while editing it. However, I think (not sure) that occasionally the Document object I get on events changes to a different value after awhile.

    Is there some property in the Document object that is gauranteed to be unique and not change for the life of the Document while it is open?

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, April 20, 2011 9:28 PM

Answers

  • I can't see an obvious candidate, but how about creating a document variable called "tempGUID" or some such in each document and setting it to a GUID - you can use code like the following to get one:

     

    Private Type GUID

      Data1 As Long

      Data2 As Long

      Data3 As Long

      Data4(8) As Byte

    End Type

     

    Private Declare Function CoCreateGUID Lib "ole32.dll" ( _

      pguid As GUID _

    ) As Long

     

    Private Declare Function StringFromGUID2 Lib "ole32.dll" ( _

      rguid As Any, _

      ByVal lpstrClsId As Long, _

      ByVal cbMax As Long _

    ) As Long

     

    Public Function getGUID() As String

     

    Const bufSize As Long = 40

    Dim buf() As Byte

    Dim resultLength As Long

    Dim theGUID As GUID

     

    CoCreateGUID theGUID

     

    ReDim buf(0 To (bufSize * 2) - 1) As Byte

    resultLength = StringFromGUID2(theGUID, VarPtr(buf(0)), bufSize) - 1

    getGUID = Left$(buf, resultLength)

     

    End Function


    Peter Jamieson
    • Marked as answer by DavidThi808 Wednesday, April 20, 2011 11:55 PM
    Wednesday, April 20, 2011 10:33 PM

All replies

  • I can't see an obvious candidate, but how about creating a document variable called "tempGUID" or some such in each document and setting it to a GUID - you can use code like the following to get one:

     

    Private Type GUID

      Data1 As Long

      Data2 As Long

      Data3 As Long

      Data4(8) As Byte

    End Type

     

    Private Declare Function CoCreateGUID Lib "ole32.dll" ( _

      pguid As GUID _

    ) As Long

     

    Private Declare Function StringFromGUID2 Lib "ole32.dll" ( _

      rguid As Any, _

      ByVal lpstrClsId As Long, _

      ByVal cbMax As Long _

    ) As Long

     

    Public Function getGUID() As String

     

    Const bufSize As Long = 40

    Dim buf() As Byte

    Dim resultLength As Long

    Dim theGUID As GUID

     

    CoCreateGUID theGUID

     

    ReDim buf(0 To (bufSize * 2) - 1) As Byte

    resultLength = StringFromGUID2(theGUID, VarPtr(buf(0)), bufSize) - 1

    getGUID = Left$(buf, resultLength)

     

    End Function


    Peter Jamieson
    • Marked as answer by DavidThi808 Wednesday, April 20, 2011 11:55 PM
    Wednesday, April 20, 2011 10:33 PM
  • Hi;

    I assume mean Document.Variables. Is that reasonably fast because I need this a LOT.

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, April 20, 2011 11:00 PM
  • > I assume mean Document.Variables.

    Yes.

    > Is that reasonably fast because I need this a LOT.

    It's obviously not going to be as fast as something that is designed to be a fast-access property of the Document object (e.g., it would be nice if the Document object had an ID or a Tag for that purpose. Bt it's probably the next best thing, unless you want to use a built-in or custom document property. Other than that, I can imagine there might be a problem if you add your Document variable to a document that already has a large number of variables, and even then, that might only be a problem if your code was being used on a site that used large numbers of variables in most documents. Probably worth doing some performance tests, just to be sure.

    Peter Jamieson
    Wednesday, April 20, 2011 11:12 PM
  • Hi again;

    Ok, this works and is fast enough but it has one giant problem...

    When a document is opened or created I set the Variable and the document is then marked as dirty (! Saved). So opening a document, the user will then be asked if they want to save it on closing even though they have not done any editing.

    This is a big problem for the blank new document because instead of being replaced by the first document opened, it stays up and a new window is opened.

    It does appear to work ok for READ/ONLY documents - although it shouldn't.

    ??? - dave


    Very funny video - What's your Metaphor?
    Wednesday, April 20, 2011 11:51 PM
  • Never mind - I always forget the Document.Saved can be set. So save and set.

    thanks for the solution - dave


    Very funny video - What's your Metaphor?
    Wednesday, April 20, 2011 11:55 PM
  • Yes, if you need to avoid dirtying the document, it's a problem.

    However, AFAIK if you use a Custom Document Property instead there is an error in Word whereby adding such a property does /not/ dirty the document. I do not know if that is the case for all versions, and would not much like to exploit such a flaw, but there it is...

    I'm not sure whether you can also get around this by setting Document.Saved = True immediately after creating the Document.Variable.

     


    Peter Jamieson
    Wednesday, April 20, 2011 11:59 PM
  • Hello,

     

    You can generate a HashCode from the document path+name. The hash is to be stored in a variable, and updated on DocumentBeforeSave (in case is a document that does not exist physically).

     

    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, April 21, 2011 5:30 AM
  • That won't work - they can rename the document. And BeforeSave occurs before the name of the file is set for the save.
    Very funny video - What's your Metaphor?
    Thursday, April 21, 2011 2:40 PM
  • Hello,


    There is another way. Save the ID in the XML of the document, and save only the XML, like so:

    WordTools.Document vstoDocument = Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();
    vstoDocument.XMLSaveDataOnly = true;
    vstoDocument.Save();
    
    So, after adding the ID, you will save it in the document, and so, you document will be clean if there where not any other changes.

     

    Hope this will help you,

    Silviu.

     

     


    http://www.rosoftlab.net/
    Friday, April 22, 2011 5:01 AM
  • Hi Silviu;

    Thank you but that will not work for us. First, we would have to read the XML every time we need the key - and that happens a lot. Second, I don't think XML mode is available back to Word 2000.

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, April 22, 2011 2:30 PM