3

Hello i try to make a part of a string bold and then add it to an excel Cell. So it looks like:

Excel Cell

What i tried:

Inside Excel using a range:

excelSheet.get_Range("A" + 16, "D" + 16).Font.Bold = true;

But this makes everything bold....

Then i tried:

"<b>" + text + "<b>"

and had no success.

So i make something wrong. Any help or advise would be great and thanks for your time.

EDIT: Working C# Code:

Excel.Range range1 = excelSheet.Range["A36"];
Excel.Characters test = range1.get_Characters(21, 4);
test.Font.Bold = true;
opelhatza
  • 244
  • 1
  • 4
  • 19

1 Answers1

6

You can't make parts of a string bold, but you can make characters in a cell bold:

Sub BoldAndBeautiful()
    With Range("A68")
      .Value = "Test 1234 Test"
      .Characters(Start:=1, Length:=4).Font.FontStyle = "bold"
      .Characters(Start:=11, Length:=4).Font.FontStyle = "bold"
    End With
End Sub

Basically do it in two steps. First put the text in the cell using the Value of the Range object and then apply the font using the Characters of the Range object.

Note that some systems use an "HTML-type" method to format parts of a string, that is they embed markers to define where formatting starts and stops. Excel is not one of them.

Just adapt this to your c# code.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99