设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2923|回复: 1
打印 上一主题 下一主题

[与其它组件] 【源码】Access代码全文搜索和替换PPT中的文字

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2015-7-9 09:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如果我们需要对所有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 文章
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

2#
发表于 2020-11-30 14:30:35 | 只看该作者
学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-12-1 20:46 , Processed in 0.095993 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表