How change the background color in a Word doc? - vba

I wanted to know how can I change the background color of the text in Word on all my doc. For exemple I got some text with blue/red/pink background color and I want that the bg color of all my doc white.
I got and exemple for the font color :
Sub color()
'
' color Macro
'
'
Dim Plage As Object, Wrd As Object
Set Plage = ActiveDocument.Content.Words
For Each Wrd In Plage
If Wrd.Font.color = RGB(0, 0, 255) Then _
Wrd.Font.color = RGB(128, 128, 128)
Next Wrd
End Sub
But I don't know how to apply this VBA code for the background color.
Maybe with Document.Background property ?

There are three background color settings you have to deal with:
You can have text marked with a Text Highlight. This can be modified with Range.HighlightColorIndex. To remove the highlight, use wdNoHighlight
You can have Shading. Shading is set on paragraph level can can be modified with Range.Shading.BackgroundPatternColor. To remove it, use wdColorAutomatic
You can have set the background color of a whole document. This can be modified using Background.Fill of the document. Either set the ForeColor to white, or set the visible-property to false.
To clean everything at once, use something like this:
Sub RemoveBackgroundColor()
With ActiveDocument.Content
.HighlightColorIndex = wdNoHighlight
.Shading.BackgroundPatternColor = wdColorAutomatic
End With
ActiveDocument.Background.Fill.Visible = msoFalse
End Sub

Related

Loop though object image to change background color

I'm trying to loop through all the images starts from the 2nd page in an MS Word file and change the background colors to white.
currently, all the Word content is changing the background colors and not only the Images.
How can I loop only the through object image?
My code:
Sub RemoveBackgroundColor()
With ActiveDocument.Content
.HighlightColorIndex = wdWhite
.Shading.BackgroundPatternColor = wdColorWhite
End With
ActiveDocument.Background.Fill.Visible = msoFalse
End Sub

Changing Font Outline color inside a Table Cell with VBA using Application.CommandBar.ExecuteMSO

