none
Word.Fields.Locked value out of range RRS feed

  • Question

  • I'm trying to write a C# console program in Visual Studio Express 2013 that will lock all fields in a given document from being able to update, like pressing Ctrl+F11 does in Word itself. I thought that Microsoft.Office.Interop.Word.Fields.Locked was the property I needed to change, so I've selected the entire document into a range object, then try to set it's Fields.Locked to 1 (since Locked is an int, instead of a bool). When I do this I get an error stating "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in <programName>.exe  Additional information: Value out of range". When I set the value of Locked to 0, I don't get the error, but fields don't lock either. What am I doing wrong? I can post code if anybody needs to see it.
    Monday, December 16, 2013 6:08 PM

Answers

  • Try -1 (negative 1). In the old WordBasic language "True" is set to -1 not 1, as in the modern languages. And the Fields stuff is old enough that it definitely bases on WordBasic.

    Just for the sake of completeness, note you could also accomplish your task using the Open XML SDK. This would work with the Word document independently of the Word application, meaning no messages, no installation, no license requirements, etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by mhokanson Tuesday, December 17, 2013 3:51 PM
    Tuesday, December 17, 2013 2:21 PM
    Moderator

All replies

  • Try -1 (negative 1). In the old WordBasic language "True" is set to -1 not 1, as in the modern languages. And the Fields stuff is old enough that it definitely bases on WordBasic.

    Just for the sake of completeness, note you could also accomplish your task using the Open XML SDK. This would work with the Word document independently of the Word application, meaning no messages, no installation, no license requirements, etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by mhokanson Tuesday, December 17, 2013 3:51 PM
    Tuesday, December 17, 2013 2:21 PM
    Moderator
  • Thanks. I actually found this was the answer late yesterday afternoon when I was frustrated enough to start throwing random integers at this property. I'm glad you explained why someone would have used -1 instead of 1.
    Tuesday, December 17, 2013 3:51 PM