Skip to main content

Change background color

Sets blue color as the background of even indexed slides, if it is odd sets an image.

(function()
{
let presentation = Api.GetPresentation();
let slidesCount = presentation.GetSlidesCount();

//Create background color and solid fill
let backgroundColor = Api.CreateRGBColor(0, 150, 255);
let colorFill = Api.CreateSolidFill(backgroundColor);

//Create blip fill from image
let imageUrl = "https://static.onlyoffice.com/assets/docs/samples/img/presentation_sky.png";
let imageFill = Api.CreateBlipFill(imageUrl, "stretch");

//Iterate through each slide
for (let i = 0; i < slidesCount; i++) {
let slide = presentation.GetSlideByIndex(i);
if(i % 2 == 0) {
//Set color as background if index of slide is even
slide.SetBackground(colorFill);
} else {
//Set image as background if index of slide is odd
slide.SetBackground(imageFill);
}
}
})();

Methods used: GetPresentation, GetSlidesCount, CreateRGBColor, CreateSolidFill, CreateBlipFill, GetSlideByIndex, SetBackground

Reference Microsoft VBA macro code​

Sub SetSlideBackground()
Dim slide As slide
Dim slideIndex As Integer
Dim slidesCount As Integer
Dim backgroundColor As Long
Dim imageUrl As String
Dim picture As Object
Dim backgroundShape As Object

' Get the active presentation
Dim presentation As presentation
Set presentation = ActivePresentation

' Get the number of slides
slidesCount = presentation.Slides.Count

' Set the RGB color for background (R = 0, G = 150, B = 255)
backgroundColor = RGB(0, 150, 255)

' Set the image URL
imageUrl = "https://static.onlyoffice.com/assets/docs/samples/img/presentation_sky.png"

' Loop through each slide
For slideIndex = 1 To slidesCount
Set slide = presentation.Slides(slideIndex)

' Check if the slide index is even or odd
If slideIndex Mod 2 = 0 Then
' Set a solid color background if the index is even
slide.FollowMasterBackground = msoFalse
slide.Background.Fill.BackColor.RGB = backgroundColor
Else
' Set an image as the background if the index is odd
slide.FollowMasterBackground = msoFalse

' Delete any previous background shapes if they exist
On Error Resume Next
slide.Shapes("background_image").Delete
On Error GoTo 0

' Add the image to the slide
Set picture = slide.Shapes.AddPicture(imageUrl, _
MsoTriState.msoFalse, _
MsoTriState.msoTrue, _
0, 0)

' Stretch the image to cover the entire slide
picture.LockAspectRatio = MsoTriState.msoFalse
picture.Width = slide.Master.Width
picture.Height = slide.Master.Height
picture.Top = 0
picture.Left = 0
picture.Name = "background_image"
End If
Next slideIndex

MsgBox "Slide backgrounds have been updated!"
End Sub

Result​