none
How do I get unavailable fonts in word? RRS feed

  • Question

  • I want to get fonts that are not available in the document through the COM component of word. And I don't want to display word.


    I found a function "Application.SubstituteFont(System.String UnavailableFont, System.String SubstituteFont)" that can replace fonts. But I don't know how to get all the unavailable fonts.

    Monday, August 28, 2017 7:02 AM

Answers

  • Hi 李冲-LC,

    try to use code below will show you which fonts are used and which fonts are missing in current document for Word Application.

     Sub demo()
    
     Dim rngMissing As Range
     Dim iCount As Integer
     Dim sUsedFontList As String, sUnUsedFontList As String
    
     On Error GoTo ERRORHANDLER
     Selection.End = 0
     Do While Selection.End < ActiveDocument.Range.End
     Selection.SelectCurrentFont
    
     If InStr(1, sUsedFontList, Selection.Font.Name & "|") = 0 And Selection.Font.Name <> "" Then
     sUsedFontList = sUsedFontList & Selection.Font.Name & "|"
     End If
     If Selection.End = ActiveDocument.Range.End Then Exit Do
     Selection.Start = Selection.End
     Loop
     sUnUsedFontList = sUsedFontList
     For iCount = 1 To Application.FontNames.Count
     If InStr(1, sUsedFontList, Application.FontNames.Item(iCount) & "|") > 0 Then
     sUnUsedFontList = Replace(sUnUsedFontList, Application.FontNames.Item(iCount) & "|", "")
     End If
     Next iCount
     If Right(sUsedFontList, 1) = "|" Then sUsedFontList = Mid(sUsedFontList, 1, Len(sUsedFontList) - 1)
     If Right(sUnUsedFontList, 1) = "|" Then sUnUsedFontList = Mid(sUnUsedFontList, 1, Len(sUsedFontList) - 1)
     ' Adding the information in a seperate document
     Documents.Add
     ActiveDocument.Select
     With Selection
     .Delete
     .Font.Bold = True
     .TypeText "Fonts Used"
     .Font.Bold = False
     .TypeParagraph
     .TypeText sUsedFontList
     .TypeParagraph
     .TypeParagraph
     .Font.Bold = True
     .TypeText "Missing Fonts"
     .Font.Bold = False
     .TypeParagraph
     If sUnUsedFontList <> "" Then
     .TypeText sUnUsedFontList
     Else
     .TypeText "There is no missing font in this document."
     End If
     Set rngMissing = ActiveDocument.Range
     rngMissing.Find.ClearFormatting
     rngMissing.Find.Execute findtext:="|", replacewith:="^p", Replace:=wdReplaceAll
     End With
     Exit Sub
    ERRORHANDLER:
     MsgBox "Some unknown error has occured. Please contact the administrator for further details." _
     & vbCrLf & vbCrLf & Err.Description, vbCritical, "Missing Fonts"
     End Sub
    

    Output:

    if you want the solution using C# or VB.Net then I suggest you to convert the above code to the desired language. there is no any readymade function or property in the object model that can return the missing fonts in word document.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 29, 2017 1:23 AM
    Moderator

All replies

  • Hi 李冲-LC,

    try to use code below will show you which fonts are used and which fonts are missing in current document for Word Application.

     Sub demo()
    
     Dim rngMissing As Range
     Dim iCount As Integer
     Dim sUsedFontList As String, sUnUsedFontList As String
    
     On Error GoTo ERRORHANDLER
     Selection.End = 0
     Do While Selection.End < ActiveDocument.Range.End
     Selection.SelectCurrentFont
    
     If InStr(1, sUsedFontList, Selection.Font.Name & "|") = 0 And Selection.Font.Name <> "" Then
     sUsedFontList = sUsedFontList & Selection.Font.Name & "|"
     End If
     If Selection.End = ActiveDocument.Range.End Then Exit Do
     Selection.Start = Selection.End
     Loop
     sUnUsedFontList = sUsedFontList
     For iCount = 1 To Application.FontNames.Count
     If InStr(1, sUsedFontList, Application.FontNames.Item(iCount) & "|") > 0 Then
     sUnUsedFontList = Replace(sUnUsedFontList, Application.FontNames.Item(iCount) & "|", "")
     End If
     Next iCount
     If Right(sUsedFontList, 1) = "|" Then sUsedFontList = Mid(sUsedFontList, 1, Len(sUsedFontList) - 1)
     If Right(sUnUsedFontList, 1) = "|" Then sUnUsedFontList = Mid(sUnUsedFontList, 1, Len(sUsedFontList) - 1)
     ' Adding the information in a seperate document
     Documents.Add
     ActiveDocument.Select
     With Selection
     .Delete
     .Font.Bold = True
     .TypeText "Fonts Used"
     .Font.Bold = False
     .TypeParagraph
     .TypeText sUsedFontList
     .TypeParagraph
     .TypeParagraph
     .Font.Bold = True
     .TypeText "Missing Fonts"
     .Font.Bold = False
     .TypeParagraph
     If sUnUsedFontList <> "" Then
     .TypeText sUnUsedFontList
     Else
     .TypeText "There is no missing font in this document."
     End If
     Set rngMissing = ActiveDocument.Range
     rngMissing.Find.ClearFormatting
     rngMissing.Find.Execute findtext:="|", replacewith:="^p", Replace:=wdReplaceAll
     End With
     Exit Sub
    ERRORHANDLER:
     MsgBox "Some unknown error has occured. Please contact the administrator for further details." _
     & vbCrLf & vbCrLf & Err.Description, vbCritical, "Missing Fonts"
     End Sub
    

    Output:

    if you want the solution using C# or VB.Net then I suggest you to convert the above code to the desired language. there is no any readymade function or property in the object model that can return the missing fonts in word document.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 29, 2017 1:23 AM
    Moderator
  • Thanks.
    I've implemented it with c#.

    Wednesday, August 30, 2017 8:10 AM