Word VBA change font size of just some of the characters in a short text string.

User avatar
DocAElstein
5StarLounger
Posts: 604
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Word VBA change font size of just some of the characters in a short text string.

Post by DocAElstein »

Hi
As ever I am pretty dumb at getting Word VBA code. ( I think what messes me up is that the macro recorder won’t let me select things when its running, which is restricting what I can do with it!? )

So lets say, for example, I have a word, Burgundy in a short piece of text.
Image

I then highlight the word Burgundy
Image

I now run this macro, and it almost gives me what I want:

Code: Select all

 Sub Makro9BBBurgundy()   
    With Selection
     .Font.Color = 1704076
     .Text = "[color=""#8C001A""] " & .Text & " [/color]"
     .Collapse Direction:=wdCollapseEnd
    End With
End Sub
It puts some BB code tags around that selected word and colours it all
Image

That is not quite what I finally want.
I would like the code to also reduce the size of those tags in the word document , lets say to font size 8 , so that finally I have it something like this
Image
BB Code Tags font site to 8.JPG
Can someone help me on this last bit

Thanks
Alan


( BTW, if I could make those code tags somehow translucent as well it would be even better, but that’s less important )
Basically I want those code tags to be there and visible to me, but I would like them to be a bit in the background so that I can read the text normally a bit easier. )
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
HansV
Administrator
Posts: 78588
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word VBA change font size of just some of the characters in a short text string.

Post by HansV »

Code: Select all

Sub Makro9BBBurgundy()
    Dim Text1 As String
    Dim Text2 As String
    Text1 = "[color=""#8C001A""]"
    Text2 = "[/color]"
    With Selection
        .InsertBefore Text:=Text1
        .InsertAfter Text:=Text2
        ActiveDocument.Range(Selection.Start, Selection.Start + Len(Text1)).Font.Size = 8
        ActiveDocument.Range(Selection.End - Len(Text2), Selection.End).Font.Size = 8
        .Font.Color = 1704076
        .Collapse Direction:=wdCollapseEnd
    End With
End Sub
Best wishes,
Hans

User avatar
DocAElstein
5StarLounger
Posts: 604
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Word VBA change font size of just some of the characters in a short text string.

Post by DocAElstein »

Thanks Hans, that's great - just what I wanted.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(