我有一個項目可以從excel中查找和替換powerpoint中的單詞,然後保存該簡報。我的代碼工作正常。但是,當ppt有mp3,那麼它會給出錯誤。請參閱代碼並告訴我應該做什麼更改。使用excel中的宏查找並替換音頻ppt中的文本
Sub pptopen()
Dim a As Integer
For a = 2 To 4
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim i As Integer, strString As String
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPres = pptApp.Presentations.Add(msoTrue) ' create a new presentation
Set pptPres = pptApp.Presentations.Open("D:\BirminghamAL.pptx")
Dim oSld As Slide
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim strWhatReplace As String, strReplaceText As String
' write find text
strWhatReplace = "Birmingham"
' write change text
strReplaceText = Cells(a, 1).Value
' go during each slides
For Each oSld In pptPres.Slides
' go during each shapes and textRanges
For Each oshp In oSld.Shapes
If oshp.Type = 14 Or oshp.Type = 17 Then
' replace in TextFrame
Set oTxtRng = oshp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(_
FindWhat:=strWhatReplace, _
Replacewhat:=strReplaceText, _
WholeWords:=True)
End If
Do While Not oTmpRng Is Nothing
Set oTxtRng = oTxtRng.Characters _
(oTmpRng.Start + oTmpRng.Length, oTxtRng.Length)
Set oTmpRng = oTxtRng.Replace(_
FindWhat:=strWhatReplace, _
Replacewhat:=strReplaceText, _
WholeWords:=True)
Loop
Next oshp
Next oSld
Dim strWhatReplace1 As String, strReplaceText1 As String
' write find text
strWhatReplace1 = "AL"
' write change text
strReplaceText1 = Cells(a, 2).Value
' go during each slides
For Each oSld In pptPres.Slides
' go during each shapes and textRanges
For Each oshp In oSld.Shapes
If oshp.Type = 14 Or oshp.Type = 17 Then
' replace in TextFrame
Set oTxtRng = oshp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(_
FindWhat:=strWhatReplace1, _
Replacewhat:=strReplaceText1, _
WholeWords:=True)
End If
Do While Not oTmpRng Is Nothing
Set oTxtRng = oTxtRng.Characters _
(oTmpRng.Start + oTmpRng.Length, oTxtRng.Length)
Set oTmpRng = oTxtRng.Replace(_
FindWhat:=strWhatReplace1, _
Replacewhat:=strReplaceText1, _
WholeWords:=True)
Loop
Next oshp
Next oSld
pptPres.SaveAs ("D:\change\" & strReplaceText & "." & strReplaceText1 & ".pptx")
Next a
End Sub
如果有一個沒有mp3的powerpoint,那麼它工作正常。只要我呼叫音頻MP3,然後它給功能錯誤 – 2013-04-11 10:23:35
'對於oSld.Shapes'中的每個oShp你必須確保在此行之後並在此行之前'設置oTxtRng = oShp.TextFrame.TextRange'形狀是一個文本框 – 2013-04-11 10:39:15