用Excel表中的PowerPoint在PowerPoint中替换多个查找

发布于 2025-02-03 19:48:24 字数 3071 浏览 1 评论 0原文

我正在尝试在下面的网页中自定义脚本,以使用预定义的Excel列表在PowerPoint文档中查找和替换单词和短语。下面的代码最适合我,但我需要与长替换列表一起使用。我尝试过很多次,但未能获得正确的数组。 Excel列表非常长,并且具有两个带标头的列:“查找”和“替换为“

excel文档名称:offices.xlsx

”路径:c:\ users \ users \ jl \ docuemnts

sheet name:sheet1

我得到此错误:(运行- time错误'424':需要对象)在此行:

set wb = workbooks.open(“ c:\ users \ jl \ docuemnts \ ofces.xlsx”)“

任何帮助都将不胜感激。

谢谢

(源脚本:: https://www.mmsofficeforums.com/powerpoint/20104-find-find--find--find-point-替换麦克罗.html

Sub PPTFindAndReplace()
Dim oPres As Presentation
Dim oSld As Slide
Dim oShp As Shape
Dim FindWhat As String
Dim ReplaceWith As String
Dim x As Integer
Set wb = Workbooks.Open("C:\Users\JL\Docuemnts\Offices.xlsx")
myArray = Workbook.Sheets("Sheet1").Range("a2:a200").Value
myArray2 = Workbook.Sheets("Sheet1").Range("b2:b200").Value
FindWhat = myArray(x)
ReplaceWith = myArray2(x)
For Each oPres In Application.Presentations
For Each oSld In oPres.Slides
For Each oShp In oSld.Shapes
Call ReplaceText(oShp, FindWhat, ReplaceWith)
Next oShp
Next oSld
Next oPres
End Sub
Sub ReplaceText(oShp As Object, FindString As String, ReplaceString As String)
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim I As Integer
Dim iRows As Integer
Dim iCols As Integer
Dim oShpTmp As Shape

' Always include the 'On error resume next' statement below when you are working with text range object.
' I know of at least one PowerPoint bug where it will error out - when an image has been dragged/pasted
' into a text box. In such a case, both HasTextFrame and HasText properties will return TRUE but PowerPoint
' will throw an error when you try to retrieve the text.

On Error Resume Next
Select Case oShp.Type
Case 19 'msoTable
For iRows = 1 To oShp.Table.Rows.Count
For iCol = 1 To oShp.Table.Rows(iRows).Cells.Count
Set oTxtRng = oShp.Table.Rows(iRows).Cells(iCol).Shape.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, WholeWords:=False)
Do While Not oTmpRng Is Nothing
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, _
After:=oTmpRng.Start + oTmpRng.Length, _
WholeWords:=False)
Loop
Next
Next
Case msoGroup 'Groups may contain shapes with text, so look within it
For I = 1 To oShp.GroupItems.Count
Call ReplaceText(oShp.GroupItems(I), FindString, ReplaceString)
Next I
Case 21 ' msoDiagram
For I = 1 To oShp.Diagram.Nodes.Count
Call ReplaceText(oShp.Diagram.Nodes(I).TextShape, FindString, ReplaceString)
Next I
Case Else
If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
Set oTxtRng = oShp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, WholeWords:=False)
Do While Not oTmpRng Is Nothing
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, _
After:=oTmpRng.Start + oTmpRng.Length, _
WholeWords:=False)
Loop
End If
End If
End Select
End Sub

I am trying to customize the script in the webpage below to find and replace words and phrases in a PowerPoint document using a pre-defined Excel list. The code below worked best for me but I need to use with long replacement lists. I have tried many times but failed to get the correct array.
The excel list is very long and has two columns with headers: "Find what" and "Replace with"

Excel document name: Offices.xlsx

Path: C:\Users\JL\Docuemnts

Sheet name: Sheet1

I get this error: (Run-time error '424': Object required) at this line:

Set wb = Workbooks.Open("C:\Users\JL\Docuemnts\Offices.xlsx")"

Any help would be much appreciated.

Thank you

(Source script: https://www.msofficeforums.com/powerpoint/20104-find-replace-macro.html)

Sub PPTFindAndReplace()
Dim oPres As Presentation
Dim oSld As Slide
Dim oShp As Shape
Dim FindWhat As String
Dim ReplaceWith As String
Dim x As Integer
Set wb = Workbooks.Open("C:\Users\JL\Docuemnts\Offices.xlsx")
myArray = Workbook.Sheets("Sheet1").Range("a2:a200").Value
myArray2 = Workbook.Sheets("Sheet1").Range("b2:b200").Value
FindWhat = myArray(x)
ReplaceWith = myArray2(x)
For Each oPres In Application.Presentations
For Each oSld In oPres.Slides
For Each oShp In oSld.Shapes
Call ReplaceText(oShp, FindWhat, ReplaceWith)
Next oShp
Next oSld
Next oPres
End Sub
Sub ReplaceText(oShp As Object, FindString As String, ReplaceString As String)
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim I As Integer
Dim iRows As Integer
Dim iCols As Integer
Dim oShpTmp As Shape

' Always include the 'On error resume next' statement below when you are working with text range object.
' I know of at least one PowerPoint bug where it will error out - when an image has been dragged/pasted
' into a text box. In such a case, both HasTextFrame and HasText properties will return TRUE but PowerPoint
' will throw an error when you try to retrieve the text.

On Error Resume Next
Select Case oShp.Type
Case 19 'msoTable
For iRows = 1 To oShp.Table.Rows.Count
For iCol = 1 To oShp.Table.Rows(iRows).Cells.Count
Set oTxtRng = oShp.Table.Rows(iRows).Cells(iCol).Shape.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, WholeWords:=False)
Do While Not oTmpRng Is Nothing
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, _
After:=oTmpRng.Start + oTmpRng.Length, _
WholeWords:=False)
Loop
Next
Next
Case msoGroup 'Groups may contain shapes with text, so look within it
For I = 1 To oShp.GroupItems.Count
Call ReplaceText(oShp.GroupItems(I), FindString, ReplaceString)
Next I
Case 21 ' msoDiagram
For I = 1 To oShp.Diagram.Nodes.Count
Call ReplaceText(oShp.Diagram.Nodes(I).TextShape, FindString, ReplaceString)
Next I
Case Else
If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
Set oTxtRng = oShp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, WholeWords:=False)
Do While Not oTmpRng Is Nothing
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
ReplaceWhat:=ReplaceString, _
After:=oTmpRng.Start + oTmpRng.Length, _
WholeWords:=False)
Loop
End If
End If
End Select
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文