Adding Document Fragments (Visual C++ Tutorial) [Office 2003 SDK Documentation]

Previous  Adding Images

The following steps show you how to add document fragments to the SimpleSample smart document.

  1. 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.

    #define cDOCFRAG(xmlns) xmlns L"#documentfragment"
    
  2. 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.

     #define cTYPES 9
    
  3. 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:
                    *Name = SysAllocString(cDOCFRAG(cNAMESPACE));
                    break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 9:
                    *Caption = SysAllocString(L"Document Fragment");
                    break;
    

    In the ControlCount property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cDOCFRAG(cNAMESPACE)) == 0)
            {           
                *Count =2;
            }
    

    In the ControlID property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cDOCFRAG(cNAMESPACE)) == 0)
            {           
                *ControlID = ControlIndex + 800;
            }
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 801:
                    *Type = C_TYPE_DOCUMENTFRAGMENT;
                    break;
    
                case 802:
                    *Type = C_TYPE_DOCUMENTFRAGMENTURL;
                    break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

                case 801:
                    *Caption = SysAllocString(L"SimpleSample text");
                    break;
    
                case 802:
                    *Caption = SysAllocString(L"Gettysburg Address");
                    break;
    
  4. 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 value and a path to an external file that contains the document fragment. For more information about document fragments, see Using Document Fragments.

            CComBSTR bstrDocFrag(m_bstrPath.m_str);
    
            switch (ControlID)
            {
                case 801:
                    *DocumentFragment=SysAllocString(L"The quick red fox jumped over the lazy brown dog");
                    break;
                case 802:                
                    bstrDocFrag.Append(L"gettysburgaddress.xml");
                    bstrDocFrag.CopyTo(DocumentFragment);
                    bstrDocFrag.Empty();
                    break;
            }
    
  5. Add the following variable declaration and code to the InvokeControl method. This method specifies what happens when the user clicks the document fragment in the Document Actions task pane. To gain access to the XML Document Object Model (DOM), you need to add a reference to the Microsoft XML, v5.0 type library. 

    How?

    1. On the the View menu, click Class Wizard.
    2. Click Add Class, and then click From a type library.
    3. Navigate to MSXML5.DLL (usually located at C:\Program Files\Common Files\Microsoft Shared\OFFICE11), and then click Open.
    4. In the Confirm Classes dialog box, click the IXMLDOMDocument class. To select multiple items in the list, hold down the CTRL key while clicking. (Note   The items in this list are not alphabetized.)
    5. Click OK. (A warning message may be displayed saying that a file was modified externally; click Yes and close the Class Wizard dialog box.) You now have a new file named msxml5.cpp in your list of source files.

    At the top of the CActions.cpp file, add the following line.

    #include "msxml5.h"
    

    Then insert the object variable for the DOMDocument object, shown below, at the top of the InvokeControl method's subroutine.

            Range objRange, objNodeRange;        
            XMLNodes objXMLNodes;    
            XMLNode objXMLNode;
            IXMLDOMDocument xmlDoc;
            CComBSTR bstrPath(m_bstrPath.m_str);
            VARIANT xmlpath;
    

    Paste the following code into the Select Case statement. Note that the use of the async property of the DOMDocument object is necessary to ensure that the code that inserts the XML from the XML document does not run before the document is fully loaded.

                case 801:
                    SetXMLNodeText(Target,1,"The quick red fox jumped over the lazy brown dog",false);
                    break;
    
                case 802:
                    xmlDoc.CreateDispatch("Msxml2.DOMDocument.5.0",NULL);
    
                    bstrPath.Append("gettysburgaddress.xml");
                    xmlpath.vt=VT_BSTR;
                    bstrPath.CopyTo(&xmlpath.bstrVal);
                    xmlDoc.load(xmlpath);
                    bstrPath.Empty();                
    
                    objRange.AttachDispatch(Target);
                    objXMLNodes.AttachDispatch(objRange.GetXMLNodes());
                    objXMLNode.AttachDispatch(objXMLNodes.Item(1));
                    objNodeRange.AttachDispatch(objXMLNode.GetRange());                
    
                    objNodeRange.InsertXML(xmlDoc.GetXml(),NULL);
    
                    objNodeRange.DetachDispatch();
                    objXMLNode.DetachDispatch();
                    objXMLNodes.DetachDispatch();
                    objRange.DetachDispatch();
                    xmlDoc.DetachDispatch();
                    break;
    
  6. Recompile your SimpleSample smart document dynamic-link library (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.

Next  Adding Labels, Separator Lines, and Hyperlinks