如何使用 VBA 快速將 PowerPoint 中特定文字進行 Highlight
你是否曾經需要快調整 PowerPoint 簡報中的文字?我們製作報告時常碰到要調整字體顏色加粗進行 Highlight,手動調整每張投影片可能非常耗時,尤其是當你的簡報很大一份的時候。在本篇文章中,會告訴你如何使用 VBA 程式碼快速調整 PowerPoint 簡報中的文字。我將提供可以調整字體顏色、加粗文字並格式化特定標記內的文字的程式碼。
Sub FormatShapesOnAllSlides()
Dim sld As Slide
Dim shp As Shape
Dim lastSlide As Long
Dim userResponse As Integer
' Activate the first slide in the presentation
ActiveWindow.View.GotoSlide 1
' Get the index of the last slide in the presentation
lastSlide = ActivePresentation.Slides.Count
' Loop through all slides in the presentation
For Each sld In ActivePresentation.Slides
' Select all shapes on the slide
For Each shp In sld.Shapes
shp.Select (False)
Next shp
' Call BoldAndColorText function to format the selected shapes
Call BoldAndColorText_All("@", 234, 115, 23) 'low light
Call BoldAndColorText_All("^", 61, 165, 217) 'high light
' Advance to the next slide
If sld.slideIndex < lastSlide Then
ActiveWindow.View.GotoSlide sld.slideIndex + 1
Else
MsgBox "End of presentation reached."
End If
Next sld
End Sub
Sub BoldAndColorText_All(mark As String, colorR As Integer, colorG As Integer, colorB As Integer)
Dim shp As Shape
Dim tbl As Table
Dim tRange As TextRange
Dim cell As cell
Dim i As Long
Dim j As Long
Dim markCount As Long ' Counter for the number of marks
' Loop through all selected shapes and tables
For Each shp In ActiveWindow.Selection.ShapeRange
' Check if the shape contains text
If shp.HasTextFrame Then
Set tRange = shp.TextFrame.TextRange
' Check if there are any * enclosed text
i = InStr(tRange.Text, mark)
While i > 0
j = InStr(i + 1, tRange.Text, mark)
If j > 0 Then
' Select the enclosed text and set bold and color
tRange.Characters(i + 1, j - i - 1).Font.Bold = True
tRange.Characters(i + 1, j - i - 1).Font.Color.RGB = RGB(colorR, colorG, colorB) 'Change the RGB values to your desired color
' Delete the * characters
tRange.Characters(j, 1).Delete
tRange.Characters(i, 1).Delete
End If
i = InStr(j + 1, tRange.Text, mark)
Wend
End If
' Check if the shape is a table
If shp.HasTable Then
Set tbl = shp.Table
' Loop through all cells in the table
For r = 1 To tbl.Rows.Count
For c = 1 To tbl.Columns.Count
' Set reference to current cell
Set cell = tbl.cell(r, c)
Set tRange = cell.Shape.TextFrame.TextRange
i = InStr(tRange.Text, mark)
While i > 0
j = InStr(i + 1, tRange.Text, mark)
If j > 0 Then
' Select the enclosed text and set bold and color
tRange.Characters(i + 1, j - i - 1).Font.Bold = True
tRange.Characters(i + 1, j - i - 1).Font.Color.RGB = RGB(colorR, colorG, colorB) 'Change the RGB values to your desired color
' Delete the * characters
tRange.Characters(j, 1).Delete
tRange.Characters(i, 1).Delete
End If
i = InStr(j + 1, tRange.Text, mark)
Wend
Next c
Next r
End If
Next shp
End Sub
程式碼說明
我們在這篇文章中使用的程式碼可以調整字體顏色,加粗文字並只調整特定標記內的文字。此程式碼中使用的標記是 '@@' 用於 LowLight,'^^' 用於 Highlight。
程式碼首先將從頭開始檢查簡報中的所有投影片,選擇投影片上的所有 shape (文字框/圖形),並調用 BoldAndColorText_All 函數來調整所選 shape 內被標記得文字。
BoldAndColorText_All 函數檢查所有選定的 shape 和表格,並檢查它們是否包含指定的標記。如果找到標記,則選擇標記內的文字並將其調整為粗體和指定顏色。然後從文字中刪除標記。
結論
使用 VBA 程式碼調整 PowerPoint 中我們要強調的字會節省大量時間,讓整個過程變得更快速且高效率。小編實際應用,只要在打字得時候使用 @@ 跟 ^^ 符號標記即可,之後一次使用此 VBA 馬上一次調整完成,超級省時又方便。
留言
張貼留言