Highlight chart title in Word Document - vba

I want to highlight Chart title text (few characters).
ReplaceWhat = "Before"
ReplaceWith = "After"
If chrt.HasTitle Then
pos = InStr(chrt.ChartTitle.Characters.Text, ReplaceWhat)
If pos > 0 Then
chrt.ChartTitle.Characters(pos, Len(ReplaceWhat)).Text = ReplaceWith
chrt.ChartTitle.Characters(pos, Len(ReplaceWhat)).Font.Bold = msoTrue
End If
End If
I am able to find and replace Chart title text and also able to make Bold.
For changing text color
chrt.ChartTitle.Format.TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)
Not found any solution for highlighting title.

Maybe try this:
Range("A1:A5").HighlightColorIndex = wdYellow
Theres an Property of Range called Range.HighlightColorIndex = //wdColorIndex//

Related

Coloring string in Word document when transfering it from Excel using VBA

Sorry for maybe unintelligible title but i cant find out how to write this better.
I've got Excel VBA code that transforms the sheet from Excel for a version to print in a Word. The problem is that when a specific cell in a row in Excel is red colour or is equal to "ENG_AT" text I need to color content of cell which will be printed.
Here you got the original working code sample.
ElseIf Not IsEmpty(calArray(i, 3)) And IsEmpty(calArray(i, 2)) Then
'item
styleName = "N1"
headerText=calArray(i, 12)
.Rows(t).Borders(-3).LineStyle = 7
calArray(i,12) is cell that is going to print.
And this is part where I tried to add my statement
ElseIf Not IsEmpty(calArray(i, 3)) And IsEmpty(calArray(i, 2)) And calArray(i, 16) = "ENG_AT" Then
'item
styleName = "N1"
Dim Text2 As String
Text2 = UCase(calArray(i, 12)
Text2.Interior.Color = RGB(0, 0, 250)
headerText = Text2
'.Font.Underline = True
.Rows(t).Borders(-3).LineStyle = 7
All I've done is only to uppercase a letters, rest is not working.
I would be really happy if someone could explain to me how can I change color of destination cell in Word table or just underline this text.
Thank you in advance for help.
Not knowing the specific variables you are using in your solution, the following is just a generic way to assign a color to the text in a Word table cell.
ActiveDocument.Tables(1).rows(1).Cells(3).Range.Font.ColorIndex = wdRed

Change Border Color of a Range Without Changing the Linestyle/Weight

I have a nicely formatted range of cells with different border line weights (some of them are medium thickness and some of them are thin, in no particular pattern). I want to run a macro that changes the color of the borders to grey, but every time I do it, it changes all of the border weights to xlThin automatically. I want to keep the original line thickness so I don't have to go through and change the respective ones back to xlMedium, which would be tedious.
Can someone help me out? Is this possible?
The code I currently have is simple, and it changes the color correctly. It just also changes the line weight automatically, even though I don't specify the weight or linestyle at all:
Range("NamedRange").Borders.Color = RGB(150, 150, 150)
This, on my Excel 2016, will only change the color of the cell border, without changing the size:
Sub changeColorOnly()
Dim rng As Range, cel As Range
Set rng = Range("C6:C9")
For Each cel In rng
cel.Borders.Color = RGB(150, 150, 150)
Next cel
End Sub
Does it still change the size for you?
Edit: Hm, I suspect there's something else going on in your code, as I can recolor a named range without it affecting the borders. However, just because I was already working on another alternative, you could also use these subs (and tweak as necessary)
Dim brdrTop, brdrLeft, brdrRight, brdrBtm, brdrInside
Sub changeColor()
saveBorderSize Range("myNamedRange")
Range("MyNamedRange").Borders.Color = RGB(150, 150, 150)
resetBorderSize Range("myNamedRange")
End Sub
Private Sub saveBorderSize(cel As Range)
brdrTop = cel.Borders(xlEdgeTop).Weight
brdrLeft = cel.Borders(xlEdgeLeft).Weight
brdrRight = cel.Borders(xlEdgeRight).Weight
brdrBtm = cel.Borders(xlEdgeBottom).Weight
brdrInside = cel.Borders(xlInsideHorizontal).Weight
End Sub
Private Sub resetBorderSize(cel As Range)
cel.Borders(xlEdgeTop).Weight = brdrTop
cel.Borders(xlEdgeLeft).Weight = brdrLeft
cel.Borders(xlEdgeRight).Weight = brdrRight
cel.Borders(xlEdgeBottom).Weight = brdrBtm
cel.Borders(xlInsideHorizontal).Weight = brdrInside
End Sub
Try .Borders.Color = RGB(216,216,216)
I ran the below script to try to identify the closest color to normal gridlines. My eyes are not great so check it out yourself to find the best color. BTW I agree it makes no sense that MS overrides the border color defying reason. Angry employees and too much bureaucracy - that's my theory.
Sub borcol()
Dim i As Integer
For i = 1 To 250
ActiveCell.Borders.Color = RGB(i, i, i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
To change the cell border color in a loop, using the enum value for each border makes it easy to loop through them.
This code will change the border color of the selected cell.
If there's no line the MsgBox will indicate its value.
Sub CellBorderColour()
Dim MyBorder(5 To 12) As String
Dim i As Integer
MyBorder(5) = "xlDiagonalDown"
MyBorder(6) = "xlDiagonalUp"
MyBorder(7) = "xlEdgeLeft"
MyBorder(8) = "xlEdgeTop"
MyBorder(9) = "xlEdgeBottom"
MyBorder(10) = "xlEdgeRight"
MyBorder(11) = "xlInsideVertical"
MyBorder(12) = "xlInsideHorizontal"
For i = 5 To 12
With Selection.Borders(i)
If .LineStyle > 0 Then
.Color = RGB(100, 100, 100)
Else
MsgBox ("Borders(" & MyBorder(i) & ").LineStyle is: " & .LineStyle)
End If
End With
Next i
End Sub

Manipulating text with symbols in PowerPoint using VBA

I am trying to use VBA to manipulate text in a PowerPoint.
I have formatted text in a frame with greek symbols, superscript and subscript.
I want to divide that text into two frames.
For example I use something like this:
Dim frame1Text As String
Dim frame2Text As String
Set frame1 = ActivePresentation.Slides(1).Shapes(1).TextFrame
Set frame2 = ActivePresentation.Slides(1).Shapes(2).TextFrame
frame1Text = frame1.TextRange.Text
frame2Text = Right(frame1Text, Len(frame1Text) - 10)
frame1Text = Left(frame1Text, Len(frame1Text) - Len(frame2Text))
frame1.TextRange.Text = frame1Text
frame2.TextRange.Text = frame2Text
As a result there are symbols and formatting lost. Is there any way to make it better?
Thanks for any help.
If possible, I would duplicate the shape and then delete what you don't want in the text. That way all formatting will be retained, character-by-character. Something along the lines of this:
Option Explicit
Sub CopyText()
Dim oShp1 As Shape
Dim oShp2 As Shape
Set oShp1 = ActivePresentation.Slides(1).Shapes(1)
oShp1.Copy
ActiveWindow.View.Slide.Shapes.Paste
Set oShp2 = ActivePresentation.Slides(1).Shapes(ActivePresentation.Slides(1).Shapes.Count)
With oShp1.TextFrame.TextRange
.Text = Left(.Text, 10)
End With
With oShp2.TextFrame.TextRange
.Text = Right(.Text, Len(.Text) - Len(oShp1.TextFrame.TextRange.Text))
End With
End Sub

VBA code in excel to made text between tags bold

I have a csv file which includes the html tags < b > and <\ b > to signify bold text. (I.e several words between these tags, in a longer block of text within the cell, should be bold). Is there a way using vba code in excel to strip the tags, and make the text between the tags bold?
Note - There are sometime multiple sets of tags within a given cell.
This should do what you want:
Sub BoldTags()
Dim X As Long, BoldOn As Boolean
BoldOn = False 'Default from start of cell is not to bold
For X = 1 To Len(ActiveCell.Text)
If UCase(Mid(ActiveCell.Text, X, 3)) = "<B>" Then
BoldOn = True
ActiveCell.Characters(X, 3).Delete
End If
If UCase(Mid(ActiveCell.Text, X, 4)) = "</B>" Then
BoldOn = False
ActiveCell.Characters(X, 4).Delete
End If
ActiveCell.Characters(X, 1).Font.Bold = BoldOn
Next
End Sub
Currently set to run on the activecell, you can just plop it in a loop to do a whole column. You can easily adapt this code for other HTML tags for Cell formatting (ie italic etc)
This was in the cell I tested on (minus the space after <): Sample < b>Te< /b>st of < B>bolding< /B> end
The result was: Sample Test of bolding end
Hope that helps

How i can add to existing cell additional text with modified font options in Microsoft Excel VBA

For example Microsoft VBA:
ActiveCell = ActiveCell & <Some Text i want to add with option Size = 20>
How i can implement that description inside "<>" brackets
You want to change the ActiveCell.Characters().Font property
Dim CurrentText, SomeText
Dim CurrentTextLen, SomeTextLen
CurrentText = ActiveCell.Value
CurrentTextLen = Len(CurrentText)
SomeText = "Some Text i want to add with option Size = 20"
SomeTextLen = Len(SomeText)
ActiveCell.Value = CurrentText & SomeText
With ActiveCell.Characters(Start:=CurrentTextLen + 1, Length:=SomeTextLen).Font
.Size = 20
End With
For this, you need to know where your <> text starts (i.e. the length of the ActiveCell current contents, plus one)
You will also need the length of your <> text (i.e. the length of the <> text)