简体   繁体   中英

Get a list of all fonts in VBA Excel 2010

I am working in excel VBA and I want to get the list of all fonts in a combo box

Can any one help me please

I tried this code but i am getting error in listcount :

...

    Set FontList = Application.CommandBars("Formatting").FindControl(ID:=1728)

    ' Put the fonts into column A

    *For i = 0 To FontList.ListCount - 1*
        combobox.AddItems  FontList.List(i + 1)
    Next i

    ' Delete temp CommandBar if it exists
    On Error Resume Next
    TempBar.Delete
End Sub

The FontList should be returning a list that is indexed as 1 based. There is no need to start at 0.

Dim FontList
Dim i As Long

Set FontList = Application.CommandBars("Formatting").FindControl(ID:=1728)

'Put the fonts into column A
For i = 1 To FontList.ListCount
    Debug.Print FontList.List(i)
    Cells(Rows.Count, 1).End(xlUp)(2) = FontList.List(i)
    'combobox.AddItems FontList.List(i)
    If i > 50 Then Exit For
Next i

That should build a list of fonts into column A of the ActiveSheet . When that is working, remove the commenting so that it goes into your combobox.

Note that you will be getting a list of fonts that exactly duplicates the font list dropdown on the Home ribbon. There will likely ba a few duplicates as that list duplicates a couple of fonts at the top of the list for the default Heading and Body categories.

Another way to get the list of fonts (from Word)

Option Explicit

Sub listFonts()
    Dim wd As Object, fontID As Variant

    Set wd = CreateObject("Word.Application")

    For Each fontID In wd.FontNames
        Sheet1.cmbFonts.AddItem fontID
    Next
    wd.Quit
    Set wd = Nothing
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM