none
CustomDocumentProperties not saving with document RRS feed

  • Question

  • All,

    Let me explain my scenario. I have a system that I need to attach Office documents into. To do so, I need to add a CustomDocumentProperty that contains an ID I can key off of later. When a user uploads a document for the first time, I need to create the property, attach it, save the document, close the document, read it into a stream and send to the webservice and reopen the document for them.

    Before saving the document, I run the following function
            private void SetDocumentControlId(Microsoft.Office.Interop.Word.Document Doc, string documentId)
            {
                var props = (DocumentProperties)Doc.CustomDocumentProperties;
                var cc = (from DocumentProperty prop in props
                          where prop.Name == DOCUMENTID
                          select prop).FirstOrDefault();
                if (cc != null)
                    props[(object)CCDOCUMENTID].Delete();
    
                props.Add(DOCUMENTID, false, MsoDocProperties.msoPropertyTypeString, documentId, Type.Missing);
    
            }
    DOCUMENTID is a simple string constant. Doc is the activeDocument.

    I believe (unless I've misread it), that if the property exists, you have to delete it in order to change its value, so that's the middle part of the code.

    After exiting from the function, I can inspect the current document. The property exists and is accessible. I call Doc.Save(), close the document, upload it, then reopen.

    The property is not there.  I *have* managed to see it work, but not consistently and it's never based on changes to that block of code.

    Can anyone point me to why those properties may not be persisting?

    Thanks,
    R
    Monday, December 7, 2009 9:11 PM

Answers

  • Cindy, another solution was discovered which I think works a bit better. One can simply set the Saved property to false. The nice part about this is that it works for any office document, not just Word.

    Just thought I'd throw that out there for anyone who might have the same problem.
    Tuesday, December 8, 2009 8:50 PM
  • Hi Robb

    <<I believe (unless I've misread it), that if the property exists, you have to delete it in order to change its value, so that's the middle part of the code.>>

    No, you don't need to delete the Property object. Just use its Value property to reassign what it contains. So cc.Value = "The new thing" should work.

    Word can be finicky about what it saves, and what it doesn't save. Changing only the value of a document property usually doesn't set the "dirty" flag, so Save won't actually execute, even if you call it. Only if you change something in the document (the "page" you see) will the "dirty" flag be set. Safest is usually to set a Range at the beginnng or end of the document, assign text to it, then DELETE it (don't Undo). Very roughly, to illustrate what I mean:
        Word.Range rng = Document.Content;
        object collapseStart = Word.WdCollapseDirection.wdCollapseStart;
        rng.Collapse(ref collapseStart);
        rng.Text = "Dirty the doc!";
        rng.Delete();
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Robb Allen Tuesday, December 8, 2009 3:49 PM
    Tuesday, December 8, 2009 2:59 PM
    Moderator

All replies

  • Hi Robb

    <<I believe (unless I've misread it), that if the property exists, you have to delete it in order to change its value, so that's the middle part of the code.>>

    No, you don't need to delete the Property object. Just use its Value property to reassign what it contains. So cc.Value = "The new thing" should work.

    Word can be finicky about what it saves, and what it doesn't save. Changing only the value of a document property usually doesn't set the "dirty" flag, so Save won't actually execute, even if you call it. Only if you change something in the document (the "page" you see) will the "dirty" flag be set. Safest is usually to set a Range at the beginnng or end of the document, assign text to it, then DELETE it (don't Undo). Very roughly, to illustrate what I mean:
        Word.Range rng = Document.Content;
        object collapseStart = Word.WdCollapseDirection.wdCollapseStart;
        rng.Collapse(ref collapseStart);
        rng.Text = "Dirty the doc!";
        rng.Delete();
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Robb Allen Tuesday, December 8, 2009 3:49 PM
    Tuesday, December 8, 2009 2:59 PM
    Moderator
  • Thank you Cindy, that seems to have been the issue. I need to run a lot more tests again before I'm comfortable, but initial testing shows that adding a space at the end then deleting it seems to work fine.

    Speaking of, rng.Delete, at least for me, requires two ref objects. I simply passed a ref to Type.Missing and it appears to work.
    Tuesday, December 8, 2009 3:50 PM
  • Hi Robb

    <<Speaking of, rng.Delete, at least for me, requires two ref objects. I simply passed a ref to Type.Missing and it appears to work.>>

    Yes, I was typing from memory - that's why I said "very roughly". The code was just to give you a starting point :-) Usually, I never pass in anything to Delete (most of my "serious" Office coding is still VB-speak). Glad you got it figurd out.
    Cindy Meister, VSTO/Word MVP
    Tuesday, December 8, 2009 4:08 PM
    Moderator
  • Cindy, another solution was discovered which I think works a bit better. One can simply set the Saved property to false. The nice part about this is that it works for any office document, not just Word.

    Just thought I'd throw that out there for anyone who might have the same problem.
    Tuesday, December 8, 2009 8:50 PM
  • i find the answer in your post, thank you very much.
    Wednesday, December 4, 2013 3:00 AM
  • Hi Robb

    <<One can simply set the Saved property to false.>>

    Yes, of course, how silly I didn't remember that! Thanks for the reminder :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, December 7, 2013 12:18 PM
    Moderator
  • this is exactly what i did,and it works well !!!

    Sunday, February 24, 2019 9:21 AM