none
How do I remove restricted Fonts programmatically from a Word document? RRS feed

  • Question

  • Hello all,

    Below code snippet raises an error when the [sOfficeFile] is using or contains fonts that are only licensed for printing or for use in printpreview. The SaveAs2 method is used to create a PDF file of the [sOfficeFile].

    When opening the [sOfficeFile] 'manually' Word2010 presents me a 'button' [Remove restricted fonts] in the goldish bar just below the ribbon. Pressing this button and saving the [sOfficeFile] results in the 'oDoc.SaveAs2' method running smoothly.

    What I'm looking for is the method / workaround to do this manual action in code....

    Public Sub OfficeToPDF(ByVal sOfficeFile As String)
    
            Dim oApp As New Microsoft.Office.Interop.Word.Application
            Select Case sOfficeFile.Substring(InStrRev(sOfficeFile, ".")).ToLower
                Case "doc"
                    Dim oDoc As Microsoft.Office.Interop.Word.Document = oApp.Documents.Open(sOfficeFile, AddToRecentFiles:=False)
                    oDoc.SaveAs2(sOfficeFile.Substring(0, InStrRev(sOfficeFile, ".")) & "pdf", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF, AddToRecentFiles:=False)
                    oDoc.Close(SaveChanges:=0)
                    oDoc = Nothing
            End Select
            Application.DoEvents()
            oApp.Quit()
            oApp = Nothing
    End sub

    Any help is greatly appreciated.

    --Erik--

    Wednesday, July 22, 2015 11:25 AM

Answers

  • You could check all the fonts in use, using the Font properties extension from MS (http://www.microsoft.com/typography/TrueTypeProperty21.mspx), and substitute another font when saving as PDF, or you could trap the error when saving with restricted fonts and set the ActiveDocument.EmbedTrueTypeFonts property to False before redoing the save.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Erik Boersma Friday, July 24, 2015 2:00 PM
    Wednesday, July 22, 2015 11:51 PM
  • Hi Paul,

    Thanks for your reply and the useful pointer.

    Part of the problem is the absence of the licensed Font that's used in these Word Files. So 'checking them out' is not really an option in this.

    I used the ActiveDocument.EmbedTrueTypeFonts=False you mentioned, this probably would work, albeit that Word(2010) opens the .DOC files in an 'editing restricted mode' which prevents this action. (despite my settings in the 'Trust Centre of Word'(?))

    Instead of trying to wrestle with this next challenge I used a different method to get my results; ActiveDocument.ExportAsFixedFormat 

    Dim oApp As New Microsoft.Office.Interop.Word.Application 
    Select Case sOfficeFile.Substring(InStrRev(sOfficeFile, ".")).ToLower 
      Case "doc" 
        Dim oDoc As Microsoft.Office.Interop.Word.Document = oApp.Documents.Open(sOfficeFile, AddToRecentFiles:=False) 
        oDoc.ExportAsFixedFormat(sOfficeFile.Substring(0, InStrRev(sOfficeFile, ".")) & "pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF) 
        oDoc.Close(SaveChanges:=0) 
        oDoc = Nothing 
    End Select 
    Application.DoEvents() 
    oApp.Quit() 
    oApp = Nothing 

    Thanks again!

    --Erik--


    Friday, July 24, 2015 1:59 PM

All replies

  • You could check all the fonts in use, using the Font properties extension from MS (http://www.microsoft.com/typography/TrueTypeProperty21.mspx), and substitute another font when saving as PDF, or you could trap the error when saving with restricted fonts and set the ActiveDocument.EmbedTrueTypeFonts property to False before redoing the save.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Erik Boersma Friday, July 24, 2015 2:00 PM
    Wednesday, July 22, 2015 11:51 PM
  • Hi Paul,

    Thanks for your reply and the useful pointer.

    Part of the problem is the absence of the licensed Font that's used in these Word Files. So 'checking them out' is not really an option in this.

    I used the ActiveDocument.EmbedTrueTypeFonts=False you mentioned, this probably would work, albeit that Word(2010) opens the .DOC files in an 'editing restricted mode' which prevents this action. (despite my settings in the 'Trust Centre of Word'(?))

    Instead of trying to wrestle with this next challenge I used a different method to get my results; ActiveDocument.ExportAsFixedFormat 

    Dim oApp As New Microsoft.Office.Interop.Word.Application 
    Select Case sOfficeFile.Substring(InStrRev(sOfficeFile, ".")).ToLower 
      Case "doc" 
        Dim oDoc As Microsoft.Office.Interop.Word.Document = oApp.Documents.Open(sOfficeFile, AddToRecentFiles:=False) 
        oDoc.ExportAsFixedFormat(sOfficeFile.Substring(0, InStrRev(sOfficeFile, ".")) & "pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF) 
        oDoc.Close(SaveChanges:=0) 
        oDoc = Nothing 
    End Select 
    Application.DoEvents() 
    oApp.Quit() 
    oApp = Nothing 

    Thanks again!

    --Erik--


    Friday, July 24, 2015 1:59 PM