I am very new to the macro. I am looking forward for the code for printing some part of string in italic and rest in bold.
e.g.
Hi There, congratulation for new assignment.
I am able to print the entire string in either bold or in Italic but not able to find the code which can fulfil my requirement.
Also splitting the text in to the dummy variable and joining those post applying the formatting seems to be a lengthy option.
Play around with this, it will do what you used for your example.
Sub SetBoldAndItalicInCell()
ActiveCell.FormulaR1C1 = "Hi There, congratulation for new assignment."
With ActiveCell.Characters(Start:=1, Length:=3).Font.FontStyle = "Regular"
ActiveCell.Characters(Start:=4, Length:=5).Font.FontStyle = "Bold Italic"
ActiveCell.Characters(Start:=9, Length:=24).Font.FontStyle = "Regular"
ActiveCell.Characters(Start:=33, Length:=11).Font.FontStyle = "Bold"
ActiveCell.Characters(Start:=44, Length:=1).Font.FontStyle = "Regular"
End Sub
Related
I have word document with different styles like heading1, heading2, normal, bodytext etc. I'm trying to write a VBA macro which will first modify the styles as per requirement and then apply those styles across the document accordingly. See below, I'm changing the font size first for style=normal and heding1 and then want to apply it for all paragraphs where style=normal is already defined. So it will update the style for existing heading1 and normal separately.
NOTE: I want to do it with style only. I can do it using for loop by going each paragraph one by one and then find what is the style of that paragraph and then apply the style accordingly AND it is working perfectly if I do in this way but in my document I have 60000 paragraphs and so it is taking long time like 10 hours. So, Im looking for a way to do it for each different styles in one shot. Please let me know if there is any way to do it.
ActiveDocument.Styles(wdStyleNormal).Font.Name = "Arial"
ActiveDocument.Styles(wdStyleNormal).Font.Size = 12
ActiveDocument.Styles(wdStyleHeading1).Font.Name = "Arial"
ActiveDocument.Styles(wdStyleHeading1).Font.Size = 20
ActiveDocument.UpdateStyles
I have tried and ended up with this solution. I hope it will work for you.
Sub ChangeStyle()
With ActiveDocument.Styles("wdStyleNormal").Font
.Color = wdColorAqua 'wdColorRed
.Name = "Arial"
.Size = 12
End With
With ActiveDocument.Styles("wdStyleNormal")
.AutomaticallyUpdate = True
End With
End Sub
I am trying to bold specific words based on a string being read into vba code.
I have the string in read into vba and now want to pick out a word or words to bold them.
If the following was read into vba code: The boy ran down the street.
In the above sentence, what code would I use if I wanted to bold just the word boy? What would I use if I wanted to bold the words boy and the?
I have tried the following code but do not know how to modify it to my specific case.
https://code.adonline.id.au/vba-format-text-microsoft-word/
In short, I want code that will read in a string, search that string for x amount of words, and bold said words to be later exported.
Let me know if you need any more details and what I am trying to accomplish.
Excel is pretty good at formatting whole cells, but it's not very good at formatting parts of cells. One way that I've been able to do this is to turn the text into very basic HTML and then paste the text into a cell. Here's an example.
Public Sub BoldCertainWords()
Dim sSentence As String
Dim dobj As DataObject
Set dobj = New DataObject
sSentence = "The boy ran down the street"
sSentence = Replace$(sSentence, "boy", "<strong>boy</strong>")
sSentence = Replace$(sSentence, "the", "<strong>the</strong>")
dobj.SetText "<html>" & sSentence & "</html>"
dobj.PutInClipboard
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End Sub
You need to set a reference to the MS Forms 2.0 library to get the DataObject if you don't already have it.
Note also that case matters. This will bold the but not The. You could repeat for typical capitalization or get clever with how you find the words and build the HTML string.
Also note that this particular PastSpecial method is Worksheet method, not a Range method. You have to select the range first.
Hello Developers and other, :)
I'm developing a macro to create a lot of button automatically, and I wants to give them a specific background color.
With Worksheets("Sheet 1")
Set r_BtnRange = .Range("O3:O4")
For Each r_Cell In r_BtnRange.Cells
Set b_Btn = .Buttons.Add(r_Cell.Left, r_Cell.Top, _
r_Cell.Width, r_Cell.Height)
With b_Btn
.Caption = "Email End-User"
.OnAction = "Email_EndUser"
.Font.Color = RGB(255, 255, 255)
'.BackColor = 16711680
End With
Next
End With
The .Button.Add return a Button Object, witch has no BackColor Proprety (according to this: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.button?view=excel-pia and the fact I receive an error when tying to uncomment the line)
please see Here
I could use a Shape to do it, but I would know if there is really any method to do it (btw, had to disable it and modify multiple propriety of it.
Thanks a lot for your answers,
Antoine
I have a cell that contains the text "Collar Lot #" and I would like to append a string onto the end of it that is not bold. An example would be "Collar Lot # 23456\34567\45678". My code attempt below makes the entire string bold, resulting in "Collar Lot # 23456\34567\45678"
With Workbooks(ThisWorkbook.name).Worksheets(1)
...
.Cells(7, 5).value = "Collar Lot # " & "23456\34567\45678"
End With
How would I make sure the "23456\34567\45678" was not bold?
You would need to specify the formatting based on the position of the characters by using something like:
.Cells(7, 5).Characters(Start:=1, Length:=13).Font.FontStyle = "Bold"
.Cells(7, 5).Characters(Start:=14).Font.FontStyle = "Regular"
The Record Macro button on the Developer Tab of the Ribbon is very useful for finding out something like this.
I am trying to change the font color for specific words with-in a cell. I don't want to change all of the text to the color just specific words. I am using an OfficeWriter ExcelTemplate.
You could use ExcelApplication to Post Process your OfficeWriter Template file.
You want to get a handle on what is referred to as a CharacterRun See the OfficeWriter documentation.
Your code would look something like:
ExcelTemplate xlt = new ExcelTemplate()
//Process Template
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Open(xlt)
Worksheet ws = wb.Worksheets[0];
Cell cellA1 = ws.Cells["A1"];
cellA1.Value = "Test";
CharacterRun charRun = cellA1.GetCharacters(0,5);
charRun.Font.Color = Color.Red
In the formula bar, select the words you want to change the color of, then change the text color.