How to Set Font Properties from an Add-In
Article ID: Q137092
---------------------------------------------------------------------
The information in this article applies to:

 - Professional and Enterprise Editions of Microsoft Visual Basic,
   16-bit and 32-bit, for Windows, version 4.0
---------------------------------------------------------------------

SUMMARY
=======

Visual Basic version 4.0 allows developers to create object applications
that add capabilities to the Visual Basic development environment. These
are called Add-In applications.

An Add-In application is instantiated when a Visual Basic user selects the
Add-In through the Add-In Manager. When the Add-In starts, Visual Basic
fires a special event (ConnectAddIn) in the registered public class of the
application and passes the current instance of Visual Basic to the event.
Once this event is fired, the object application can then manipulate the
Visual Basic environment through the Visual Basic object passed to the
ConnectAddIn event. For example, forms and controls can be added to the
project, and controls can be added to forms. You can then set various
properties of controls and forms, including the Font property, from the Add-
In.

Font objects are accessed differently from Visual Basic's standard objects
when referenced from an Add-In. In Visual Basic 4.0, the Font is a standard
OLE object provided by OLE and not Visual Basic. Although the Font object
is provided, OLE does not have a mechanism for marshaling these objects
across processes, and because an Add-In is usually an out-of-process OLE
server, the Font object of forms and controls cannot be directly
manipulated. To reference the Font object, you must use late binding to
query the object for its properties from the Add-In.

MORE INFORMATION
================

NOTE: This article does not cover early and late ID binding in detail,
although it does contain a brief overview.

OLE defines a number of ways to gain access to an object's properties and
methods. Early binding and late binding are two of these methods. With
early binding, an object application provides all its interfaces through
the use of a type library. The controlling application can reference the
type library to determine which objects, properties, and methods the object
exposes.

Late binding is another way to gain access to an object. OLE defines a
standard interface called QueryInterface that allows an OLE client
(container) to query the object at run time for the interfaces that the
object provides. Because the interfaces are not known to the client
application until run time, this is called late binding.

Obviously, early ID binding is more efficient than late binding because
Visual Basic does not need to constantly call QueryInterface on objects; it
can resolve referenced interfaces at compile time and provide feedback to
the developer through syntax checking and proper capitalization. When late
binding, Visual Basic must resolve all references by calling QueryInterface
at run time and asking the object if the object, property, or method is
valid.

Step-by-Step Example
--------------------

The following sample code creates a small Add-In that, when accessed
from the Visual Basic IDE, adds a new form to the current project, adds a
Label control, and sets the Font of the control.

1. Start a new project in Visual Basic. Form1 is created by default.

2. Remove Form1 from the project by selecting Form1 in the Project window
   and clicking Remove File on the File menu (ALT F R).

3. Add a code module (Module1) to the project (ALT I M).

4. Add the following code to Module1:

   Option Explicit

   ' API functions to read/write Vb.ini
   #If Win16 Then
   Declare Function OSWritePrivateProfileString% Lib "KERNEL" Alias _
      "WritePrivateProfileString" (ByVal AppName$, ByVal KeyName$, _
      ByVal keydefault$, ByVal filename$)
   Declare Function OSGetPrivateProfileString% Lib "KERNEL" Alias _
      "GetPrivateProfileString" (ByVal AppName$, ByVal KeyName$, _
      ByVal keydefault$, ByVal ReturnString$, ByVal NumBytes As Integer, _
      ByVal filename$)
   #Else
   Declare Function OSWritePrivateProfileString% Lib "Kernel32" Alias _
      "WritePrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, _
      ByVal keydefault$, ByVal filename$)
   Declare Function OSGetPrivateProfileString% Lib "Kernel32" Alias _
      "GetPrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, _
      ByVal keydefault$, ByVal ReturnString$, ByVal NumBytes As Integer, _
      ByVal filename$)
   #End If

   Sub Main()
      Dim strReturned As String
      Dim strSection As String
      Dim rc As Variant

      ' Check to see if you are in the Vb.ini File.
      ' If not, Add yourself to the .ini file
      #If Win16 Then
         strSection = "Add-Ins16"
      #Else
         strSection = "Add-Ins32"
      #End If

      ' Initialize returned string
      strReturned = String$(255, Chr$(0))

      ' See if you are in the Vb.ini
      rc = OSGetPrivateProfileString(strSection, _
         "FontAddIn.clsAddInConnector", "NotFound", strReturned, _
         Len(strReturned) + 1, "Vb.ini")

      ' If you are not in the .ini file, add
      If Left$(strReturned, InStr(strReturned, Chr(0)) - 1) = _
         "NotFound" Then
         rc = OSWritePrivateProfileString%(strSection, _
            "FontAddIn.clsAddInConnector", "0", "Vb.ini")
      End If
   End Sub

