Adding Document Fragments (C# Tutorial) [Office 2003 SDK Documentation]
Previous Adding Images
The following steps show you how to add document fragments to the SimpleSample smart document.
The first thing you need to do is add a constant for the documentfragment element in the SimpleSample schema. Insert the following code into the general declarations section of your document, below the existing constants.
const String cDOCFRAG = cNAMESPACE + "#documentfragment";
Next, you need to add 1 to the cTYPES constant. Remove the existing cTYPES constant, and enter the following code or change your code to match.
const Int32 cTYPES = 9;
Now you are ready to modify the existing code to insert the document fragment. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.
In the SmartDocXMLTypeName property subroutine, insert the following code.
case 9: strTypeName = cDOCFRAG; break;
In the SmartDocXMLTypeCaption property subroutine, insert the following code.
case 9: strTypeCaption = "Document Fragment"; break;
In the ControlCount property subroutine, insert the following code.
case cDOCFRAG: intNumberOfControls = 2; break;
In the ControlID property subroutine, insert the following code.
case cDOCFRAG: intControlID = ControlIndex + 800; break;
In the ControlTypeFromID property subroutine, insert the following code.
case 801: type = C_TYPE.C_TYPE_DOCUMENTFRAGMENT; break; case 802: type = C_TYPE.C_TYPE_DOCUMENTFRAGMENTURL; break;
In the ControlCaptionFromID property subroutine, insert the following code.
case 801: strControlCaption = "SimpleSample text"; break; case 802: strControlCaption = "Gettysburg Address"; break;
Use the PopulateDocumentFragment method to specify the text or file location of the document fragment. The following code shows how to use a hard-coded string and a path to an external file that contains the document fragment. For more information about document fragments, see Using Document Fragments.
switch (ControlID) { case 801: DocumentFragment = "The quick red fox jumped over " + "the lazy brown dog."; break; case 802: DocumentFragment = strPath + "gettysburgaddress.xml"; break; }
Add the following variable declaration and code to the InvokeControl method. This specifies what happens when the user clicks the document fragment in the Document Actions task pane. Insert the object variable for the System.Xml.XmlDocument object, as shown below, at the top of the InvokeControl method subroutine.
System.Xml.XmlDocument objXML; Microsoft.Office.Interop.Word.Range objRange; //Temporary object for the optional parameters //of the InsertXML method. object objTemp = System.Reflection.Missing.Value;
Then insert the following code into the Select Case statement.
case 801: objRange = (Microsoft.Office.Interop.Word.Range)Target; objRange.XMLNodes[1].Range.Text = "The quick red fox jumped over the lazy brown dog."; break; case 802: objRange = (Microsoft.Office.Interop.Word.Range)Target; objXML = new System.Xml.XmlDocument(); objXML.Load (strPath + "gettysburgaddress.xml"); objRange.XMLNodes[1].Range.InsertXML(objXML.InnerXml, ref objTemp); break;
Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.