How to set the font of a run

Posted by: david.sheppard on 20 August 2023, 7:36 am EST

    • Post Options:
    • Link

    Posted 20 August 2023, 7:36 am EST

    My code searches through documents for complex fields and if the complex field is a mail merge field it adds text as a run after the complex field then it deletes the complex field. I want the new text to have the same font as the complex field. I cannot figure out how to set the font for the new text.

    For example, the attached document has complex fields where the font is bold for the complex fields. When I run the following code the font of the added text is not bold. Everything that I have tried to set the font for the new run gives me an error.

    Dim FileNameSource As String = “C:\temp\labreport.docx”

        Dim Doc1 As New GrapeCity.Documents.Word.GcWordDocument
        Dim DocSec1 As GrapeCity.Documents.Word.Section
        Dim DocSecRange1 As GrapeCity.Documents.Word.Range
        Dim ComplexField1 As GrapeCity.Documents.Word.ComplexField
        Dim Run1 As GrapeCity.Documents.Word.Run
        Dim FontComplectField1 As GrapeCity.Documents.Word.Font
    
        'Load the template
        Doc1.Load(fileName:=FileNameSource)
        DocSec1 = Doc1.Body.Sections.First
        DocSecRange1 = DocSec1.GetRange()
        If DocSecRange1.ComplexFields.Count > 0 Then
            ComplexField1 = DocSecRange1.ComplexFields(0)
            FontComplectField1 = ComplexField1.GetRange().Runs.Last.Font
            Run1 = ComplexField1.GetRange().Runs.Insert("", location:=InsertLocation.After)
            Run1.GetRange().Texts.Add("XXXXXXXXXX")
            ComplexField1.Delete()
        End If
    

    labreport.zip

  • Posted 21 August 2023, 7:59 am EST

    Hi,

    Thanks for the code snippet.

    We can set Font directly as it is a read-only property. However, we are discussing this with the development team Will get back to you once we have any update from them.[Internal Tracking Id - DOC-5679]

    Regards,

    Nitin

  • Posted 21 August 2023, 9:43 am EST

    Thank you. It is a big issue that an inserted run does not inherit the font of the location where the run is being inserted to. Especially since we cannot set the font of the run.

  • Posted 23 August 2023, 12:44 am EST

    Hi,

    As per the development team,

    Font is read-only, but its sub-properties are not and can be copied:

    Run1 = ComplexField1.GetRange().Runs.Insert("AAAAA", style, location:=InsertLocation.After)
        Run1.Font.Name = FontComplectField1.Name
        Run1.Font.Bold = FontComplectField1.Bold
        Run1.Font.Italic = FontComplectField1.Italic
        Run1.Font.Underline = FontComplectField1.Underline

    Currently, there is no such built-in method to copy Font to another, you need to copy all relevant style attributes one by one.

    Please refer the attached sample for the same: RunStyle.zip

    Best Regards,

    Nitin

  • Posted 23 August 2023, 8:33 am EST

    Thank you. That worked perfectly.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels