Gabriel Vieira
Gabriel Vieira

Reputation: 57

Word how to add symbol in same line as text? (VB.NET - WORD)

I need to have a symbol followed by text all created by vb.net in word. Like: [symbol] Yes, i accept!

note: ⬜ Yes i accept (its not what i pretend) [crossedbox] yes i accept (is what i want to make)

thx in advance

Here is my code

oPara(10) = oDoc.Content.Paragraphs.Add
        oPara(10).Range.Font.Color = RGB(0, 0, 0)
        oPara(10).Range.InsertSymbol(CharacterNumber:=53, Font:="Wingdings 2", Unicode:=True)
        oPara(10).Range.Text = "S"
        oPara(10).Range.Font.Size = 7
        oPara(10).Range.Font.Bold = True
        oPara(10).Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle

        oPara(10).Range.Font.Color = RGB(0, 0, 0)
        oPara(10).Range.Font.Name = "Calibri"
        oPara(10).Range.Text = "Bla bla."
        oPara(10).Range.Font.Size = 7
        oPara(10).Range.Font.Bold = True
        oPara(10).Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle
        oPara(10).Range.InsertParagraphAfter()

Upvotes: 0

Views: 1031

Answers (1)

soohoonigan
soohoonigan

Reputation: 2360

If you don't want your styling changes to affect the rest of the paragraph, then you can't use the Paragraph.Range to set styling. You will have to take more control over where the range is referencing by explicitly keeping a range reference and moving it within the paragraph. This way, the styling instructions apply only to the range, and not the entire paragraph. In your current example, you appear to be overwriting your symbol by setting the range text to "S", which is why InsertSymbol doesn't appear to be doing anything. Here's a snippet that should get you going:

Dim oWord As New Word.Application
oWord.Visible = True
Dim oDoc As Word.Document = oWord.Documents.Add
Dim oPara As Word.Paragraph = oDoc.Content.Paragraphs.Add
Dim oRng As Word.Range = oPara.Range
oRng.InsertSymbol(CharacterNumber:=53, Font:="Wingdings 2", Unicode:=True)
oRng.Start = oRng.End + 1
oRng.Text &= " Yes I Accept"
oRng.Font.Name = "Calibri"

Inserts the following into a new word document:

enter image description here

So now that you've seen how ranges can be controlled a bit more, now you have to think about whether you really need to do that at all...because you could just save yourself that whole headache by simply using:

oPara.Range.Text = "⬜ Yes I Accept"

Upvotes: 0

Related Questions