Adding Images (Visual Basic 6.0 Tutorial) [Office 2003 SDK Documentation]
Previous Adding List Boxes and Combo Boxes
The following steps show you how to add two images to the SimpleSample smart document.
The first thing you need to do is add a constant for the image element in the SimpleSample schema. Insert the following code into the general declarations section of your code module, below the existing constants.
Const cIMAGE As String = cNAMESPACE & "#image"
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 = 8
Now, you are ready to modify the existing code to insert the image. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.
In the SmartDocXMLTypeName property, insert the following code.
Case 8 ISmartDocument_SmartDocXmlTypeName = cIMAGE
In the SmartDocXMLTypeCaption property, insert the following code.
Case 8 ISmartDocument_SmartDocXmlTypeCaption = "Image"
In the ControlCount property, insert the following code. You are adding two images to the task pane for the image element.
Case cIMAGE ISmartDocument_ControlCount = 2
In the ControlID property subroutine, insert the following code.
Case cIMAGE ISmartDocument_ControlID = ControlIndex + 700
In the ControlTypeFromID property, insert the following code.
Case 701, 702 ISmartDocument_ControlTypeFromID = C_TYPE_IMAGE
In the ControlCaptionFromID property, insert the following code.
Case 701 ISmartDocument_ControlCaptionFromID = _ "Click letter to type text." Case 702 ISmartDocument_ControlCaptionFromID = _ "Click image to insert into document."
Use the PopulateImage method to specify the path of the image to display in the Document Actions task pane for the image element. The following code sample designates a path for the ImageSrc parameter.
Select Case ControlID Case 701 ImageSrc = strPath & "alphabet.gif" Case 702 ImageSrc = strPath & "simplesample.bmp" End Select
In the ImageClick method, insert the code that specifies what happens when the user clicks the image. The following code uses the XCoordinate and YCoordinate parameters to determine which letter of the alphabet the user has clicked.
Note You need to add a reference to the Microsoft Excel 11.0 Object Library in order for this code to function as it is written.
Dim objWdRange As Word.Range Dim objXlRange As Excel.Range Dim strImage As String Static strText As String Select Case ControlID Case 701 Select Case XCoordinate Case 0 To 16 Select Case YCoordinate Case 0 To 20 strText = strText & "A" Case 21 To 40 strText = strText & "G" Case 41 To 60 strText = strText & "M" Case 61 To 80 strText = strText & "S" End Select Case 17 To 32 Select Case YCoordinate Case 0 To 20 strText = strText & "B" Case 21 To 40 strText = strText & "H" Case 41 To 60 strText = strText & "N" Case 61 To 80 strText = strText & "T" End Select Case 33 To 48 Select Case YCoordinate Case 0 To 20 strText = strText & "C" Case 21 To 40 strText = strText & "I" Case 41 To 60 strText = strText & "O" Case 61 To 80 strText = strText & "U" Case 81 To 100 strText = strText & "Y" End Select Case 49 To 64 Select Case YCoordinate Case 0 To 20 strText = strText & "D" Case 21 To 40 strText = strText & "J" Case 41 To 60 strText = strText & "P" Case 61 To 80 strText = strText & "V" Case 81 To 100 strText = strText & "Z" End Select Case 65 To 80 Select Case YCoordinate Case 0 To 20 strText = strText & "E" Case 21 To 40 strText = strText & "K" Case 41 To 60 strText = strText & "Q" Case 61 To 80 strText = strText & "W" End Select Case 81 To 96 Select Case YCoordinate Case 0 To 20 strText = strText & "F" Case 21 To 40 strText = strText & "L" Case 41 To 60 strText = strText & "R" Case 61 To 80 strText = strText & "X" End Select End Select If ApplicationName = "Word.Application.11" Then Set objWdRange = Target.XMLNodes(1).Range objWdRange.Text = strText Else Set objXlRange = Target objXlRange.value = strText End If Case 702 strImage = strPath & "simplesample.bmp" If ApplicationName = "Word.Application.11" Then Set objWdRange = Target.XMLNodes(1).Range objWdRange.Select Selection.InlineShapes.AddPicture strImage Else Set objXlRange = Target objXlRange.Select Target.Parent.Pictures.Insert(strImage).Select End If strText = "" End Select
Note The XCoordinate and YCoordinate parameters are measured by using pixels. If you are using graphics for which you don't know the measurements and you need to create sectioned areas that specify different actions when different areas of a graphic are clicked (as shown in the alphabet.gif code example above), you can easily determine the location of the x-coordinate and y-coordinate by using a message box that displays the XCoordinate and YCoordinate parameters and then clicking the boundaries of the sections for which you want to provide specific actions. Then, using the x-coordinate and y-coordinate for the boundaries you specified, you can create your own select case statements to provide actions for these specific areas.
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.