在PowerPoint 2003下运行通过,使用时只要随便新建一个宏,把它自动生成的代码全删去,粘贴下面这段代码。然后运行 ReColor 这个宏就可以了。
' The macro to excute Sub ReColor() Dim sld As Slide Dim sh As Shape For Each sld In ActivePresentation.Slides For Each sh In sld.Shapes Call ReColorSH(sh) Next Next End Sub Function ReColorSH(sh As Shape) Dim ssh As Shape If sh.Type = msoGroup Then ' when the shape itself is a group For Each ssh In sh.GroupItems Call ReColorSH(ssh) ' the recursion Next '改变公式中文字的颜色为黑色,不知如何设置为其他颜色 ElseIf sh.Type = msoEmbeddedOLEObject Then ' recolor the equation If Left(sh.OLEFormat.ProgID, 8) = "Equation" Then sh.PictureFormat.ColorType = msoPictureBlackAndWhite sh.PictureFormat.Brightness = 0 sh.PictureFormat.Contrast = 1 'sh.Fill.Visible = msoFalse End If '改变文本框中文字的颜色,可自己设定 ElseIf sh.HasTextFrame Then ' /* 当前幻灯片中的当前形状包含文本. */ If sh.TextFrame.HasText Then ' 引用文本框架中的文本. Set trng = sh.TextFrame.TextRange ' /* 遍历文本框架中的每一个字符. */ For i = 1 To trng.Characters.Count ' 这里请自行修改为原来的颜色值 (白色). 'If trng.Characters(i).Font.Color = vbWhite Then ' 这里请自行修改为要替换的颜色值 (黑色). trng.Characters(i).Font.Color = vbBlack 'End If Next End If End If End Function
可是接下来问题又来了,老师为了排版,把很多公式与文字及图片组合起来,而且有时还不止一层组合,这样就必须递归遍历来找出其中的公式(如果直接全部打成黑的,很多图片就会变成一团黑,看不清。)递归本来很简单,我写了如下的代码,这也是我后来代码的原型:
' The macro to excute Sub ReColor() Dim sld As Slide Dim sh As Shape For Each sld In ActivePresentation.Slides For Each sh In sld.Shapes Call ReColorSH(sh) Next Next End Sub Sub ReColorSH(sh As Shape) Dim ssh As Shape If sh.Type = msoGroup Then ' when the shape itself is a group For Each ssh In sh.GroupItems ReColorSH(ssh) ' the recursion Next ElseIf sh.Type = msoEmbeddedOLEObject Then ' recolor the equation If Left(sh.OLEFormat.ProgID, 8) = "Equation" Then sh.BlackWhiteMode = msoBlackWhiteBlack End If End If End Sub
' VBA cannot pass Shape into a function, so global var is used Public sh As Shape ' The macro to excute Sub ReColor() Dim sld As Slide For Each sld In ActivePresentation.Slides For Each sh In sld.Shapes Call ReColorSH Next Next End Sub Sub ReColorSH() Dim ip As Integer ' point to the top of the i stack Dim sp As Integer ' point to the top of the shape stack Dim istk() As Integer ' the i stack, using dynamic array Dim sstk() As Shape ' the Shape stack, using dynamic array Dim ssize As Integer ' the size of both stacks ssize = 10 ReDim istk(ssize) ReDim sstk(ssize) ip = 0 sp = 0 Dim i As Integer L2: If sh.Type = msoGroup Then i = 1 L1: 'pushS(sh) sp = sp + 1 If sp > ssize Then ssize = ssize + 1 ReDim Preserve istk(ssize) ReDim Preserve sstk(ssize) End If Set sstk(sp) = sh '---------- 'pushI (i) ip = ip + 1 istk(ip) = i '---------- Set sh = sh.GroupItems(i) GoTo L2 L3: 'popI(i) i = istk(ip) ip = ip - 1 '---------- 'popS(sh) Set sh = sstk(sp) sp = sp - 1 '---------- If i < sh.GroupItems.Count Then i = i + 1 GoTo L1 End If ElseIf sh.Type = msoEmbeddedOLEObject Then If Left(sh.OLEFormat.ProgID, 8) = "Equation" Then sh.BlackWhiteMode = msoBlackWhiteBlack End If End If If ip > 0 Then GoTo L3 End Sub
以下代码可以实现批量替换活动窗口中打开的演示文稿中的所有幻灯片中的文字颜色(具体颜色值请自行修改):
Dim s As Slide Dim shp As Shape Dim trng As TextRange Dim i As Integer ' /* 遍历活动窗口中打开的演示文稿中的幻灯片. */ For Each s In ActivePresentation.Slides ' /* 遍历当前幻灯片中的形状对象. */ For Each shp In s.Shapes ' /* 当前幻灯片中的当前形状含有文本框架. */ If shp.HasTextFrame Then ' /* 当前幻灯片中的当前形状包含文本. */ If shp.TextFrame.HasText Then ' 引用文本框架中的文本. Set trng = shp.TextFrame.TextRange ' /* 遍历文本框架中的每一个字符. */ For i = 1 To trng.Characters.Count ' 这里请自行修改为原来的颜色值 (浅绿色). If trng.Characters(i).Font.Color = vbRed Then ' 这里请自行修改为要替换的颜色值 (深绿色). trng.Characters(i).Font.Color = vbBlue End If Next End If End If Next Next
不错的帖子。我也写了一个类似的程序,但是跟你的一样,这个程序只能够把公式的文字转成黑白,不能转成其它颜色,例如蓝色或红色。我需要把公式全部变成红色...
Sub change_text_color() Dim oSld As Slide Dim oShp As Shape Dim oShapes As Shapes Dim textColor As RGBColor For Each oSld In ActivePresentation.Slides Set oShapes = oSld.Shapes For Each oShp In oShapes If oShp.Type = 7 Then oShp.PictureFormat.ColorType = msoPictureBlackAndWhite oShp.PictureFormat.Brightness = 0 oShp.PictureFormat.Contrast = 1 oShp.Fill.Visible = msoFalse End If Next oShp Next oSld End Sub