Adding Images (Visual Basic .NET Tutorial) [Office 2003 SDK Documentation]
Previous Adding Labels, Separator Lines, and Hyperlinks
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 SmartDocXmlTypeName = cIMAGE
In the SmartDocXMLTypeCaption property, insert the following code.
Case 8 SmartDocXmlTypeCaption = "Images"
In the ControlCount property, insert the following code. You are adding two images to the task pane for the image element.
Case cIMAGE ControlCount = 2
In the ControlID property, insert the following code.
Case cIMAGE ControlID = ControlIndex + 700
In the ControlTypeFromID property, insert the following code.
Case 701, 702 ControlTypeFromID = C_TYPE.C_TYPE_IMAGE
In the ControlCaptionFromID property, insert the following code.
Case 701 ControlCaptionFromID = _ "Click letter to type text." Case 702 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, place 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 clicked.
Dim objRange As Microsoft.Office.Interop.Word.Range Dim strImage As String Static strText As String Select Case ControlID Case 701 objRange = Target.XMLNodes(1).Range 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 objRange.Text = strText Case 702 objRange = Target.XMLNodes(1).Range strImage = strPath & "simplesample.bmp" objRange.Select() Target.Application.Selection.InlineShapes.AddPicture(strImage) 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.