Share via


Adding Document Fragments (Visual Basic 6.0 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
                ISmartDocument_SmartDocXmlTypeName = cDOCFRAG
    

    In the SmartDocXMLTypeCaption property, insert the following code.

            Case 9
                ISmartDocument_SmartDocXmlTypeCaption = _ 
                    "Document Fragments"
    

    In the ControlCount property, insert the following code.

            Case cDOCFRAG
                ISmartDocument_ControlCount = 2
    

    In the ControlID property, insert the following code.

            Case cDOCFRAG
                ISmartDocument_ControlID = ControlIndex + 800
    

    In the ControlTypeFromID property, insert the following code.

            Case 801
                ISmartDocument_ControlTypeFromID = C_TYPE_DOCUMENTFRAGMENT
            Case 802
                ISmartDocument_ControlTypeFromID = _
                    C_TYPE_DOCUMENTFRAGMENTURL
    

    In the ControlCaptionFromID property, insert the following code.

            Case 801
                ISmartDocument_ControlCaptionFromID = _
                    "SimpleSample text"
            Case 802
                ISmartDocument_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. To gain access to the XML Document Object Model (DOM), you need to add a reference to the Microsoft XML, v5.0 object model. You can then insert the object variable for the DOMDocument object, shown in the following code, at the top of the InvokeControl method subroutine.

        Dim objXML As MSXML2.DOMDocument
        Dim objRange As Word.Range
    

    Paste the following code into the Select Case statement. 

    Note  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
                If ApplicationName = "Word.Application.11" Then
                    Set objRange = Target.XMLNodes(1).Range
                    objRange.Text = "The quick red fox jumped over the lazy brown dog."
                    Set objRange = Nothing
                End If
            Case 802
                If ApplicationName = "Word.Application.11" Then
                    Set objRange = Target.XMLNodes(1).Range
                    Set objXML = New MSXML2.DOMDocument50
    
                    objXML.async = False
                    objXML.Load (strPath & "gettysburgaddress.xml")
                    objRange.InsertXML objXML.Xml
    
                    Set objXML = Nothing
                    Set objRange = Nothing
                End If
            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