Etudes for Microsoft Word Programmers

Home ] Up ] Etude 2.1. Built-in Toolbars ] Etude 2.2. Custom Toolbars ] Etude 2.3. Built-in Icons ] Etude 2.4. Icon Instruments ] Etude 2.5. Modifying Toolbars ]


Etude 2.4. Icon Instruments

                                                                When you're showing off, the only thing that matters is the screen shot. Make it 100% beautiful. Don't, for a minute, think that you can get away with asking anybody to imagine how cool this would be. Don't think that they're looking at the functionality. They're not. They want to see pretty pixels.
 

Joel Spolsky. The Iceberg Secret, Revealed.

 

The programming way of creation a toolbar with controls of different types was investigated in the previous etude.

Let’s now study the programming way of inserting icons and other graphic elements. The functions of the controls CopyFace() and PasteFace() are suitable for working with pictures. The function CopyFace() copies the element image to the clipboard and the function PasteFace() pastes the image from the clipboard. CopyFace() can work only with the available images.

Using authentic images requires additional effort. In this etude we investigate the creation of an icon with the help of special dynamic library and also the creation of an icon using graphic files. Files with extensions which can be pasted in the Microsoft Word document are suitable (BMP, JPG, GIF, TIF and many others).

The Clip2File utility is also the tool suitable for working with icons.

 

Purpose

The utility "Clip2File.exe" works with the clipboard and is intended for saving information and its transfer/relocating from the file to the clipboard and vice versa. Saving files have special format. The utility also can copy bitmap files directly to the clipboard.

Certainly, all the described actions could be implemented in any graphic editor. In Paint, for instance.

Resources

Program "Clip2File.exe", 82 Êá, ZIP,  [download].

Source code of the Clip2File project [later!].

The Clip2File page

Main Window

The utility main window looks like this:

Basic Functions

Basic functions of the utility "Clip2File.exe" are briefly described in the following table.

Button

Description

Save CB Save the information from the clipboard in the program internal buffer.
The previous data are lost.
Restore CB Restore the previously saved data from the internal buffer to the clipboard.
Browse Select a file for the command "File2Clip" or "Bitmap File2Clip".
Clip2File Save the data from the clipboard in the selected file. The information is saved formatted in a special way.
File2Clip Restore the data from the selected file. The file should be created with the help of the "Clip2File" command.
Bitmap File2Clip Restore the data from the selected file. The file should have the .BMP extension.
Close Close the Clip2File.exe program window.

Resources

The template "AhWordIcons1.dot" is intended for working together with the library AhWordImages.dll 105 Êá, ZIP format [download].

AhWordImages.dll library

The AhWordImages.dll Library exports the CopyWordImage(imageId) function, which copies the set of the pictures prepared beforehand to the clipboard.

Any library with the necessary pictures may be used in much the same way.

Source code of the  AhWordImages project [later!].

The Clip2File page

The dynamic library AhWordImages.dll should be placed in the system folder (it is usually "C:\Windows\System32") before use.

Functional Specification

Task

The algorithm of the Microsoft Word control icon creation “manually”  consists of the following steps:

1.     the creation of a new or the opening of the available picture in any graphic editor

2.     copying of the image data to the clipboard

3.     using the PasteFace() control function in the VBA code

The task is to create the "AhWordIcons1.dot" template to demonstrate the application capabilities of the user’s images for the Microsoft Word control toolbar icon.

The dynamic library (let’s call it "AhWordImages.dll") should export the function CopyWordImage(imageId), which copies to the clipboard the necessary image from the outer file or from the inner library resource.

Then, using the following code in VBA the Microsoft Word control icon is created on the basis of the image from the library number imageId.

' declaration of the external library function
Declare Function CopyWordImage Lib "AhWordImages.dll" (imageId As Long) As Long
 
With CommandBarControls.Item(1)    ' the required control
      .Caption = "Caption"         ' cation
      CopyWordImage imageId        ' copy image to the clipboard
      .PasteFace                   ' paste clipboard image to the control
End With

Toolbar

The "AhWordIcons1.dot" template contains a number of macros and the toolbar "Testing AhWordImages.dll" aimed at testing the functions of the Microsoft Word toolbar button which alters the image.

The toolbar "Testing AhWordImages.dll" looks like this:

Button

Macro

Description

Help

AhWordImagesHelp

Brief prompt on the toolbar buttons.

Image id =

AhTestWordImagesDLL

Pastes the icon from the library.

Image From File

AhWordImageFromFileTest

Pastes the icon from the selected file.

Brief Prompt on the Toolbar Buttons

Brief prompt on the toolbar buttons looks like this:

Pasting an Icon from the Library

To paste the icon from the library the user presses the button "Image id" (the second button of the toolbar). As the result the icon of the second button is changed./ changes its outlook/shape/form/ style.

 

Pasting an Icon from the Selected File

To paste the icon from the graphic file the user presses the button "Image From File”, inputs the path of the file and presses the button “OK”.

The image from the graphic file is pasted to the active document, selected, copied to the clipboard and deleted from the document. After the pasting of the icon from the graphic file the icon of the second button is changed./ changes its outlook/shape/form/ style.

Implementation - template "AhWordIcons1.dot"

Declaration of the Library External Function

'
' File AhWordIcons1.dot|AhWordIcons1
'
' Etudes for Microsoft Word Programmers.
' Etude 2.4. Icon Instruments.
'
' First published on http://www.transcriber.ru
'
' © 2000-2008. Evgeny Akhundzhanov. All rights reserved worldwide.
'
Option Explicit
Declare Function CopyWordImage Lib "AhWordImages.dll" (ByVal imageId As Long) As Long
Dim dwImageId As Long
 

