Share via


Adding Document Fragments (Visual Basic .NET 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.

    Const cDOCFRAG As String = cNAMESPACE & "#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.

    Const cTYPES As Integer = 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, insert the following code.

            Case 9
                SmartDocXmlTypeName = cDOCFRAG
    

    In the SmartDocXMLTypeCaption property, insert the following code.

            Case 9
                SmartDocXmlTypeCaption = _ 
                    "Document Fragments"
    

    In the ControlCount property, insert the following code.

            Case cDOCFRAG
                ControlCount = 2
    

    In the ControlID property, insert the following code.

            Case cDOCFRAG
                ControlID = ControlIndex + 800
    

    In the ControlTypeFromID property, insert the following code.

            Case 801
                ControlTypeFromID = C_TYPE.C_TYPE_DOCUMENTFRAGMENT
            Case 802
                ControlTypeFromID = _
                    C_TYPE.C_TYPE_DOCUMENTFRAGMENTURL
    

    In the ControlCaptionFromID property, insert the following code.

            Case 801
                ControlCaptionFromID = _
                    "SimpleSample text"
            Case 802
                ControlCaptionFromID = _
                    "Gettysburg Address"
    
  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 and a path to an external file that contains the document fragment. For more information about document fragments, see Using Document Fragments.

        Select Case ControlID
            Case 801
                DocumentFragment = "The quick red " & _
                    "fox jumped over the lazy brown dog."
            Case 802
                DocumentFragment = strPath & "gettysburgaddress.xml"
        End Select
    
  5. 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.

        Dim objXML As System.Xml.XmlDocument
        Dim objRange As Microsoft.Office.Interop.Word.Range
    

    Then insert the following code into the Select Case statement.

                Case 801
                    objRange = Target
                    objRange.XMLNodes(1).Text = "The quick red fox jumped over " & _
                        "the lazy brown dog."
                Case 802
                    objRange = Target
                    objXML = New Xml.XmlDocument()
                    objXML.Load(strPath & "gettysburgaddress.xml")
                    objRange.XMLNodes(1).Range.InsertXML(objXML.InnerXml)
                Case Else
    
  6. 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.

Next  Adding ActiveX Controls