Win10x64 Office 365 PPT v 16.0.12325.202080 64-bits
I need to show a character with a yellow font color but a black outline, for readability purposes. This character goes inside a Powerpoint table Cell.
The following link has a method that I'm currently using that consists of creating a dummy shape, adding text to it, modify the shape's textframe2 textrange font line properties and then copying it and pasting it back to the table cell.
http://www.vbaexpress.com/forum/archive/index.php/t-43787.html
This was asked 8 years ago, but I'm currently seeing the same behaviour where we can't directly manipulate the textframe2 wordart format of text inside a cell. The program doesn't show an error but doesn't work.
I have given up on trying to modify the textrame2 textrange font line properties directly from VBA.
I have managed to get it to activate the font outline color using
Application.CommandBars.ExecuteMso ("TextOutlineColorPicker")
After it's activated I thought I could modify the textframe2 textrange font line properties, but it still doesn't work.
Is there an Application.CommandBars idMso for changing the font outline color and font outline line width inside a table cell?
Or another other than pasting the formatted text inside a table cell.
Edit:
Adding an image to illustrate what I mean by text color and text outline color and the menu used to show them in red circle:
Edit2
Added another snapshot to exemplify a character inside a cell with black outline and a character inside a cell without an outline
Thanks
Here's an example to access a Table on a given slide and change one cell's attributes. The example slide I'm using looks like this
The code itself creates a function that allows you to select a table from a particular slide, and a single cell within the table and highlight it.
Option Explicit
Sub test()
HighlightTableCell 1, 2, 3
End Sub
Sub HighlightTableCell(ByVal slideNumber As Long, _
ByVal thisRow As Long, _
ByVal thisCol As Long)
Dim theSlide As Slide
Set theSlide = ActivePresentation.Slides(slideNumber)
Dim shp As Shape
For Each shp In theSlide.Shapes
If shp.Type = msoTable Then
Dim theTable As Table
Set theTable = shp.Table
With theTable.Cell(thisRow, thisCol)
With .Shape.TextFrame2.TextRange.Characters.Font.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 255, 0)
.Transparency = 0
.Solid
End With
With .Shape.TextFrame2.TextRange.Characters.Font.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
End With
End If
Next shp
End Sub
This question should be answered by Microsoft Office developers.
Currently, to overcome this bug-some situation, I think, copying the formatted text outside the table and pasting it into a table cell is the only work-around for this trouble.
As you mentioned, according to John Wilson, one of the most brilliant PowerPoint MVPs(http://www.vbaexpress.com/forum/archive/index.php/t-43787.html), if we copy the text from a textbox or shape that is located outside of the table, the format of the text can be preserved even for the text in a table cell.
Option Explicit
Sub test()
Dim shp As Shape, tshp As Shape
Dim sld As Slide
Dim tbl As Table
Dim r%, c%
If ActiveWindow.Selection.Type = ppSelectionNone Then MsgBox "Select a table first.": Exit Sub
Set shp = ActiveWindow.Selection.ShapeRange(1)
Set sld = shp.Parent
'add a temporary textbox for copying the formatted text into a cell
Set tshp = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 541, 960, 540)
tshp.Visible = False
Set tbl = shp.Table
For r = 1 To tbl.Rows.Count
For c = 1 To tbl.Columns.Count
'1) cell -> 'tshp'
tbl.Cell(r, c).Shape.TextFrame2.TextRange.Copy
tshp.TextFrame2.TextRange.Paste
'2) outline the text in 'tshp'
With tshp.TextFrame2.TextRange.Font.Line
.Visible = msoTrue
.Weight = 0.2
.ForeColor.RGB = RGB(255, 127, 127)
End With
'3) 'tshp' -> cell
tshp.TextFrame2.TextRange.Copy
tbl.Cell(r, c).Shape.TextFrame2.TextRange.Paste
'// the code below doesn't work
'With tbl.Cell(r, c).shape.TextFrame2.TextRange.Characters.Font.Line
'With tbl.Cell(r, c).shape.TextFrame2.TextRange.Font.Line
' .Visible = msoTrue
' .Weight = 0.5
' .ForeColor.RGB = RGB(255, 127, 127)
'End With
Next c
Next r
'remove the tempoarary textbox
tshp.Delete
End Sub
The above snippet creates a temporary textbox on left-top area of the slide and applies the outlined text format. Then, it copies the content of each cell to the temporary textbox and copy/paste the formatted text back to the cell. By using this method, we can apply the outlined text format to the text in a cell.

Using a macro to create conditional formatting in Word as per Excel

I have an Excel doc that has conditional formatting that changes the background colour of a cell, dependant upon the specific [dropdown] text that is selected. E.g Yes, changes cell background to Green, No to Red, Unknown to Yellow and Not applicable to Grey.
All easy stuff.
I then need to mailmerge to a Word doc to populate the Word doc with the Excel table - the Word doc also has other non-excel related text.
As conditional formatting of the cells does not come across, I have used the under noted code in a macro to change the background colour in Word. It runs, but seems after the first loop, it seems to crash with the error - Runtime error 5907 - there is no table at this location.
the line in the code r.Cells(1).Shading.BackgroundPatternColorIndex = backgroundColor is highlighted yellow.
My level of coding is basic so I don't know what's going wrong.
If anyone is able to offer an insight to a simple solution, I would appreciate it.
Thank
Dim r As Range
Sub UBC()
color "No", wdRed
color "Yes", wdGreen
color "Unknown", wdYellow
color "Not Applicable", wdGray50
End Sub
Function color(text As String, backgroundColor As WdColorIndex)
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:=text, MatchWholeWord:=True, Forward:=True) = True
r.Cells(1).Shading.BackgroundPatternColorIndex = backgroundColor
Loop
End With
End Function
It's possible (likely) that Word is finding a character combination outside a table cell. Safest is to test whether the found term is actually in a table. (Note: I've also put the variable declaration Dim r inside the Function...)
Sub UBC()
color "No", wdRed
color "Yes", wdGreen
color "Unknown", wdYellow
color "Not Applicable", wdGray50
End Sub
Function color(text As String, backgroundColor As WdColorIndex)
Dim r As Word.Range
Set r = ActiveDocument.content
With r.Find
Do While .Execute(findText:=text, MatchWholeWord:=True, Forward:=True) = True
If r.Tables.Count > 0 Then
r.Cells(1).Shading.BackgroundPatternColorIndex = backgroundColor
End If
Loop
End With
End Function