Constants

'
' Constants
'
Const strThisToolBarName As String = "Testing AhWordImages.dll"
Const uiTargetButtonIndex As Long = 2

Subroutine AutoExec

The AutoExec subroutine is called only in case the template is located in the startup folder.

'
' AutoExec
'
' !!! This will run only if template is in the Startup folder.
'
Sub AutoExec()
    dwImageId = 1
    With CommandBars(strThisToolBarName).Controls.Item(uiTargetButtonIndex)
        .Style = msoButtonIconAndCaption
        .Caption = "Press button to test CopyWordImage function"
   
End With
End Sub

Subroutine AhWordImagesHelp

'
' AhWordImagesHelp
'
Sub AhWordImagesHelp()
    MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
        "Etude 2.4. Icon Instruments (AhWordIcons1.dot)." & vbCrLf & _
        "http://www.transcriber.ru" & vbCrLf & vbCrLf & _
        "Image id - inserts next icon from AhWordImages.dll" & vbCrLf & _
        "Image From File - inserts button icon from graphic file"
   
AutoExec
End Sub

Subroutine AhTestWordImagesDLL

'
' AhTestWordImagesDLL
'
Sub AhTestWordImagesDLL()
Dim dwResult As Long
Dim strLabel As String
    ' assume 1st item is "Help"
    With CommandBars(strThisToolBarName).Controls.Item(uiTargetButtonIndex)
        .Style = msoButtonIconAndCaption
        dwResult = CopyWordImage(dwImageId)
        If 0 = dwResult Then
            strLabel = "Image id = " & dwImageId
            .PasteFace
        Else
            strLabel = "Image id = " & dwImageId & " is invalid."
            dwImageId = 0
        End If
        .Caption = strLabel
    End With
   
dwImageId = dwImageId + 1
End Sub

Subroutine AhWordImageFromFile

'
' AhWordImageFromFile
'
Sub AhWordImageFromFile(ByVal strImageFilePath As String)
Dim oShape As Shape
    '
    ' Add picture to the document's Shapes collection,
    ' select it, copy to the clipboard and delete.
    '
    Set oShape = ActiveDocument.Shapes.AddPicture(FileName:=strImageFilePath)
    oShape.Select
    Selection.CopyAsPicture
    Selection.Delete

    With CommandBars(strThisToolBarName).Controls.Item(uiTargetButtonIndex)
        .PasteFace
    End With

    Set oShape = Nothing
End Sub

Subroutine AhWordImageFromFileTest

' AhWordImageFromFileTest
'
Sub AhWordImageFromFileTest()
If Documents.Count = 0 Then Exit Sub    ' AhWordImageFromFile() requires open document
Dim strImageFilePath As String
Dim strImageFilePathDef As String
   
    strImageFilePathDef = ActiveDocument.AttachedTemplate.Path + "\" + "flag_rus.gif"
   
    strImageFilePath = InputBox("Enter path to graphic file", "AhWordImageFromFileTest", strImageFilePathDef)
    If Len(strImageFilePath) = 0 Then Exit Sub
   
    Dim bFileExists As Boolean
    Dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")
    bFileExists = fs.FileExists(strImageFilePath)
    Set fs = Nothing
   
    If Not bFileExists Then
        MsgBox "File <" & strImageFilePath & "> doesn't exist."
        Exit Sub
    End If
   
   
    On Error GoTo ErrorLabel
    AhWordImageFromFile strImageFilePath
    Exit Sub
   
ErrorLabel:
    MsgBox _
    "AhWordImageFromFile: error pasting file <" & strImageFilePath & _
    ">. Toolbar = <" & strThisToolBarName & _
    ">. Button index = " & CStr(uiTargetButtonIndex)
End Sub

Conclusion

The "Clip2File.exe" utility is aimed at working with clipboard and allows the data of the clipboard to be saved in a file.

The "AhWordImages.dll" library contains the files of the previously saved icons as resources, and exports the function CopyWordImage(), which is called from the VBA code and is used for copying icons to the clipboard.

The "AhWordIcons1.dot" template contains the "Testing AhWordImages.dll" toolbar, which demonstrates the work with the user’s icons – pasting an icon from the dynamic library or from any graphic file.

Tasks

Task 1

Study the properties and methods of the Object Shape and the Shapes Collection just in case there is still something interesting for you to work with.

 

Task 2

There are some imperfections in the code above. Firstly, the Collection Shapes of the current document is used. Thereafter at least one open document is required/necessary for the work of the macro.

Secondly, during copying and pasting the selection is changed, i.e. some properties of the Selection object are lost. Can you obviate these imperfections?

 

Task 3

Study the properties and methods of the Object CommandBarButton, including the properties Picture and Mask, which return Objects like IPictureDisp.

Pay attention to the fact that the Object stdole and the methods LoadPicture and SavePicture and its properties StdFunctions are used in the code of the examples. And there is no information about the Object stdole in the Microsoft Word  Reference Information. Find and study the description of the Objects stdole and IPictureDisp.

Write the macro that will save all the icons (and masks) of any toolbar to the disk. Use properties and methods of the Object stdole.

 


Etudes for Microsoft Word Programmers. Etude 2.4. Icon Instruments


Unless otherwise noted, all materials on this site are
© 2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site.
www.transcriber.ru | E-mail the Author