5. Add a Class Module (Class1) to the project (ALT I C). Set the
   following properties of Class1:

   Instancing: 2 - Creatable MultiUse
   Name      : clsFontSample
   Public    : False

6. Add the following code to clsFontSample:

   Option Explicit

   ' Public object to capture instance of Visual Basic
   ' that the Add-In was started with
   Public vbInstance As Object

   ' This event is fired when the user clicks the submenu
   Public Sub AfterClick()
      Dim ct As Object
      Dim ft As Object

      ' Add a form to the project
      Set ft = vbInstance.ActiveProject.AddFormTemplate

      ' Add a label control to the new form
      Set ct = ft.ControlTemplates.Add("Label")

      ' Set the properties for the control
      With ct
         .Properties("Caption") = "This is an example of Fonts!"
         .Properties("Font")!Bold = True
         .Properties("Font")!Italic = True
         .Properties("Font")!Strikethrough = True
         .Properties("Font")!Underline = True
         .Properties("Font")!Name = "Courier New"
         .Properties("Font")!Size = 14
         .Properties("Autosize") = True
         .Properties("Left") = (ft.Properties("Width") - _
         .Properties("Width")) / 2
      End With

      ' Free objects
      Set ct = Nothing
      Set ft = Nothing
   End Sub

7. Add another Class Module (Class1) to the project (ALT I C). Set the
   following properties of Class1:

   Instancing: 2 - Creatable MultiUse
   Name      : clsAddInConnector
   Public    : True

8. Add the following code to clsAddInConnector:

   Option Explicit

   Dim lclVBInstance As Object
   Dim FontHandler As New clsFontSample

   ' Main menu and font menu
   Dim MainMenu As Object
   Dim FontMenu As Object

   ' This event is fired when the user selects from the Add-In Manager
   Public Sub ConnectAddIn(vbInstance As Object)
      Dim FontCookie As Long

      ' Capture the Visual Basic Instance
      Set lclVBInstance = vbInstance

      ' Add the menu to Visual Basic's Add-In menu
      Set MainMenu = lclVBInstance.AddInMenu.MenuItems.AddMenu _
         ("Font Add-In")
      Set FontMenu = MainMenu.MenuItems.Add("Fonts")

      ' Connect the font menu to the font handler class
      FontCookie = FontMenu.ConnectEvents(FontHandler)

      ' Pass the instance to the class handler
      Set FontHandler.vbInstance = lclVBInstance
   End Sub

   Public Sub DisconnectAddIn(Reason As Integer)
      ' This event is fired when you are removed through the Add-In Manger
      ' or if Visual Basic is shut down. The means is passed through
      ' Reason

      ' Remove the menus you added
      MainMenu.MenuItems.Remove FontMenu
      lclVBInstance.AddInMenu.MenuItems.Remove MainMenu
   End Sub

9. To set the project options, click Options on the Tools Menu (ALT T O).
   Click the Project tab of the Options dialog box, and set the following
   project properties:

   Application Description: Add-In Sample To Show Setting Fonts
   Startup Form           : Sub Main
   Project Name           : FontAddIn
   StartMode              : OLE Server

10. Save the project (ALT F V), and start the Add-In by pressing F5.
    Minimize the instance of Visual Basic.

11. Start another instance of Visual Basic, and open the Add-In Manager
    from the Add-In menu (ALT A A). Select FontAddIn.clsAddInConnector and
    click OK. The Add-In will be instantiated, and the Font Add-In menu
    will appear on Visual Basic's Add-In menu. To start the Add-In sample,
    click Font Add-In on the Add-In menu, and then click Fonts (ALT A F F).

A new form will be added to your project (Form2) and a Label control will
be added to the form. The Add-In will then set the caption of the Label and
number of the Font properties of the control.

To remove the Add-In from Visual Basic's IDE, start the Add-In manager
again (ALT A A), clear the FontAddIn.clsAddInConnector selection, and
click OK.

Additional reference words: 4.00 AddIn Font stdFont vb4win vb4all
KBCategory: kbole kbcode kbenv
KBSubcategory: IAPOLE