How to create a custom textbox with set text, font type, font size, font colour in Powerpoint?

I need to create a custom textbox in the active slide with set text, font type, font size, font colour, and bold and italics feature in Powerpoint 2007 using a macro.
I am currently using this code:
Sub InsertTextBox()
Set myDocument = ActivePresentation.Slides(1)
Set newTextBox = myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
100, Top:=100, Width:=541.44, Height:=43.218)
With newTextBox.TextFrame.TextRange
.Text = "Slide Title"
.Font.Size = 24
.Font.Name = "Arial"
.Font.Colour = RGB(107, 107, 107)
End With
End Sub
Its throwing an exception in .Font.Colour saying that this property is not associated with the object. Also i need to incorporate a feature for bold and italics..Plz help me on this
You want
.Font.Color.RGB = RGB(107, 107, 107)
And as Kazjaw points out, it's .Color rather than .Colour
You can use Find on a range to locate a string within a textrange and return a new textrange consisting of just the found text:
With newTextBox.TextFrame.TextRange
With .Find("Slide Title")
.Font.Bold = True
End With
End With
In practice, you'd want to use Instr to verify that the text you're looking for is indeed in the text box.

Apply Font Formatting to PowerPoint Text Programmatically

I am trying to use VBA to insert some text into a PowerPoint TextRange, I use something like this:
ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"
However, I can't figure out how to apply bold, italic and underline programmatically (I don't see a .RichText property or something similar).
What I have is some simple HTML text with bold, italic and underlined text I would like to convert over.
How to do this?
This is easily accomplished by using the TextRange's Characters, Words, Sentences, Runs and Paragraphs objects and then it's Font object to set Bold, Underline and Italic (amongst other properties). For example:
Sub setTextDetails()
Dim tr As TextRange
Set tr = ActiveWindow.Selection.SlideRange.Shapes(1).TextFrame.TextRange
With tr
.Text = "Hi There Buddy!"
.Words(1).Font.Bold = msoTrue
.Runs(1).Font.Italic = msoTrue
.Paragraphs(1).Font.Underline = msoTrue
End With
End Sub
Try looking at MSDN's documentation on the TextRange object. It contains samples of how to access the Font properties of the TextRange object.
EDIT: You can access things like Bold and Italics programmatically in this manner:
TextRange.Font.Bold = msoTrue
EDIT EDIT: There are several methods by which you can select only certain text in a text range. See the following:
Characters Method
Lines Method
Paragraphs Method
Words Method
According to the sames from this link, you can select a portion of the text using one of these methods and set the font programmatically. For example:
Application.ActiveDocument.Pages(1).Shapes(2) _
.TextFrame.TextRange.Words(Start:=2, Length:=3) _
.Font.Bold = True
That example was taken from the Words Method link.
In addition to the above answer, you should try to name the objects you'll be changing, since selecting them in the middle of a presentation could make PowerPoint act oddly. Create a new TextRange object and set it like this.
dim mytextrange As TextRange
Set mytextrange = ActiveDocument.Pages(1).Shapes(2).TextFrame.TextRange
mytextrange.Words...
Here is how you can do it to change the font of a specific text:
Sub changeFont()
Dim oPresentation As Presentation
Dim oSlide As Slide
Dim oShape As Shape
Dim stringSearched As String
stringSearched = "something"
'all opened presentations
For Each oPresentation In Presentations
'all slide in them
For Each oSlide In oPresentation.Slides
'all shapes (anything)
For Each oShape In oSlide.Shapes
'only those that contain text
If oShape.HasTextFrame Then
If InStr(oShape.TextFrame.TextRange.Text, stringSearched) > 0 Then
'here you need to define where the text ends and start
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Underline = msoTrue
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Italic = msoFalse
End If
End If
Next
Next
Next
End Sub