none
Augh! - OpenXML is Hard! - Trying to Change Font in VB.NET Using OpenXML on Word Document RRS feed

  • Question

  • I am new to using the OpenXML SDK in VB.NET for creating MS Word documents. (I am switching over from using the COM object which works great but interferes with any open word documents.)

    My question is how do I change font sizes? I can insert text, insert images, change fonts, but cannot seem to get the font size to change.

        Public Sub CreateWordprocessingDocument(ByVal filepath As String)
            Using wordDocument As WordprocessingDocument = _
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)
                Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
                Dim rPrArial As New RunProperties(New RunFonts() With {.Ascii = "Arial"})
                Dim rPrCourier As New RunProperties(New RunFonts() With {.Ascii = "Courier"})
                Dim run_paragraph As New Run
                Dim RunProperties As RunProperties = run_paragraph.AppendChild(New RunProperties())
                Dim runFonts1 As New RunFonts
                Dim rFontSize As New FontSize
                mainPart.Document = New Document()
                Dim body As Body = mainPart.Document.AppendChild(New Body())
                Dim para As Paragraph = body.AppendChild(New Paragraph())
                Dim run As Run = para.AppendChild(New Run())
                run.AppendChild(Of RunProperties)(rPrArial)
                rFontSize.Val = "22"
                run.AppendChild(rFontSize)
                run.AppendChild(New Text("Some Text"))
            End Using
        End Sub

    What am I doing wrong? Also, how would I change font color.

    Any help would be greatly appreciated.

    Lou


    Tuesday, March 7, 2017 10:38 PM

Answers

  • Hi Lou,

    I am glad it works for you, and I would suggest you mark my first suggestion as answer to close original issue.

    >> When I try to change fonts to another size a second time, that does not work.

    For this new issue, I would suggest you post a new thread, and share us more information like do you want to change the different font size for the same string or different string.

    To focus on the specific issue, one issue is recommended to post in one thread.

    Best Regards,

    Edward


    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.

    • Marked as answer by Lou Gallo Friday, March 10, 2017 12:54 PM
    Friday, March 10, 2017 8:23 AM

All replies

  • Hi,

    You need to append RunFonts to RunProperties instead of Run.

    Here is a simple code:

        Public Sub CreateWordprocessingDocument(ByVal filepath As String)
            Using wordDocument As WordprocessingDocument =
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)
                Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
                Dim rPrArial As New RunProperties(New RunFonts() With {.Ascii = "Arial"})
                Dim runProp As RunProperties = New RunProperties()
                Dim rFontSize As New FontSize()
                mainPart.Document = New Document()
                Dim body As Body = mainPart.Document.AppendChild(New Body())
                Dim para As Paragraph = body.AppendChild(New Paragraph())
                Dim run As Run = para.AppendChild(New Run())
                runProp.AppendChild(rPrArial)
                rFontSize.Val = "96"
                runProp.AppendChild(rFontSize)
                run.PrependChild(Of RunProperties)(runProp)
                run.AppendChild(New Text("Some Text"))
            End Using
        End Sub

    Note, 96 is half-point font size which means you will get 48 fonts.

    Best Regards,

    Edward


    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.

    Wednesday, March 8, 2017 6:02 AM
  • Thanks Edward, that worked. OpenXML still hates me though. :-)

    When I try to change fonts to another size a second time, that does not work.

    Sorry to keep bugging you. Also how do you change font colors?

    Any help is appreciated.

        Public Sub CreateWordprocessingDocument(ByVal filepath As String)
            Using wordDocument As WordprocessingDocument =
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)
                Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
                Dim rPrArial As New RunProperties(New RunFonts() With {.Ascii = "Arial"})
                Dim rPrCourier As New RunProperties(New RunFonts() With {.Ascii = "Courier"})
                Dim runProp As RunProperties = New RunProperties()
                Dim rFontSize As New FontSize()
                mainPart.Document = New Document()
                Dim body As Body = mainPart.Document.AppendChild(New Body())
                Dim para As Paragraph = body.AppendChild(New Paragraph())
                Dim run As Run = para.AppendChild(New Run())
                Dim break1 As New Break()
                break1.Type = BreakValues.Page
                '
                runProp.AppendChild(rPrArial)
                rFontSize.Val = "96"
                runProp.AppendChild(rFontSize)
                run.PrependChild(Of RunProperties)(runProp)
                run.AppendChild(New Text("Some Text"))
                run.AppendChild(New Break())
                '
                run.PrependChild(Of RunProperties)(runProp.CloneNode(True))
                run.AppendChild(New Text("Some Text 2").CloneNode(True))
                run.AppendChild(New Break())
                '
                run.PrependChild(Of RunProperties)(runProp.CloneNode(True))
                run.AppendChild(New Text("Some Text 3").CloneNode(True))
                run.AppendChild(break1)
                '
                '   How do I change to a new font size? This does not work. It sets everything to 8 point font.
                '
                rFontSize.Val = "8"
                runProp.AppendChild(rFontSize.CloneNode(True))
                run.PrependChild(Of RunProperties)(runProp.CloneNode(True))
                run.AppendChild(New Text("Some Text 4").CloneNode(True))
                run.AppendChild(break1.CloneNode(True))
                '
                run.PrependChild(Of RunProperties)(runProp.CloneNode(True))
                run.AppendChild(New Text("Some Text 5").CloneNode(True))
            End Using
        End Sub
    Lou
    Thursday, March 9, 2017 9:12 PM
  • Hi Lou,

    I am glad it works for you, and I would suggest you mark my first suggestion as answer to close original issue.

    >> When I try to change fonts to another size a second time, that does not work.

    For this new issue, I would suggest you post a new thread, and share us more information like do you want to change the different font size for the same string or different string.

    To focus on the specific issue, one issue is recommended to post in one thread.

    Best Regards,

    Edward


    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.

    • Marked as answer by Lou Gallo Friday, March 10, 2017 12:54 PM
    Friday, March 10, 2017 8:23 AM
  • Thanks Edward. I will open a new thread for the next question. Thanks for your help!
    Friday, March 10, 2017 12:55 PM