I want to add a custom control (which i have created) in a word document with an application add-in and have it persist when I save and close the document. However, the control disappears when I save and close the document.
From my understanding this has to do with the custom control being an extended "object" that only exist in the extended word object obtained with GetVstoObject.
MSDN propose the following to "persist" the control:
Re-Creating Windows Forms Controls When Documents Are Opened
You can re-create deleted Windows Forms controls when the user reopens the document. To do this, your solution must perform the following tasks:
Store information about the size, location, and state of the controls when the document is saved or closed. In a document-level customization, you can save this data to the data cache in the document. In an application-level add-in, you can save this data to a custom XML part in the document.
Re-create the controls in an event that is raised when the document is opened. In document-level projects, you can do this in the Sheetn_Startup or ThisDocument_Startup event handlers. In application-level projects, you can do this in the event handlers for the WorkbookOpen or DocumentOpen events.
I basically have two questions:
1)This add-in will be used globally, and I understand that some office 2007 in the USA would not have the custom xml functionality, and from another post on the forum here that this also does not exist in any office 2010, so this would not be a "global" solution, or should my firm update to 2010, it will not work on any computer? Therefore, using the custom XML parts to store the "information" about the control and regenerate them upon opening the document would not be I full solution, either for the people in the USA currently, or everyone should my firm upgrade to 2010?
Am I correct with the above, if so then I will have to find another solution.
2). If I add a dynamic control to the word document and I save and close it, some wrapper remains? What type is this "wrapper", how would I access this wrapper before I save and close the document and how can I cycle through these wrappers when I reopen the document?
- 편집됨 RoelofC 2012년 5월 1일 화요일 오전 12:43
Q 1: Custom XML Parts are something different from the "custom XML" technology that was removed from Office due to the court decision. Custom XML Parts are XML files stored in the XML (Zip) package file structure, but aren't displayed on the document surface. The thing that was removed was the display of XML Nodes on the document surface. So there absolutely no problem with storing the information as the article suggests.
Q 2: The only non-text thing a Word document can handle is a graphic. As far as the document is concerned, all controls placed on it are graphics belonging to either the InlineShapes or Shapes collection; usually a control will be an InlineShape object (no text flow wrapping around it - it behaves like a text character). This graphical object is the "wrapper" - think of it as a placeholder.
If the document contains no other InlineShape objects, then you can loop that collection. However, an InlineShape provides no indentifying information; it has no Name or Tag property you can use. What is possible is to place the a Bookmark object "around" the InlineShape. You can (must) give a bookmark a name and your code can access a bookmark's contents directly. In VB-speak the basic syntax would be:
Cindy Meister, VSTO/Word MVP
- 답변으로 표시됨 Tom_Xu_WXModerator 2012년 5월 8일 화요일 오전 9:33