フォントのスタイル、サイズ、効果を変更する

次の例では 、Font オブジェクトと、フォントに関連する BoldItalicSizeStrikethroughUnderlineWeight プロパティを示します。 オブジェクトのフォント プロパティは、直接操作するか、またはこの例で示しているようにエイリアスを使用して操作することができます。

この例を利用するには、次のコード例をフォームのスクリプト エディターにコピーします。 コードを実行するには、 Open イベントが生じるようにフォームを開く必要があります。 Make sure that the form contains:

  • ラベル ( Label ) コントロール (Label1)

  • 4 つのトグル ボタン ( ToggleButton ) コントロール (ToggleButton1、ToggleButton2、ToggleButton3、および ToggleButton4)

  • 2 つ目のラベル ( Label ) コントロール (Label2)、およびテキスト ボックス ( TextBox ) コントロール (TextBox1)

Dim MyFont 
Dim ToggleButton1 
Dim ToggleButton2 
Dim ToggleButton3 
Dim ToggleButton4 
Dim Label1 
Dim Label2 
Dim TextBox1 
 
Sub Item_Open() 
 Set MyPage = Item.GetInspector.ModifiedFormPages("P.2") 
 Set ToggleButton1 = MyPage.ToggleButton1 
 Set ToggleButton2 = MyPage.ToggleButton2 
 Set ToggleButton3 = MyPage.ToggleButton3 
 Set ToggleButton4 = MyPage.ToggleButton4 
 Set Label1 = MyPage.Label1 
 Set Label2 = MyPage.Label2 
 Set TextBox1 = MyPage.TextBox1 
 Set MyFont = Label1.Font 
 
 ToggleButton1.Value = True 
 ToggleButton1.Caption = "Bold On" 
 
 Label1.AutoSize = True 'Set size of Label1 
 Label1.AutoSize = False 
 
 ToggleButton2.Value = False 
 ToggleButton2.Caption = "Italic Off" 
 
 ToggleButton3.Value = False 
 ToggleButton3.Caption = "StrikeThrough Off" 
 
 ToggleButton4.Value = False 
 ToggleButton4.Caption = "Underline Off" 
 
 Label2.Caption = "Font Weight" 
 TextBox1.Text = Label1.Font.Weight 
 TextBox1.Enabled = False 
 
End Sub 
 
Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 MyFont.Bold = True 'Using MyFont alias to control font 
 ToggleButton1.Caption = "Bold On" 
 MyFont.Size = 22 'Increase the font size 
 Else 
 MyFont.Bold = False 
 ToggleButton1.Caption = "Bold Off" 
 MyFont.Size = 8 'Return font size to initial size 
 End If 
 
 TextBox1.Text = CStr(MyFont.Weight) 'Bold and Weight are related 
End Sub 
 
Sub ToggleButton2_Click() 
 If ToggleButton2.Value = True Then 
 Label1.Font.Italic = True 'Using Label1.Font directly 
 ToggleButton2.Caption = "Italic On" 
 Else 
 Label1.Font.Italic = False 
 ToggleButton2.Caption = "Italic Off" 
 End If 
End Sub 
 
Sub ToggleButton3_Click() 
 If ToggleButton3.Value = True Then 
 Label1.Font.Strikethrough = True 'Using Label1.Font directly 
 ToggleButton3.Caption = "StrikeThrough On" 
 Else 
 Label1.Font.Strikethrough = False 
 ToggleButton3.Caption = "StrikeThrough Off" 
 End If 
End Sub 
 
Sub ToggleButton4_Click() 
 If ToggleButton4.Value = True Then 
 MyFont.Underline = True 'Using MyFont alias for Label1.Font 
 ToggleButton4.Caption = "Underline On" 
 Else 
 Label1.Font.Underline = False 
 ToggleButton4.Caption = "Underline Off" 
 End If 
End Sub

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。