Office中国论坛/Access中国论坛

标题: 【源码】Access代码全文搜索和替换PPT中的文字 [打印本页]

作者: tmtony    时间: 2015-7-9 09:29
标题: 【源码】Access代码全文搜索和替换PPT中的文字
如果我们需要对所有PPT里形状里的文字和内容进行搜索和替换时,以下这个通用函数就非常有用了。其实VBA代码是相通的。只要在Office环境中,VBA可实现很多自动化工作。

  1. ' --------------------------------------------------------------------------------
  2. ' Copyright ©1999-2015, Shyam Pillai, All Rights Reserved.
  3. ' --------------------------------------------------------------------------------
  4. ' You are free to use this code within your own applications, add-ins,
  5. ' documents etc but you are expressly forbidden from selling or
  6. ' otherwise distributing this source code without prior consent.
  7. ' This includes both posting free demo projects made from this
  8. ' code as well as reproducing the code in text or html format.
  9. ' --------------------------------------------------------------------------------
  10. Sub GlobalFindAndReplace()
  11. Dim oPres As Presentation
  12. Dim oSld As Slide
  13. Dim oShp As Shape
  14. Dim FindWhat As String
  15. Dim ReplaceWith As String

  16. FindWhat = "Like"
  17. ReplaceWith = "Not Like"
  18. For Each oPres In Application.Presentations
  19.      For Each oSld In oPres.Slides
  20.         For Each oShp In oSld.Shapes
  21.             Call ReplaceText(oShp, FindWhat, ReplaceWith)
  22.         Next oShp
  23.     Next oSld
  24. Next oPres
  25. End Sub

  26. Sub ReplaceText(oShp As Object, FindString As String, ReplaceString As String)
  27. Dim oTxtRng As TextRange
  28. Dim oTmpRng As TextRange
  29. Dim I As Integer
  30. Dim iRows As Integer
  31. Dim iCols As Integer
  32. Dim oShpTmp As Shape

  33. ' Always include the 'On error resume next' statement below when you are working with text range object.
  34. ' I know of at least one PowerPoint bug where it will error out - when an image has been dragged/pasted
  35. ' into a text box. In such a case, both HasTextFrame and HasText properties will return TRUE but PowerPoint
  36. ' will throw an error when you try to retrieve the text.
  37. On Error Resume Next
  38. Select Case oShp.Type
  39. Case 19 'msoTable
  40.     For iRows = 1 To oShp.Table.Rows.Count
  41.         For icol = 1 To oShp.Table.Rows(iRows).Cells.Count
  42.             Set oTxtRng = oShp.Table.Rows(iRows).Cells(iCol).Shape.TextFrame.TextRange
  43.             Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
  44.                                   Replacewhat:=ReplaceString, WholeWords:=True)
  45.             Do While Not oTmpRng Is Nothing
  46.             Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
  47.                                 Replacewhat:=ReplaceString, _
  48.                                 After:=oTmpRng.Start + oTmpRng.Length, _
  49.                                 WholeWords:=True)
  50.             Loop
  51.         Next
  52.     Next
  53. Case msoGroup 'Groups may contain shapes with text, so look within it
  54.     For I = 1 To oShp.GroupItems.Count
  55.         Call ReplaceText(oShp.GroupItems(I), FindString, ReplaceString)
  56.     Next I
  57. Case 21 ' msoDiagram
  58.     For I = 1 To oShp.Diagram.Nodes.Count
  59.         Call ReplaceText(oShp.Diagram.Nodes(I).TextShape, FindString,     ReplaceString)
  60.     Next I
  61. Case Else
  62.     If oShp.HasTextFrame Then
  63.         If oShp.TextFrame.HasText Then
  64.             Set oTxtRng = oShp.TextFrame.TextRange
  65.             Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
  66.                 Replacewhat:=ReplaceString, WholeWords:=True)
  67.             Do While Not oTmpRng Is Nothing
  68.                 Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
  69.                             Replacewhat:=ReplaceString, _
  70.                             After:=oTmpRng.Start + oTmpRng.Length, _
  71.                             WholeWords:=True)
  72.             Loop
  73.        End If
  74.     End If
  75. End Select
  76. End Sub
复制代码
摘自 Shyam Pillai 文章

作者: wuwu200222    时间: 2020-11-30 14:30
学习




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3