【问题标题】:VBA + PowerPoint: iterate over text in textboxes doesn't seem to find text by colorVBA + PowerPoint:迭代文本框中的文本似乎没有按颜色查找文本
【发布时间】:2014-07-31 15:12:40
【问题描述】:

我必须对我的 PowerPoint 幻灯片中的某些颜色进行搜索和替换。长话短说,我不允许更改模板配色方案。

我找到了这个 VBA 脚本http://skp.mvps.org/pptxp006.htm,它在线条/填充 PPT 图形上效果很好。但它似乎没有正确处理文本。

这个VBA脚本的一般做法是:

For Each oSld In ActivePresentation.Slides
    For Each oShp In oSld.Shapes
        If oShp.Type = msoGroup Then
        For I = 1 To oShp.GroupItems.Count
                [[ do something to oShp.GroupItems(i) ]]
            Next I
        Else
            [[ do something to oShp ]]        
    End If
    Next oShp
Next oSld

“做某事”包括对FindAndReColourText()的调用:

Function FindAndReColourText(oShp As Shape, _
    oRGB As Long, oNewRGB As Long)
Dim I As Integer
Dim oTxtRng As TextRange
On Error Resume Next
If oShp.HasTextFrame Then
    If oShp.TextFrame.HasText Then
        Set oTxtRng = oShp.TextFrame.TextRange            
        For I = 1 To oTxtRng.Runs.Count
            With oTxtRng.Runs(I).Font.Color                    
                If .Type = msoColorTypeRGB Then                       
                    If .rgb = oRGB Then
                        .rgb = oNewRGB
                    End If
                End If
            End With
        Next I
    End If
End If
End Function

我已经检查过了,它似乎确实遍历了文本,但它与颜色不匹配。关于我应该如何调试的任何建议?

(我真的不知道在哪里可以找到这个,有人很快就把这个任务交给了我,我需要更改几十张幻灯片。)

【问题讨论】:

    标签: vba automation powerpoint object-model


    【解决方案1】:

    啊哈——问题在于它不适用于使用模板颜色的文本; .Type = msoColorTypeRGB 测试不匹配。

    If .Type = msoColorTypeRGB Then                       
       If .rgb = oRGB Then
             .rgb = oNewRGB
       End If
    End If
    

    我删除了这个外部If,它工作正常。

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      相关资源
      最近更新 更多