I am using Microsoft Visual Basic for Applications and have a standard form containing a list box (”ListBox1”) and a label (”Label1”). I am trying to copy bold and italic properties from the list box to the label, as shown in the code below:
Option Explicit
Private Sub UserForm_Initialize()
Dim FontHeight As Double
With Label1
Debug.Print "Label1 :", .Font.Name, Font.Size, .Font.Bold, .Font.Italic, Font.Weight
End With
With ListBox1
Debug.Print "ListBox1 :", .Font.Name, Font.Size, .Font.Bold, .Font.Italic, Font.Weight
End With
With Label1
.AutoSize = False
.Height = 100
.Font.Name = ListBox1.Font.Name
.Font.Size = ListBox1.Font.Size
' If ListBox1.Font.Bold Then
' .Font.Bold = True
' Else
' .Font.Bold = False
' End If
.Font.Bold = ListBox1.Font.Bold
.Font.Italic = ListBox1.Font.Italic
.AutoSize = True
FontHeight = .Height
Debug.Print "Label1 :", .Font.Name, Font.Size, .Font.Bold, .Font.Italic, Font.Weight
End With
End Sub
The output from the three Debug-calls are as follows:
Label1 : Tahoma 8.25 False False 400
ListBox1 : Tahoma 8.25 False False 400
Label1 : Tahoma 8.25 True True 400
As shown both by the output and by visual inspection, the label text is now both bold and italic!
I set a breakpoint at the line
.Font.Bold = ListBox1.Font.Bold
Before executing the line both .Bold values were False, but after execution .Fold.Bold becomes True.
I tried instead to use the lines commented out with the same result: The line
.Fold. Bold = False
results in .Fold.Bold becoming True!?!
The Visual Basic Reference states that Bold and Italic (as well as Underline and StrikeThrough) as Boolean variables and informs that ”Conversely, setting Weight to anything over 550 sets Bold to True; setting Weight to 550 or less sets Bold to False.”. In my case Weight = 400, so this is not the explanation.
What is the reason for this strange behavior and how can it be avoided?